Question About Collection Proxies

Hi, all! I’ve been reading the documentation regarding collection proxies and had a quick question: so if you have a game that features, for example, a title screen collection, several level collections, an end title collection, etc.-- is the general idea that you have a ‘master’ collection (like the ‘loader’ collection in the documentation example) that stays loaded and orchestrates the loading and unloading of all the game’s collections?

Thanks!
Bryan

1 Like

Yes, have a collection with your loader script along with the collection proxies containing your scenes. When buttons are pressed to switch to another scene, you’ll send a message to your loader which will switch it. IE;

if message_id == hash("show_game") then --Person clicks play button on your menu and sends a message to loader
    		msg.post("#level1", "load") -- Level 1 is loaded
    		msg.post("#menuproxy", "unload")--	main menu is unloaded
1 Like

Great, thanks for that!

Bryan

1 Like

Also: I always have such a hard time figuring out what object url’s I should use for messaging; I have a top-level collection called ‘main’ which contains a game object (loader) which has two collection proxies (title_screen and main_game). The ‘main_game’ collection, in turn, has a game object called ‘controller’ I’m trying to address. What URL do I use for the controller object when trying to send it a message from my ‘loader’ object???

Thanks,
Bryan

Hm. Not sure I fully understand what you mean. inside your main collection which has your collection proxies, the controller should be the loader. You could name the game object “controller” and then the script “controller”. Something in your levels sends a message to your controller (loader) script.

msg.post("controller:/controller", "show_game") -- This is sent to your loader and your loader reacts to this by loading the scene and unloading the current one

What I have is this:

main.collection
  loader.go
    main_gameProxy --> proxy for 'main_game.collection'
    title_screenProxy
    loader.script

Now-- inside my ‘main_game’ collection (main_game.collection) I have a game object (controller.go) which has as script attached to it (controller.script):

main_game.collection
  controller.go
    controller.script

I’m trying to send a message from loader.script to controller.script…

Bryan

If you open main_game.collection, it will have a “name” field in the properties view. It’s possible you called it “main_game”, but this name is allowed to be different than the file name so check to make sure. I’m assuming you are going to call it “main_game”. This name is used when the collection is loaded via a proxy to define the “socket” of the new collection (which in practice is the message queue). The socket comes first in the url, and is only required when you are communicating across collections loaded via proxies. You are essentially communicating across “worlds” as far as the engine is concerned, that’s why you need to say which “world” you are communicating with.

Depending on how you name the things in your main_game.collection and controller.go, the resulting url could be:
main_game:/controller#controller
or:
main_game:/controller#script

Read more how it works in the Collection proxy manual, for example under “Naming collections”.

By the way, in your two examples you should consider making loader.go and controller.go embedded game objects to get rid of some of the files. This is brushed over in the building blocks manual, but essentially means that you right-click the collection and choose “add game object” rather than “add game object from file”. It should be easier to manage the structures, game object files are only required when you want to reuse them in many places.

You’re exactly right on how I named things; but using 'main_game:/controller#controller doesn’t seem to be working for me.

I’m sending this message from my ‘loader.script’:

msg.post("main_game:/controller#controller", "hello_dude")

And in main_game:/controller#controller.script I have this debug line in ‘function message(self, message_id, message, sender)’:

print(message_id)

…and nothing is being printed to the console. On the plus side, using the above URL isn’t throwing an error, so I must be targeting SOMETHING that exists. :slight_smile:

I know it’s got to be some stupid noob mistake…

[btw, thanks for the tip about ‘embedded game objects’.]

Bryan

1 Like

Alright, I can see two reasons why it wouldn’t work.

  1. Has the collection been properly loaded and initialised before you post the message? You need to wait for the “proxy_loaded” message and then post “enable” back to the proxy (see this). Immediately after you posted “enable”, you can communicate with it.
  2. Is the URL actually correct? You can check by adding the following to the init-function in controller.script:
print(msg.url()) -- empty constructor resolves the URL into where we are calling from, i.e. this script

Yes, the collection is loaded and enabled; I’m sending the message while the collection is visible on screen (it’s the game level that loads up after the title screen). Still not sure what’s going on yet, but I don’t really care-- you’ve given me the best piece of debugging advice ever (print(msg.url())!

Now I’ve got the tool I need to figure out exactly what url I need to use to properly address objects, so thanks for that… :slight_smile:

1 Like

Print debugging actually works incredibly well in Defold and should be used a lot, in combination with hot-reload. Don’t be afraid to add an on_reload-function now and then, to reset the script state or do whatever.

hi everyone :slight_smile: . is there a simple example for the use of collection proxies?
such as “main menu => scene1 => scene2 =>main menu”

thanks in advance

This example https://github.com/britzl/publicexamples/tree/master/examples/menu_and_game shows how to transition between a menu and a game and back again. There’s a controller script that takes care of loading and unloading. It should be trivial to add additional scenes and transitions like you’re asking for. Let me know if you need additional help.

So, collection proxies are used to load/unload collection files. is that all? :neutral_face:

Yes. There’s a whole manual page describing all you need to know about collections and proxies.