Struggling to stack objects - Grabbing child node from another node

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Illyas_Onii

Hi there! I’m having issues stacking together puzzles pieces in this game I’m working on. The code in particular i’m struggling is here.
First line works just fine, in which when I have the piece held, it correctly finds the Position node and sets its own position to there.
The confusing part I’m asking for help with is trying to stack pieces, where each tile also has a Position2D, that I would expect to function the same way as when I pick up a tile.
the Issue comes in that I cannot figure out how to actually find or call the StackPosition node from the stackedBy variable, which just stores the tile object below it.

Thanks in advance~!

EDIT: Fixed! Signals always seem to trip me up and I flubbed a connect() function

  if held:
		self.position = get_node("../Player/Position2D").global_position
	if stacked && !held:
		self.position = stackedBy.get_node("../StackPosition").global_position
:bust_in_silhouette: Reply From: exuin

So the node hierarchy is this?

Tile
- StackPosition

Then the path should be stackedBy.get_node("StackPosition") since .. gets the parent of the node it’s called on.

Thank you for the answer, yes that is correct that is how the hierarchy is set up. I changed the line of code, yet still the position does not update in the same way the held tile does. I’ve double checked that the bool is set correctly when it lands on another and sure enough it does output correctly that it is being changed, however it still is not moving with the tile below it.

Illyas_Onii | 2021-04-16 18:15

Is that code in the process function?

exuin | 2021-04-16 18:17

Yes it is, there is a raycast that detects the object below it, and in the process it hopefully gets the global position of the StackPosition node, and applies it to itself when the bool conditions are met.

Illyas_Onii | 2021-04-16 18:24

Can you try printing out the position of the tiles? Or looking at them in the remote tab?

exuin | 2021-04-16 18:26

ok i did that, and it appears that the numbers are the same, and when moving to the right they remain the same instead of moving with the tile.
[1]

Illyas_Onii | 2021-04-16 18:31

So the StackPosition node doesn’t move with its parent node?

exuin | 2021-04-16 18:34

that seems to be the case, I wonder if its an order of operations issue? I’m still kinda new to godot so some of its quirks like this are a bit confusing coming from unity

Illyas_Onii | 2021-04-16 18:38

I don’t think so, child nodes should move when their parent nodes are moved unless they’ve been explicitly set to not inherit their parent’s transform. Maybe there’s something else going on?

exuin | 2021-04-16 18:40

ok so to double check, I’m fairly sure I got this right, but I might have gotten the connecting lines wrong, This right here should connect the object that collides with the raycast to the “stack” signal, and also emits it to the function stack together, which is what sets the bool and updates what the object is stacked with. Perhaps there is a better solution for this I haven’t found?

if raycast.is_colliding():
	var col = raycast.get_collider()
	#print("col hit ", col.name)
	if col != null && col.is_in_group("canStack"):
		connect("stack", col, "stackTogether",[], CONNECT_ONESHOT)
		self.emit_signal("stack", col)
	return false
return true

Illyas_Onii | 2021-04-16 18:44

Okay, so when you emit the signal, you pass along the node that you’re emitting the signal to, which seems redundant. Do you mean to pass self as the argument instead?

exuin | 2021-04-16 18:48

Oh wow! yes that was it thank you! I tell you the signal system is really confusing with godot, I always seem to mess those up in one way or another! I appreciate your help very much! :smiley:

Illyas_Onii | 2021-04-16 18:51

No problem!!

exuin | 2021-04-16 18:52