Message passing between Monarch screens (SOLVED)

Hi,
I’m currently using @britzl’s Monarch extension to build a game and I have a question regarding how to communicate between spawned collections.

Steps:

  • main (collection): spawns “game” scene via a collection factory by calling monarch.show.
  • game (collection)'s url is main:/collection1/
  • game: spawns a dialog again via a collection factory by calling monarch.show.
  • item_dialog (collection)'s url is main:/collection2/

Now I need to send a message (buy_item) from the spawned dialog to the game collection. How can I do so?

I’m sure there’s a better way of handling this type of situation other than sending messages to collectionN but I can’t figure out what it should be.

Edit: To clarify, the dialog’s collectionfactory and screen_factory are in the “game” collection.

You would communicate between them like you normally would.

Store urls

You can store the urls in a module to (re)use later with something like urls.lua, sending it to one listener.

You can also use something like Britlz’s signal or broadcast

Message to direct address

Call a msg.post() directly to collectionN (you can probably name your collection better to see what they contain).

Pass the address to the popup

When you do monarch.show("item_dialog") you can pass along some data monarch.show("item_dialog", {}, {sender=msg.url()}) you can then grab the data and the sender in your item_dialog.

Personally I use something like urls.lua

3 Likes

Thanks @Jerakin for the suggestions.
I especially love your urls.lua module! It’s simple but powerful for this type of situations. I think I’ll go with it.

(Btw, I already tried to change the collectionN name but apparently it isn’t controllable by me. It appears to be generated by Monarch or Defold automatically.)

Edit: typo

1 Like

It is generated by Defold.

1 Like

This thread got me thinking that maybe it would be useful to have a monarch.send(id) function or similar that would do a msg.post() to a URL that can be specified on the screen_proxy/factory.script much in the same way as you can specify a Transition and Focus URL.

2 Likes

That would be a very useful addition to the extension.

Preferable it shouldn’t require an additional field for the user.
If monarch.show("buy_item") show that popup then monarch.send("buy_item", "message_id") should send a message to it.

monarch.show(“buy_item”) will load and enable a collection associated with the key “buy_item”. Monarch knows nothing about the contents of the loaded collection. This is why there is a need to specify a Transition Url and Focus Url.

We could maybe agree upon a standard naming format, saying that for it to work there must be a gameobject with id monarch inside of the collection.

1 Like

Oh right of course, I tend to name them the same way. But I guess that it isn’t enforced so we can’t assume people will do that. :thinking: