1 /******************************
2 ** Tsunagari Tile Engine **
4 ** Copyright 2011 OmegaSDG **
5 ******************************/
24 : ownState(true), L(lua_open())
29 Script::Script(lua_State
* L
)
30 :ownState(false), L(L
)
40 void Script::bindGlobalFn(const char* name
, lua_CFunction fn
)
42 lua_register(L
, name
, fn
);
45 void Script::bindObjFn(const char* table
, const char* index
, lua_CFunction fn
)
48 lua_getglobal(L
, table
);
51 lua_pushstring(L
, index
);
52 lua_pushcfunction(L
, fn
);
59 void Script::bindInt(const char* name
, lua_Integer i
)
61 lua_pushinteger(L
, i
);
62 lua_setglobal(L
, name
);
65 void Script::bindEntity(const char* name
, Entity
* entity
)
67 // Create table to hold our object and its functions/variables.
68 lua_createtable(L
, 0, 3);
70 lua_pushstring(L
, "object");
72 // Create type-aware wrapper around Entity.
73 CppObj
* obj
= (CppObj
*)lua_newuserdata(L
, sizeof(CppObj
));
77 // table.object = entity
81 lua_setglobal(L
, name
);
84 Entity
* Script::getEntity(int pos
)
87 if (!lua_istable(L
, pos
))
89 lua_pushstring(L
, "object");
92 // Check if table.object is an Entity
93 if (!lua_isuserdata(L
, -1))
95 CppObj
* obj
= (CppObj
*)lua_touserdata(L
, -1);
96 if (obj
->type
!= ENTITY
)
101 void Script::run(Resourcer
* rc
, const char* fn
)
103 if (!rc
->getLuaScript(fn
, L
)) // error logged
106 // TODO: make fourth parameter to lua_call an error handler so we can
107 // gather stack trace through Lua state... we can't do it after the
109 switch (lua_pcall(L
, 0, 0, 0)) {
111 Log::err("Script::run", lua_tostring(L
, -1));
114 // Should we even bother with this?
115 Log::err("Script::run", "Lua: out of memory");
118 Log::err("Script::run", "error in Lua error facility"
119 "... what did you do!?");