From 84c87cc99be29a694f0ffe83ab7a06ae433bb0cd Mon Sep 17 00:00:00 2001 From: Thorbjørn Lindeijer Date: Fri, 2 Mar 2012 23:17:07 +0100 Subject: Use callbacks for items, monsters and status effects Previously, global function names were defined in the respective XML definitions of items, monsters and status effects. This was reasonable when they all had the same state, but now they're sharing the single global Lua state. Now the Lua API provides access to the ItemClass, MonsterClass and StatusEffect instances, on which callbacks for both standard and custom events can be explicitly set. Reviewed-by: Erik Schilling --- src/scripting/luautil.h | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) (limited to 'src/scripting/luautil.h') diff --git a/src/scripting/luautil.h b/src/scripting/luautil.h index 049276fe..2fc7543a 100644 --- a/src/scripting/luautil.h +++ b/src/scripting/luautil.h @@ -31,12 +31,14 @@ extern "C" { #include #include -#include "game-server/map.h" - class Being; -class NPC; class Character; +class ItemClass; +class MapObject; class Monster; +class MonsterClass; +class NPC; +class StatusEffect; class Thing; // Report script errors and interrupt the script. @@ -105,10 +107,16 @@ public: /** * Pushes a userdata reference to the given object on the stack. Either by * creating one, or reusing an existing one. + * + * When a null-pointer is passed for \a object, the value 'nil' is pushed. */ - static int push(lua_State *s, T *object) + static void push(lua_State *s, T *object) { - if (!UserDataCache::retrieve(s, object)) + if (!object) + { + lua_pushnil(s); + } + else if (!UserDataCache::retrieve(s, object)) { void *userData = lua_newuserdata(s, sizeof(T*)); * static_cast(userData) = object; @@ -118,8 +126,6 @@ public: UserDataCache::insert(s, object); } - - return 1; } /** @@ -138,7 +144,10 @@ private: template const char * LuaUserData::mTypeName; +typedef LuaUserData LuaItemClass; typedef LuaUserData LuaMapObject; +typedef LuaUserData LuaMonsterClass; +typedef LuaUserData LuaStatusEffect; NPC *getNPC(lua_State *s, int p); -- cgit v1.2.3-60-g2f50