From ba5b55f3eba0aa3898c5fe42de9838b22473c24a Mon Sep 17 00:00:00 2001 From: Thorbjørn Lindeijer Date: Tue, 28 Feb 2012 23:37:23 +0100 Subject: Use callbacks for handling character death and respawn Rather than relying on the availability of global functions with certain predefined names, the Lua script now calls API functions to set which function should be called on these global events. This mechanism should make it easier to avoid name collisions in the global namespace, which is important now that there is only a single script state. For these global events this was not likely to become a problem, but this solution can also be used for callbacks on specific item or monster types, or even allow setting callbacks on certain instances. Reviewed-by: Erik Schilling Reviewed-by: Yohann Ferreira --- src/scripting/lua.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'src/scripting/lua.cpp') diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp index 9aec6b3f..7a94ce24 100644 --- a/src/scripting/lua.cpp +++ b/src/scripting/lua.cpp @@ -73,6 +73,30 @@ static Script *getScript(lua_State *s) } +/** + * mana.on_character_death( function(Character*) ): void + * Sets a listener function to the character death event. + */ +static int on_character_death(lua_State *s) +{ + luaL_checktype(s, 1, LUA_TFUNCTION); + Character::setDeathCallback(getScript(s)); + return 0; +} + +/** + * mana.on_character_death_accept( function(Character*) ): void + * Sets a listener function that is called when the player clicks on the OK + * button after the death message appeared. It should be used to implement the + * respawn mechanic. + */ +static int on_character_death_accept(lua_State *s) +{ + luaL_checktype(s, 1, LUA_TFUNCTION); + Character::setDeathAcceptedCallback(getScript(s)); + return 0; +} + /** * mana.npc_message(NPC*, Character*, string): void * Callback for sending a NPC_MESSAGE. @@ -2564,6 +2588,8 @@ LuaScript::LuaScript(): // Put the callback functions in the scripting environment. static luaL_Reg const callbacks[] = { + { "on_character_death", &on_character_death }, + { "on_character_death_accept", &on_character_death_accept }, { "npc_create", &npc_create }, { "npc_message", &npc_message }, { "npc_choice", &npc_choice }, -- cgit v1.2.3-70-g09d2