[workaround] Issues with factory and unknown game object

i’m using a factory to spawn in projectiles, and i’m getting a very confusing issue where the scripts in the newly created game object all seem to be pointing to the same game object.
i tried debugging it a bit, but that creates even more questions. on the factory side, everything seems to be going smoothly, with the created game objects being what was expected, such as hash: [/instance11].
though if i go into the script attached to the game object that was created and print the ID of the game object, i get hash: [14062770599921625650 (unknown)]. this hashed id is the same for every single factory-created object

i really don’t know what’s going on here, any help would be good.

for further clarification if needed, here’s the parts of the code where stuff is being printed

the code for the factory making the projectile

local proj=factory.create("/handle#makeprojectile")
print(proj)
...
msg.post(proj,"defs",  newspelldat  )

and the code inside the projectile game object that shows the weird ID:

function on_message(self, message_id, message, sender)
	print(go.get_id("."))
	if message_id==hash"defs"  then
	...

Does it matter if you use go.get_id(".") or go.get_id()? It shouldn’t, but might as well check.

that does change the output to the expected instance hash
i put this to print in the update function of the script, and it appears that there are, in fact, separate instances of game objects.
but there is still some really weird unexpected behavior coming from the actual objects. for example, the msg.post call doesn’t always seem to be going to the correct object, but rather objects that have already been created by the factory?

i’ve decided instead of using a factory to make GOs with controler scripts, i will just implement the code in a for loop in the main script instead.
that works.

print(go.get_id(".")) in any game-object results in the invalid result hash: [14062770599921625650 (unknown)]
print(go.get_id()) result in a valid result.

The incorrect usage is given in Addressing in Defold
So it would be good to change that.

It’s not really an invalid hash, but for some reason we do not do a reverse lookup from the hash back to a human readable string in this case. Not sure why this is the case @JCash ?

If you make a release build and show the result from go.get_id() and go.get_id(".") in a label or gui text node they should show the same value.

The reverse lookup is automatic in debug builds. The question is: why wasn’t the string put in the (global) table?

If only one of the strings are shown, then it should follow that the other hash isn’t the same. I.e. go.get_id(".") ~= go.get_id()

I tried a release bundle, this also results in the error.
I will try printing in a label tonight, late for work now.
I am running on Ubuntu 22, using the side scroller tutorial. The error is consistent which is good:-) It looks like an invalid memory reference, maybe the Linux compiler has not initialised a variable, but could be anything really.

A release bundle will never do reverse lookups for any hashes (ie go from hash/number back to string). How did you even show the hash in a release build if you didn’t show it in a label/text node?

I doubt this is the case. What makes you think that is the case?

Looking at the code, we return either the absolute id, or the local id. They are naturally different.

I think the main concern here is expectations, since the documentation says:
“Returns or constructs an instance identifier. The instance id is a hash of the absolute path to the instance.”

So, we need to discuss internally how to deal with this issue (i.e change the function or the documentation)

what is a local id?
forgive me for not knowing about the inner workings of the engine, but i would expect that a game object just has a single id that is the same everywhere.

I used the id as a table reference. When I used go.get_id() the bundled-build worked as intended, when I used go.get_id(".") it did not. Both mirrored what occurred in the editor. In the editor:
print( go.get_id(".")) → hash: [14062770599921625650 (unknown)]
print( go.get_id()) → hash: [/instance1]
I need to work out how to add a label/text node.
I see now that ‘Invalid mem ref’ is an unfounded wild speculation on my part, it was based on the result from print being a decimal representation of a 64bit number, also all game objects give this same value. Whereas go.get_id() gives hash: [/instance1],hash: [/instance2],…

It is an internal detail. where a game object has an instance id (go.get_id())
The absolute id includes the path of its parents as well.

Yeah, the “[/instance]” is just the human readable version in debug mode. It always is the 64bit hash value behind the scenes.

Regardless, I think for now, your best bet is to use the go.get_id() as a key, since it seems to match the one from the factory.create().

Thanks, will do. I am currently trying to learn and understand, however I don’t want to waste your time asking to many questions.

1 Like