(Thanks @Pkeod!)
Here you go @Hakim_A, because you asked so nicely.
You essentially have three options, depending on your game and preference.
- The thing keeping level score is the central thing so it does not need to be sent off, it takes any decisions what to do internally. Other things send it messages to increment the score by directly addressing it. (e.g. msg.post("/my_central_thing#script", “inc_score”, {added_score = 100}) I prefer doing things in one place until I see a reason to break things apart, or know more where the game is going and what I need.
- The thing keeping level score knows which thing needs to be notified. In my previous example, listener would result in something like “/the_other_thing#script”.
- Other things register to be notified about changes to the level score, e.g. that it passed known limits like in my example. The things need to be able to address the score keeper. That would look like this:
In the thing that registers itself:
msg.post("/score_keeper#script", "register_me")
In the thing keeping scores:
function on_message(self, message_id, message, sender)
if message_id == hash("register_me") then
-- self.listeners was initialized to an empty table in the init function
table.insert(self.listeners, sender) -- note that sender will be the thing that posted the message above
end
end
Then self.listeners would be iterated in my example where it said – for each listener, just like an ordinary lua table. There is a ton of examples how to iterate tables. (hint: ipairs) Note that I intentionally chose ridiculous names so you can replace them with things that makes more sense for your situation.