HI, I’m rather new to Defold and trying to make a simple game.
I have a player gameobject that I want several other gameobjects to know when it has been killed, such as the camera gameobject, level manager, enemies, etc
What is the best way to do this? Have the player post messages to all of the other gameobjects?
Usually when using other engines I would have an event on the player and then have the other gameobjects register to that event. What’s the best way to do that in Defold?
Thanks
I think you should just tell the level manager that you are dead and no one else. The manager does the (managing) rest as required. You can also use Lua modules to share information/data about.
You can create a broadcast and listener system using a Lua module. Here’s one such example:
Yeah, broadcasting is a good idea, I use my library for this
If you’ll use it, let me know if you find it good or not compared to solutions in other engines
Other solution is for example having a simple Lua module with a table only - where you can mark what objects are removed and other systems can read it (or negative - mark which are “alive” an operate only on them). Lua tables inside modules, when required are instantiated and you have only one instance of it, so each script modifying it - modifies the only instance (of course you can create multiple instances too, but that’s different topic )
Also want to share my approach with event library - Defold Event - Cross-Context Defold Event System
Easy to use, with instant callbacks, with different options you can find useful in your cases