Set_parent don't work with GameObject is created by Factory (DEF-3256)

Continuing the discussion from Deleting/Hiding a weapon:

I’m using the following code:

local gun_go = msg.url("gun")
local hand_go = spine.get_go("player#spinemodel", "right_hand")
msg.post(gun_go, "set_parent", {parent_id = hand_go})

if add the collection of Player of manually to stage(main collection), this works fine, the gun_go gameobject is attached to bone.

but if add the collection of Player via Factory - something is going weird, gun_go gameobject is not displayed.

P.S. for Defold team I’ve updated the project with name “Spine_v1.2.120” in the dashboard.

1 Like

It could be several things here.
gun_go is probably created (and can be retrieved as your first line says).
I assume you dont get any errors so the set_parent is working.

What you could look for is:
Scale: Whenever you are creating through a factory, it inherits a scale of the gameobject from where the creating message is sent.
Position: When creating through factory you might have a different position than if put in collection from start. As you are keeping world transform (by not passing any value in the message) it might wind up in a different position.

Have you printed out gun_go and hang_go? Have you tried to print out their positions before parenting and compare to the first when collection already in main collection?

1 Like

hi @andreas.strangequest
thats my debug log:

pprint(gun_go)						--url: [game:/collection0/gun_attach]  	| 	url: [game:/dodik/gun_attach]
pprint(go.get_scale(gun_go))		--vmath.vector3(1, 1, 1)				|	vmath.vector3(1, 1, 1)
pprint(go.get_position(gun_go))		--vmath.vector3(49, 55, 1)				|	vmath.vector3(49, 55, 1)
pprint(hand_go)						--hash: [/instance60]					|	hash: [/instance26]
pprint(go.get_scale(hand_go))		--vmath.vector3(1, 1, 1)				|	vmath.vector3(1, 1, 1)
pprint(go.get_position(hand_go))	--vmath.vector3(13, 0, 0)				|	vmath.vector3(13, 0, 0)

as you see there are same positions and scales, just not displayed when do it in factory’s created collection.
let me know your email, I will add you to this project if you want to see sources.

I’d love to take a look at it: andreas@strangequest.com

2 Likes

I got message that looks like you doesn’t have an account on Defold yet :slight_smile:

aah :stuck_out_tongue: it’s andreas.strangequest@gmail.com (as our main mail is not gmail connected)

added, project name “Spine_v1.2.120”

Yes, I’ve checked it and I cannot really see anything other than it is a bug. I changed vars so they were self.somevars instead and tried to make sure it was waterproof.
As transformation is not immediately changing after set_parent (as it is a message) I check the transf in the first update call.

function update(self, dt)
	if not self.first then
		self.first = true
		print(go.get_id())
		print(go.get_scale(self.gun_go),go.get_position(self.gun_go),go.get_rotation(self.gun_go))
		print(go.get_world_scale(self.gun_go),go.get_world_position(self.gun_go),go.get_world_rotation(self.gun_go))
	end
end

And it prints:

DEBUG:SCRIPT: hash: [/dodik/root]
DEBUG:SCRIPT: vmath.vector3(1.0000001192093, 1.0000001192093, 1)	vmath.vector3(45.899993896484, 23.800003051758, 1)	vmath.quat(0, 0, 0, 1)
DEBUG:SCRIPT: vmath.vector3(1, 1, 1)	vmath.vector3(496, 257, 1)	vmath.quat(0, 0, 0, 1)
DEBUG:SCRIPT: hash: [/collection0/root]
DEBUG:SCRIPT: vmath.vector3(nan, nan, nan)	vmath.vector3(nan, nan, nan)	vmath.quat(nan, nan, nan, nan)
DEBUG:SCRIPT: vmath.vector3(nan, nan, nan)	vmath.vector3(nan, nan, nan)	vmath.quat(nan, nan, nan, nan)

Weird as I do similar things in our project but not with Spine bones though. Works fine when using keep_world_transform = 0.
I would file it as bug

2 Likes

If you have a minimal repo case that we could attach to the bug that would be awesome.

let me know your email, I will add you too. I’ve added already @sven @britzl @Mathias_Westerdahl to this project (“Spine_v1.2.120”)

Ok, all those guys on the project should be enough. :slight_smile:

2 Likes

Confirmed. Created issue: DEF-3256

STR:
A factory created collection contains two game objects A and B
Game object A has a spine model and a script
Game object B has a sprite
Game object B is set as a child of A in the init() function
B is no longer visible and printing go.set_position() results in vmath.vector3(nan, nan, nan)

Delaying the set_parent operation until after init() works.

2 Likes

Is this bug still open?
If I set_parent in init() after the create , go.set_position fail. But if i set go.set_position in on_input, it works.
Is there a workaround for this?

card_ids = collectionfactory.create("/factories#card_factory", s )
msg.post(card_ids[hash("/cards")], "set_parent", {parent_id =  hash("/temp_cards") })
s = vmath.vector3(0,-200,0)
go.set_position(s, hash("/temp_cards"))

as I know, bug still no fixed. as workaround you can write it via message like:

function init(self)
	msg.post(".", "setPosition")
end

function on_message(self, message_id, message, sender)
	if message_id == hash("setPosition") then
		--go.set_position
	end
end
1 Like

Thank you @BunBunBun.