[SOLVED] Game Object is not following parent transform

Hi,

I’m following the Magic Ink tutorial on the Defold website. However, I’ve made it to the point where the magic effects are spawned and parented to the block object, and I’ve run into a problem. Checking the effect’s parent immediately after parenting them gives a nil value, which means the following code also doesn’t work as intended.

Here’s my code:

self.fx1 = factory.create("#fxfactory", nil, nil, { direction = hash("left") })
self.fx2 = factory.create("#fxfactory", nil, nil, { direction = hash("right") })

msg.post(self.fx1, "set_parent", { parent_id = go.get_id() })
msg.post(self.fx2, "set_parent", { parent_id = go.get_id() })

print(go.get_parent(self.fx1)) -- Returns nil

go.set(self.fx1, "position.z", 0.01)
go.set(self.fx1, "scale", 1)
go.set(self.fx2, "position.z", 0.02)
go.set(self.fx1, "scale", 1)

Messages aren’t processed until the end of the current frame so getting the parent won’t work immediately (see the update loop manual). It shouldn’t be causing problems with the rest of the code - what’s the exact issue you’re running into?

2 Likes

Setting the position and scale of the game objects works as if they’re not parented to anything, instead of being parented to the block.

Ah this is actually interesting.

It turns out that the go.set_parent() method has keep_world_transform default to false.
But the set_parent message has keep_world_transform default to true :smiley:

So in your case, because you are using the message and not the method, you are are instructing it to keep the same world position after it gets assigned to the new parent.

7 Likes

Thanks for the response! It’s interesting that they have different defaults. When I run the game with keep_world_transform at false, this happens:


The magic effects are offset from their parents and one of them has a much smaller scale.

However, if I reset the positions manually, the magic works as intended.

Here’s the code:

self.fx1 = factory.create("#fxfactory", nil, nil, { direction = hash("left") })
self.fx2 = factory.create("#fxfactory", nil, nil, { direction = hash("right") })

msg.post(self.fx1, "set_parent", { parent_id = go.get_id(), keep_world_transform = 0 })
msg.post(self.fx2, "set_parent", { parent_id = go.get_id(), keep_world_transform = 0 })

go.set(self.fx1, "position", vmath.vector3(0, 0, 0.01))
go.set(self.fx1, "scale", 1)
go.set(self.fx2, "position", vmath.vector3(0, 0, 0.02))
go.set(self.fx2, "scale", 1)