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 --- example/scripts/global_events.lua | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'example/scripts') diff --git a/example/scripts/global_events.lua b/example/scripts/global_events.lua index fe4175b..c320d16 100644 --- a/example/scripts/global_events.lua +++ b/example/scripts/global_events.lua @@ -10,24 +10,29 @@ --]] - --- This function is called when the hit points of a character reach zero. -function on_chr_death(ch) +-- Register the callback that is called when the hit points of a character +-- reach zero. +mana.on_character_death(function(ch) mana.being_say(ch, "Noooooo!!!") -end +end) -- This function is called when the player clicks on the OK button after the -- death message appeared. It should be used to implement the respawn -- mechanic (for example: warp the character to the respawn location and -- bring HP above zero in some way) -function on_chr_death_accept(ch) +mana.on_character_death_accept(function(ch) -- restores to full hp mana.being_heal(ch) -- restores 1 hp (in case you want to be less nice) -- mana.being_heal(ch, 1) -- warp the character to the respawn location mana.chr_warp(ch, 1, 815, 100) -end +end) + + +-- +-- TODO: All of the below functions are not implemented yet! +-- -- This function is called after chr_death_accept. The difference is that -- it is called in the context of the map the character is spawned on after -- cgit