Relative position become absolute when changed by go.set

Hi,
I have, then, that collection with objects.
I create instances of that collection with a Collection Factory.
I set the position of the Objects in the Collection by moving them visually in the “collection” tab. The move is reflect in the outline : If I move an Object 32 px downwards, I see -32 in the Position.Y input box.
All is good.

But when I want to change the Object by using go.set, like that :

go.set(tmp_Label, "position.y", -32*(math.pow(zoom_gap, -zoom_state)))

The Object is put in absolute position (I tried with positive numbers in place of -32, of course)
tmp_label is the instancied ID.

What I want to do :
I want to change the position of collectionX/my_Object relatively of the Collection position.

What kind of value do you get when you do a go.get() for the same property?

I did that :

				pprint(go.get(tmp_Label, "position.y"))
				go.set(tmp_Label, "position.y", -32*(math.pow(zoom_gap, -zoom_state)))
				pprint(go.get(tmp_Label, "position.y"))

The first pprint give me the screen position, the second the result from “-32*something”.

Can be of use :
The Label is part of that collection :

Collection
* Game Object
** Collisionobject
*** Shape
** Script
** Sprite
** Sprite
* Game Object 
** Label (the one and only)

The Collection is instancied (up to an hundred). The collection position is fixed in collectionfactory.create position, and doesn’t change after that.

Edit : What I’m targeting is the second Game Object, not the component Label.

That’s weird, if go.set “scale.y” or “scale.x” are understood, “scale.z” has no effect. At all.

Edit : Tried to change directly the label element : No change.

go.set(tmp_Label, “position.y”, -32*…

The Object is put in absolute position

The “position” property is expressed in local coordinates, that is local with respect to the current parent. What is the parent of your object?

If you wish to move it in relation to the current position, you can use:

go.set(url,  "position.y", go.get(url, "position.y") + 32)

Parent is the Collection, wich is instancied by a collection factory.
That’s that collection that have absolute coordinates.

Everything in it is supposed to have coordinates relative to their Collection. Here, the GameObject with the label is at -32 Y at creation, and the Label in it is at 0.0 Y.
Meaning the label is supposed to be centered on its parent GameObject, and the GameObject is supposed to be 32 pixels below the center of the Collection.

It’s called by using the instance URL (like hash: [/collection0/Label]) Label being the name of the Object having the relative coordinates “position.y = -32” at creation (the “Label” in it having 0.0 coordinates).
The script try to change that coordinate with a go.set.
It’s working, as in “position.y is changed”, but this position is Absolute (as in World coordinate - not in Screen as I though).
I wanted the position be changed, but keeping it in relative position. AKA I don’t wand my GameObject with the label in it put 32 pixels under the bottom of the Wold, but just 32 pixels below the center of its parent, Collection.

Can you please share a minimal project where we can test this. I don’t quite understand how it is configured.

Then the game object does not have a parent.
The collection itself is only a “container”, and does not act as a parent.

You can sove that by putting a top game object as a root in your collection, and when spawning using the collection factory, you supply a position to the root object.
Then, moving any children of the root object, will be in the space of their parent (the root object).

I hope that makes sense.

You mean I don’t give a position in the collectionfactory.create() function itself, but pass it by the properties ?

The factory components don’t have a position.

I don’t understand

pos = vmath.vector3(x1, y1, 1)
local idf = collectionfactory.create("#factory_etoile", pos, nil, props, 1)

And it’s working. I’m pretty sure of it, since I have it under my eyes.

the API reference page tell me also: for https://defold.com/ref/collectionfactory/#collectionfactory.create:url-[position]-[rotation]-[properties]-[scale]

[position]	vector3	position to assign to the newly spawned collection

Still, it’s a misconception that there is a collection with a position.
The collection factory takes that position you give it, and place all game objects relative to that position.

But that’s just the initial placement. The game objects don’t have a “collection” object as a parent. So, when you move them, you do so in relation to the world position.

As I suggested, if you in your collection add a “root” game object, and place all game objects as children to it, then they’ll have the root as a parent, and then you can move them in relation to the “root” game object.

1 Like

Okay, I will do as you say. but I Don’t see the need for Collections, then. Why not just Factory the “root” object if you can’t do anything with a "Collection.
I’m reading the manual (about beans), and honestly what the use of Collections ?

Mmh…
The “factory_etoile” (a collection factory) calling on the prototype “EtoileCarte.collection” with this hierachy :

*EtoileCarte.collection
** EtoileCarte.go
***Etoile.go
***Label

By this code :

		local idf = collectionfactory.create("#factory_etoile", pos, nil, props, 1)
		pprint (idf)

Give me things like that :

{ --[[0x7f4319166cf0]]
  hash: [/Label] = hash: [/collection99/Label],
  hash: [/EtoileCarte] = hash: [/collection99/EtoileCarte],
  hash: [/Etoile] = hash: [/collection99/Etoile]
}

Meaning, no hierarchy. I’m not sure I understand correctly. Is it normal ?

If you wish to spawn a collection (doh) of game objects in a certain configuration and hierarchy you use a collection.

If there was no concept of collections you would need to spawn each game object individually and then parent and configure them as needed.

You understand correctly!

The collection is just a “container”, a means to transport the data from the editor to the runtime.

Okay, the position works in relative, now. Thank you.

But what is the difference between puting objects in a Collection rather in an another Object ? It’s not really clear.

The collection does not exist at runtime. The game objects inside the collection are the only objects that exist in your game, nothing else. Compare these two:

Screenshot 2024-10-09 at 19.46.58

When you spawn the above collection using a collection factory you get three game objects “go1”, “go2” and “go3”. They have no relationship and exist at the same “level” of the scene hierarchy.

Screenshot 2024-10-09 at 19.47.21

When you spawn this collection you have four game objects “parent”, “go1”, “go2” and go3. The three game objects “go1”, “go2” and “go3” will all have “parent” as their parent object and move relative to it.

1 Like

Thanks for your explanation.
But Does that mean you can’t put hierarchy out of collection ?
Can’t I just put a GO in another GO, and just call the parent, or does the parent HAVE to be in a collection for the hirarchy to work properly ?