Go.set_parent can only access instances within the same collection

Ooh, neat, contextual menu in the editor. Even the console ! Thanks for that.

Anyway…

		galaxie.orbitals.id[self.Etoile_nb] = collectionfactory.create("/Galaxy#Orbital", nil, nil, props , nil)
		go.set_parent(tostring(galaxie.orbitals.id[self.Etoile_nb]), go.get_id(), true)

got :

go.set_parent can only access instances within the same collection.

tostring(galaxie.orbitals.id[self.Etoile_nb]) is the child object, created just one line before.

The object parent and the object child are from two different collection, each instancied by a different collection factory (put in the same up-level object).

EDIT : Yes, the objects are created and working (I test with “print” in it).

How are those two lines called? In a callback? Directly in one of the lifecycle functions?

Using tostring() there seems wrong. What’s in galaxie.orbitals.id[self.Etoile_nb]?

— To Potota

in galaxie.orbitals.id[self.Etoile_nb], it’s the id of the instance I want to put as child of the object where this script is (this ID is given by the line just before.

I tried hash, but I have “string expected, got table” as answer.
I tried the value alone, and I have “url expected, got table”
I tried tostring, and I have “go.set_parent can only access instances within the same collection”

---- To Britzl

They are in the Init function.

  • Main collection has a few objects, like Camera.go, curseur.go and galaxie.go
    All’s good.

  • Galaxie.go has three component : galaxie.script and two collectionfactory
    All’s good

  • In galaxie.go, there is a call (in INIT) for one collectionfactory, which create an instance of another collection, StarSystem.collection

  • works like a charm

galaxie.etoiles.id[i] = collectionfactory.create("#StarSystem", nil, nil, props , nil)
  • StarSystem.collection has one Object, StarSystem.go with a child object (for the label) put as child directly in the Tab outline (it’s not in the StarSystem.go itself, just added as a child in the collection).

  • No problem here.

  • StarSystem.go has components like collisionobject, two sprites and a script

  • Works well

  • In the StarSystem.script, I have a call (in INIT) for the second collectionfactory, Orbital.collection, with one object, Orbital.go, with components like sprite, collision, script.

galaxie.orbitals.id[self.Etoile_nb] = collectionfactory.create("/Galaxy#Orbital", nil, nil, props , nil)

Well, it’s working apparently, since some print() in Orbital.script answers.

At the end, I have two instance “groups” : One of StarSystems, and one of Orbitals.

But when, in StarSystem.script, the line AFTER the call for collectionfactory on Orbital :

		props[hash("/Orbital")] = {etoile = self.Etoile_nb, orbite = i}
		galaxie.orbitals.id[self.Etoile_nb] = collectionfactory.create("/Galaxy#Orbital", nil, nil, props , nil)

I have :

go.set_parent can only access instances within the same collection.

pprint(galaxie.orbitals.id[self.Etoile_nb]) (the ID of the instance just created) give me :

{ --[[0x7f297821eb70]]
  hash: [/Orbital] = hash: [/collection1/Orbital]
}```

Basically, the goal is to have stars (StarSystem) with planets (Orbital) rotating around them

I don’t know why this is happening. It seems like you are mixing up objects between different collections. Do you have more than one collection loaded at the same time? Can you reduce this to a minimal project where the problem can be seen?

Oh, I didn’t pay close enough attention here. collectionfactory.create() returns a table that lists the IDs of all the newly-spawned objects. You need to pick out one of those IDs to use for go.set_parent(), not just give it the entire table.

So that would be:

go.set_parent(galaxie.orbitals.id[self.Etoile_nb][hash("/Orbital")], go.get_id(), true)
1 Like

Oh ! Stupid me ! I completely not though of that !

Thank you, I can continue my Conquest of the Stars tonight ! :smiley:

— Britzl

Yes, I have a three collections at the same time : Main (the game), and two created by collection factories, the call of the second one being in the instances created by the first.
Again, basically, Main is the Galaxy, in which a first collection instancing stars is created, and in each instance of stars a collection instancing planets is created. Now, thinking of moons…

Reminder that converting hashes to strings is strictly a debug feature.
Do not rely on such functionality in release builds!

3 Likes

Okay, I will keep that in mind.