Monster API clones from Entity API, read more
This is my solution for handling inheritance via lua. The Entity class is only a base class, so the entity API is designed to be copied from, rather than used directly. linit.c: It should be noted that the Entity API must always be placed before the utilizing child APIs. I don't know about how lua handles things internally, but I'm assuming that this is the case. There's no real meat in the API code yet, since that's just busy-work. Right now I feel beter about writing the connective tissue. This case could aslo extend to the waypoint and monster APIs. The waypoint system had some API and class methods removed for brevity.
This commit is contained in:
@@ -23,11 +23,55 @@
|
||||
|
||||
#include "monster_data.hpp"
|
||||
|
||||
#include "entity_api.hpp"
|
||||
|
||||
static int setAvatar(lua_State* L) {
|
||||
//TODO
|
||||
}
|
||||
|
||||
static int getAvatar(lua_State* L) {
|
||||
//TODO
|
||||
}
|
||||
|
||||
static int setScript(lua_State* L) {
|
||||
//TODO
|
||||
}
|
||||
|
||||
static int getScript(lua_State* L) {
|
||||
//TODO
|
||||
}
|
||||
|
||||
static const luaL_Reg monsterLib[] = {
|
||||
{"SetAvatar", setAvatar},
|
||||
{"GetAvatar", getAvatar},
|
||||
{"SetScript", setScript},
|
||||
{"GetScript", getScript},
|
||||
{nullptr, nullptr}
|
||||
};
|
||||
|
||||
LUAMOD_API int openMonsterAPI(lua_State* L) {
|
||||
//the local table
|
||||
luaL_newlib(L, monsterLib);
|
||||
|
||||
//get the parent table
|
||||
luaL_requiref(L, TORTUGA_ENTITY_API, openEntityAPI, false);
|
||||
|
||||
//clone the parent table into the local table
|
||||
lua_pushnil(L); //first key
|
||||
while(lua_next(L, -2)) {
|
||||
//copy the key-value pair
|
||||
lua_pushvalue(L, -2);
|
||||
lua_pushvalue(L, -2);
|
||||
|
||||
//push the copy to the local table
|
||||
lua_settable(L, -6);
|
||||
|
||||
//pop the original value before continuing
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
|
||||
//remove the parent table, leaving the expanded child table
|
||||
lua_pop(L, 1);
|
||||
|
||||
return 1;
|
||||
}
|
||||
Reference in New Issue
Block a user