diff options
author | Thorbjørn Lindeijer <thorbjorn@lindeijer.nl> | 2012-02-28 23:37:23 +0100 |
---|---|---|
committer | Thorbjørn Lindeijer <thorbjorn@lindeijer.nl> | 2012-03-02 18:12:20 +0100 |
commit | ba5b55f3eba0aa3898c5fe42de9838b22473c24a (patch) | |
tree | 465e70d3bc4671b75f1af763d866eafa459f5d6f /src/scripting/lua.cpp | |
parent | 05bb880a3b0ebe401bac83b7a2400aa315161f9c (diff) | |
download | manaserv-ba5b55f3eba0aa3898c5fe42de9838b22473c24a.tar.gz manaserv-ba5b55f3eba0aa3898c5fe42de9838b22473c24a.tar.bz2 manaserv-ba5b55f3eba0aa3898c5fe42de9838b22473c24a.tar.xz manaserv-ba5b55f3eba0aa3898c5fe42de9838b22473c24a.zip |
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
Diffstat (limited to 'src/scripting/lua.cpp')
-rw-r--r-- | src/scripting/lua.cpp | 26 |
1 files changed, 26 insertions, 0 deletions
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 @@ -74,6 +74,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 }, |