Message passing between GUI's

I’ve been making a game where it includes level progression (beat level 1 in order to gain access to level 2) I used messaging passing to achieve this but the collection level 1 sends to the level viewer to tell that the player has completed the level doesn’t seem to send, I imagine this is due to the level viewer collection being unloaded. Anyone can help me find a work around on this?

Yes, when a collection is unloaded you may lose messages (i think).

Could you solve this by first posting the message and then delay the unload using a timer?

I am really new to Defold and have no idea how to use timers, I have seen the API reference for it but still do not understand. Can you help me further on this?

I don’t think I explained this very well. When I complete level 1 it will mean to send a message to the level viewer which is a different collection a message to enable a GUI node to enter into level 2, my problem is that the level viewer collection is unloaded so I am guessing it doesn’t receive any messages which makes me unable to use the messaging passing to my ability

Yes, if you unload a collection then it doesn’t exist, so you can’t send messages to it. You need to have something that persists between levels. This could be as simple as one global variable that stores the current level number—the completed level could set it to the next number, and the level viewer could read this when it loads again. Or you could have something in your loader collection that the level sends a message to when it’s been completed, which in turn notifies the level viewer (when it is reloaded).

There’s an example of a central loader collection here:

The global variable doesn’t seem to persist when unloading and loading collections in my case. The value is incremented but when I unload and load different collections and then print the value in any collection the value is still 1, and the loader collection thing you mentioned I don’t think it will work either as it is unloaded and messages don’t get received

Try storing it in a variable inside of lua module.

1 Like

Thank you so much, this worked perfectly another great tool in mind

1 Like

In your game.project file, in the “Script” section, make sure you have “Shared State” enabled. This means your gui scripts and game-object scripts will share the same globals. If that’s not the problem, then please post the code so we can see. A global variable shouldn’t be affected by collection loading/unloading at all.

Hmm, I think we need to back up a bit. Did you look at the example that britzl posted? Is that how you have your project organized? Are you using collection proxies to load and unload things?

You must have a bootstrap collection that is always loaded (as far as I know you can’t unload the bootstrap collection).

1 Like

I am using collection proxies to unload and load collections, and I think I typed that badly I meant to say that the loader collection can’t send messages to unloaded collections. I am very new to this game development thing so I am most likely just doing something really dumb but someone mentioned using a Lua module which worked perfectly

No problem, we all started there. Here’s what I meant:

Let’s say you have a “Level Manager” object in your bootstrap collection - so it persists between levels. It has a script that controls loading and unloading each level and the Level Viewer.

  1. Level 1 collection - when the level is complete, sends a message to the Level Manager, saying it has been completed successfully.

  2. Level Manager unloads Level 1.

  3. Level Manager loads Level Viewer.

  4. When Level Viewer is done loading, Level Manager sends it a message telling it which level is next.

In other words: you can just wait until a collection is loaded, and then send it messages. It means, in your Level Viewer, you may have to move some code from init() to on_message() (where it gets the message about which level is selected).


This is just one method of many. Using a Lua module is probably easier, you don’t have to delay anything or send messages back and forth.

It can be useful in some situations to load a collection and then send it setup information though.

4 Likes