Strange addressing behavior (SOLVED)

This is my project dir tree

If it’s difficult to understand the structure , here is a summary:

Current map (grassland.collection) collection

And player collection

Here is my question: from controller#script on the player collection i cant access other game objects (character) using just ‘/character’ or ‘/character#sprite’. I need to refer to my collection inside the map collection, using ‘/player/character#sprite’ as if i was in the map collection.

If i try to use player:/character#sprite i receive an error message

It’s not insane, it’s actually a little bit simpler than you think it is. To start with:

Addressing has nothing to do with your file system!

The ONLY influence is: When you add something to a collection from a file, it’s default ID is the filename. But you can change that ID. Other than that, your filenames and folders have no influence on your running game.


You should re-read the part of the manual that talks about the URL format.

[socket:][path][#fragment]

Socket (the part before the colon):

…the game world of the target…You almost never need to specify the socket…

The socket can be one of two things: 1. Your bootstrap collection name (usually “main” or “default”), or 2. The name of the loaded proxy collection.

Again, the name of the collection is not the filename. Open the collection and in the Outline panel, click on the root item: called “Collection”. Then you will see it’s name in the properties panel.

Path

There’s a vital point you’re missing about the path: “/” makes the path absolute—a path from the socket collection. If you want it to be relative, leave off the socket: and the /.

Just use: “character” and “character#sprite”. – no /

Using: print(msg.url()) is very handy when you’re confused about addresses.

4 Likes

TL;DR: Whenever you have trouble, just read the manual again, it’s all in there:

Any address that starts with a ‘/’ will be resolved from the root of the game world. This corresponds to the root of the bootstrap collection that is loaded on game start.

4 Likes

There’s a vital point you’re missing about the path: “/” makes the path absolute —a path from the socket collection. If you want it to be relative, leave off the socket: and the /.

I really missed this point.

But i’m a bit confused yet, since I named the collection as ‘player’, I should be able to access using ‘player:/character#sprite’, no? Or should i use ‘player:character#sprite’?

About the structure of the source, its only to keep everything organized.

2 Likes

No, “player” is not the socket. You only use “:” if you need a different socket AKA “game world”. Every other collection you separate with a “/”. It’s just: “player/character#sprite”.

Think of it like the URL in your browser:
https://forum.defold.com/t/strange-addressing-behavior/65406/4
You only have a “:” after https, which you don’t even need to think about, everything else is separated by “/”.

1 Like

I think that i’ve understood now, the collection only receive a ‘socket’ to address when its loaded by a proxy, right? The player was added on the map through the editor.

Pretty much. They will always have a socket, but the socket won’t ever be different unless you load it with a proxy.

4 Likes

Thanks, you helped me a lot.

1 Like