Making Sense of the Defold Logical Workflow

Hey everyone! Here I am again, bothering you all with my Defold learning journey! :slight_smile:

After getting a better understanding of how variables work in Defold, I decided to dig a little deeper into the engine. So far, I’ve come to believe that the biggest hurdle to overcome isn’t the coding itself or the component system, but rather the engine’s overall workflow and logic, which are quite different from those of other engines I’ve used in the past.

One thing that has puzzled me the most is the messaging system. I often see people praising it as one of Defold’s greatest strengths, and while I can certainly see how powerful and flexible it is, I can’t help wondering whether relying heavily on messages could become difficult to manage once a project grows beyond the scope of a tutorial or a game jam.

As a project gets bigger, I imagine that keeping track of all the messages flying around could eventually become quite a challenge.

This got me thinking about how to structure a Defold project in a way that remains manageable as it grows. My current theory is that, rather than relying too heavily on messages, it might make more sense to move most of the game’s logic into dedicated Lua modules, each responsible for a specific purpose and accessible to whichever scripts need them.

In other words, I’m imagining a structure along these lines:

  • Small scripts attached to individual game objects, handling their specific behaviour.

  • Messages for simple, localized interactions between game objects.

  • Modules for shared logic and systems that need to be accessed from multiple places.

The idea would be to keep game object scripts relatively small and focused, with most of the more complex logic living in dedicated modules rather than being spread across numerous scripts communicating through messages.

Is this a common approach when working with Defold, or am I misunderstanding something about how the engine is intended to be used?

I’d love to hear how more experienced Defold developers approach this, especially when working on larger projects.

Thanks a bunch! :slight_smile:

1 Like

I agree with you. I find message passing difficult to debug, I avoid it for things I can do with modules. I try to only use it when it is obviously the best approach, for example sending an NPC a message that it has been hit, so it can react, works for me. Whereas central game control like data I prefer to be in a module.

I think it is that message passing needs more explaining than modules, rather than it being preferred to them. However this is my opinion and I am happy to be wrong, as then I can learn :slight_smile:

2 Likes

I agree with your thinking. Finding a good balance between messages and modules is definitely an important part of structuring a Defold project.

I actually wrote a blog post about how I usually structure my Defold games. Maybe you’d find it interesting.

https://orenjispark.com/blog/posts/how-i-usually-structure-defold-games

7 Likes

That’s a nice blog post! I think I am working quite like you, your post said it entirely.
I think The point on “keep it simple” is important, because at start I was designing a ton of OOP classes and being systematic about everything…but in the end of the day 80% of the game code wont be reused.
Some patterns will, some librairies too or big systems like audio, files saving, scene managers or bus events, but the rest should be just simple.
If it’s a simple gameobject (like a chest), just put all in a script and that’s it.
for the Player character or complexe behaviours, yes create modules with its own state, then use .script to compose and glue everything together. Design systems inside lua module, compose them inside .scripts.

Regarding message passing, I view it as an interface, a contract between systems.
I also use pigeon. My rule of thumb is:

  • every interaction between GUI and GO is through message bus
  • every interaction between objects and different services is through message bus
  • direct interactions between two GO because of collisions is direct msg post
  • internal communication is through direct msg post (like a GO talking to some of its parts or systems).
  • I centralize all messages definitions in one file with annotations, so you can autocomplete everywhere and know what’s the message payload.

What I do also is to centralise a gamedata.lua module that keeps configuration of the game (like base speed of player, gravity, name of collision groups etc.)
or content data (like level data, number of enemies to spawn etc.)
(they can be built from multiple subfiles)
Like this I can expose this data through Imgui for ingame live adjustment of gameplay (I am just missing a writeback system to savec my changes directly. Right now I still need to persist changes manualy)

I think one good practice is: never hardcode things. Put that in a variable you can change later, use the variable in code. For everything: url, settings, physical values etc. Centralise these variables.

unless it is really internal to a gameobject, but then never write it twice!
For example, store the url of sprite in self.sprite, then never write ‘#sprite’ again.

3 Likes

Here’s a way to think about this that might help clarify what makes a good setup for your project’s needs:

You’re creating your architecture for two distinct use-cases: to author content during development (e.g. editing scenes and levels in the editor) and to run the game simulation (for the end user or you as the developer). These have different needs, but you will be using game objects etc. in both.

During authoring, game objects and components are how you specify what will be packaged in your collections. They also allow you to use of the editor’s UI to edit them.

At runtime, game objects and components are your interface to the engine. E.g. if you want to add something like a sound, visual, or physics object to the simulation, you do it by creating a component on a game object. Same thing for messages; since the engine only knows about game objects and components, that’s what messages are sent from and to.

If your game has no procedural content, then it shouldn’t get too complicated, as the “world” seen during authoring is more or less what the world looks like during runtime. But if the levels and most of the stuff in them are procedurally generated, then it can get difficult to make authoring, debugging and playtesting all have great UX/UI. Maybe you’ll need to make custom data editors and ingame data visualizers.

So, with all that said, I like to start from the position of having a single game object and script component in the game. Then I ask myself the question, what are the authoring issues I need to solve, and do they need additional game objects and components? I also ask myself, at runtime, what does the engine need to do for me and be aware of (e.g. draw a sprite somewhere or simulate a physics object), and should I spawn these game objects and components on demand or when the collection is loaded?

The messaging question then becomes one way to solve certain problems that arise. I generally don’t use messages when I a direct function call from one module to another will do, since that leaves me in full control of when things execute.

5 Likes

Yeah, that’s exactly my point! :grinning_face_with_smiling_eyes:

I’m thinking that, as powerful as the messaging system may be, relying too heavily on it could eventually turn a project into a mess to navigate, maintain, and debug. As the project grows, I can imagine ending up with messages flying all over the place, with different objects communicating with each other in ways that become increasingly difficult to keep track of.

That’s why I feel that message passing can become problematic if it’s not kept under control. It’s not so much about preferring one approach over another, but about avoiding a situation where the entire project becomes heavily dependent on messages flying back and forth between different objects.

Personally, I’d rather keep most of the game logic and shared data in well-organized modules, and use messages only when there’s a good reason to have different objects communicate with each other.

Of course, I’m still learning how Defold works, so I might be missing something here. But from a code organization and maintainability standpoint, keeping message passing to a minimum seems like a sensible approach to me. :sweat_smile:

That was a really interesting and useful blog post, thanks for sharing it! :grinning_face_with_smiling_eyes:

I found the section about the event bus particularly interesting, especially the idea of using events to decouple systems without having everything communicate directly with each other.

I’ll definitely look into Pigeon and how to implement an event bus in Defold. I’m also really interested in the idea of using scene controllers to coordinate the different parts of a scene, as that seems like it could help keep things organized as a project grows.

Lots of things to learn, but this gives me a much better idea of what to look into next. Thanks again! :blush:

1 Like

Thanks for sharing your thoughts! :grinning_face_with_smiling_eyes:

I agree that keeping things simple is important. I tend to think a lot about how to organize a project because I want to make sure it stays readable and easy to maintain as it grows.

After reading the blog post mentioned earlier in the discussion, I started thinking about using an event system similar to the Observer pattern in Unity. The idea would be to have Game Objects emit events, and let any interested systems subscribe to them. This way, I’d have a central place to manage events without having to manually connect every system to every other system.

I’d probably use direct messages for interactions between specific Game Objects, such as collisions, and Lua modules for shared data and logic.

For example, each enemy would keep its own stats in its script, while an Enemy Manager could track things like how many enemies have been defeated in a level. A separate Drop Manager could handle loot drops if the system became complex enough to justify it.

I’m still figuring out what works best in Defold, so these are just ideas I’m exploring. My main goal is to keep things modular and make it easy to understand what’s happening in the project, without overcomplicating everything from the start.

Thanks for sharing your experience! :blush:

Thanks, that’s an interesting way to look at it! :blush:

I see your point about starting simple and adding things only when there’s a real need for them. I also agree that direct function calls are often a better choice when one module needs to communicate with another specific module.

What I’m still trying to figure out is how to keep systems decoupled as the project grows. For example, when an enemy dies, several systems might need to react to it, such as the enemy counter, the loot system, or a quest system.

That’s why I was thinking about an event system based on the Observer pattern, where a system can emit an event without needing to know who’s listening for it. Of course, I wouldn’t use it for everything, and direct messages or function calls would still make sense in many situations.

I’m still learning how Defold handles these things, so it’s really helpful to hear how other people approach architecture in their projects. Thanks for sharing your perspective!

I just want to thank everyone who has taken the time to reply to my posts. Your answers have been extremely helpful in helping me reason about how I want to use Defold when I finally make the switch.

As you can imagine, coming from years of object-oriented programming in game development, Defold feels extremely confusing to me, almost alien. To be honest, its workflow often feels like it’s designed around the engine’s internal logic rather than the way a developer would naturally expect things to work. I often struggle to understand the reasoning behind some of its design choices.

I do get discouraged sometimes when studying the engine, because it feels like every new thing I learn just adds another layer of confusion instead of making things clearer. I’ve even considered giving up on Defold and switching to something like Cocos.

But in the end, I’m really invested in learning it because I believe Defold could offer a great deal of creative freedom once I truly understand how to work with it. So I’m sticking with it!

That’s why I’m putting so much effort into trying to make sense of it and figuring out an approach that works for me.

I really appreciate everyone’s patience and willingness to share their experience. It’s been a huge help!

I have open this feature request a while ago:

For now I am using the very good Pigeon extension (just use that for intersystem communication), but I was feeling this should be builtin!
You can vote on this to push it to a release maybe one day

1 Like

Nice! It’s very good to know I was not the only one thinking about such a solution, and know that even experienced developers thought about it!:grinning_face_with_smiling_eyes:

You can for sure do it with observers or similar.

Another way is to separate data and logic. The data is all your plain old data structures that makes up your game state. Have logic (i.e. systems) only read and write data, and never directly interact with other systems.

When a system causes something to happen that should trigger a reaction in another system, you either create a data object or change a value in the game state to indicate this. Later during the frame update, the reactive system checks for these objects or values, and if they are found it executes. At the end of the frame update, you may clear any ephemeral objects used only as signals.

In my current project I essentially have an update function that calls/runs each system once. The calls are ordered manually in such a way that data dependencies are honored. For example, enemies called wave_unit will spawn and attack you, and here are some systems that updates them:

  • wave_unit_spawner: does some logic to determine if it should create any new spawning_wave_unit objects.
  • wave_unit_spawn_complete: iterates over any spawning_wave_unit objects and checks if they have completed their spawning animation and should become idle_wave_unit.
  • wave_unit_rebalance: iterates over all idle_wave_unit objects and gives them something to do, like turning them into attacking_wave_unit.
  • wave_unit_attack: iterates over any attacking_wave_unit objects and have them carry out their attack.
  • wave_unit_attack_target_check: iterates over all attacking_wave_unit objects and checks if their targets still exist, and if not, turns them into idle_wave_unit.
  • wave_unit_hp_check: iterates over all wave_unit objects and checks if their HP is at 0 and should become dying_wave_unit.

When game state data is updated I call to the presentation layer so that it can do its thing. E.g. when a system applies damage to units, once the system is done updating, the presentation module’s on_take_damage(t, units) is called. It will then update animations, play audio etc.