Questions about id and url

I’m using code from this example:

Right now I’m doing this, and it works:

myobject = factory.create(component, pos)
local url = msg.url(nil, myobject, “sprite”)
goput.add(url, nil, handle_click)

I have a few questions and observations.

  1. What’s the difference between the hash id returned by factory.create() and the url retrieved by msg.url()? Do we really need both concepts?
  2. Why factory.create() returns the hash id and not the url of the object created ?
  3. Coming from an object oriented language background, I think it would be more intuitive to get an “instance” of an object with factory,create() and then access its “properties” with something like myobject.url, myobject.id etc. Am I completly missing the point, since Lua is not object oriented?
  4. If I type url. ctrl+space I would expect to see a popup with the fields of url (socket, path, fragment). Is this impossibile at editing time, or is/will it implemented in Editor 2.0 ?
1 Like

An URL is a resource identifier that points all the way down to a component instance. URL:s have three components:

socket: the name of the world the instance lives in. This is usually not necessary to specify, unless you load new worlds through collection proxies.

path: the id of the game object, hashed. This is what factory.create() gives you, i.e. hash("/instance01")

fragment: the id of the component, hashed, i.e. hash("#myscript")

If you have an id to a game object and want to reference a specific component in that object, you use msg.url(), just as in your example. You can read more about this here: http://www.defold.com/manuals/message-passing/

And you can definitely hit ctrl+space. Editor 2 does snippets as well as this:

2 Likes

In most OO languages an “instance” is a pointer to a piece of memory. In Defold you don’t get that but instead you get a name. For all static content, that name will be intact forever, even between app runs, meaning that you can store it, pass it on to another process and use it at a later point. Pointers do not allow you to do that.

1 Like

Thanks for the clarification about id and url, now it’s clear.

Regarding ctrl+space, I know that it works with functions, but I was asking about objects, for example:

local myurl = msg.url(nil, myobject, “sprite”)

If I type myurl. and hit crtl+space I would like to see suggested: socket, path, fragment.

Moreover, I don’t understand this feature you told me about: “For all static content, that name will be intact forever, even between app runs, meaning that you can store it, pass it on to another process and use it at a later point”.
Where can I find an example?

1 Like

Ah, no the editor does not infer the type of any variables. Neither do Editor 2 at the moment, but pehaps later on.

What I meant is that if you put a game object in main and call it “myobject”, that name is static. It won’t ever change. If you read some data saying “myobject” should get the message “be_invincible” at at game startup, and send the message, that will always work.

1 Like