EDIT:
Updated the ModObject url to a newer version
I've made this really small but incredibly useful module that lets you create, connect, and fire custom events!
It requires the "ModObject" OOP module found here: https://gist.github.com/Elmuti/76af9f55ac1cba475f26
local Signal = require("Signal")
Player.Died = Signal.New() --our event named "PlayerDied", let's make it a member of a player class
function playerDeath(player) --"listener" function
print("player "..Player.Name.." just died!")
--reset game state and do stuff
end
Player.Died:Connect(playerDeath) --connects the listener function to the event
--and somewhere in your code where a player gets killed, you just do this:
Player.Died:Fire(player)
TODO:
Signal:Wait() --Yields the current thread until the signal fires
Last edited by ElmuKelmuZ on Sun May 24, 2015 2:39 pm, edited 3 times in total.
function playerDeath(player)
print("player " .. player.Name .. " just died!")
end
Player.Died = playerDeath
-- and later
Player.Died(player)
Maybe it'd be more useful if you could have multiple connections, instead of just one?
Could be really useful. Imagine you have a player that can die, and when the player dies, you want to change the game state to "Game Over". If you don't have a signal, that means you'd need to pass the game state to the method Player:is_hit() so that you can do game_state.gotoState('Game Over'). If you have a signal, you can uncouple the game state and the hero.
function playerDeath(player)
print("player " .. player.Name .. " just died!")
end
Player.Died = playerDeath
-- and later
Player.Died(player)
Maybe it'd be more useful if you could have multiple connections, instead of just one?
Could be really useful. Imagine you have a player that can die, and when the player dies, you want to change the game state to "Game Over". If you don't have a signal, that means you'd need to pass the game state to the method Player:is_hit() so that you can do game_state.gotoState('Game Over'). If you have a signal, you can uncouple the game state and the hero.
Well... the solution to that is to just check if the player is dead outside of the player.
function playerDeath(player)
print("player " .. player.Name .. " just died!")
end
Player.Died = playerDeath
-- and later
Player.Died(player)
Maybe it'd be more useful if you could have multiple connections, instead of just one?
Could be really useful. Imagine you have a player that can die, and when the player dies, you want to change the game state to "Game Over". If you don't have a signal, that means you'd need to pass the game state to the method Player:is_hit() so that you can do game_state.gotoState('Game Over'). If you have a signal, you can uncouple the game state and the hero.
Well... the solution to that is to just check if the player is dead outside of the player.