I’ve been struggling heavily with thinking about how to best structure a game instead of actually making a game lately, and one thing I’ve been hung up on is the best way to handle shared base functionality between objects in Defold.
For example, say I’m working on a platformer with defeatable enemies. I’d want health/damage handling, respawning, and basic movement/collision to be encapsulated and shared between all enemy objects in the game, among other functionality.
I can think of two approaches to handle this, but they both have caveats:
- Putting the functionality in a module that’s required and called by the enemy’s script. Main issue is that the Lua in Defold manual directly advises against modfying the internals of a game object with a module, and in this example I’d most likely be manipulating the position of a game object at the very least.
- Use two script components per enemy object (one with base functionality, the other with enemy-specific code) and communicate between the two with messages and properties. Problem with this is that the manuals state there’s no consistent order to
update()
being called, and I assume this is also true for multiple scripts in a single object. In this example, I’d want to guarantee the base script component executes first.
Any suggestions on how I’d work around these issues, or any approaches that would be better than either of these? Defold is very much a “there’s more than one way to do it” engine from what I understand, so I’m not looking for a one-size-fits-all solution.