Why does "position" not update after move_and_collide()?

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

Potentially noob question, but I am a Godot noob and cannot find what I’m missing.

I have two scenes. A Parent Scene, which can create multiple instances of a Child scene.
Parent
|
----> Child

The Parent Scene can issue “move” commands to the child scenes. This causes the Child scenes to move_and_collide() with a “StaticBody2D” node on the parent scene. The Child scene emits a signal once it has collided with a wall and stopped.

My problem comes from the following code snippets:

Parent.gd

print("childs initial position: " + str(childToMove.position)) 
childToMove.move_direction(direction) 
yield(childToMove, "child_done_moving") 
print("childs final position: " + str(childToMove.position))

The output is as follows:
childs initial position: (192, 192)
childs tiles final position: (192, 192)

I see the child scene moving when I play it. It very clearly doesn’t have the same position.
I’ve checked the global_position, and it’s the same story. What am I missing?

The following is the child code that I think is illuminating.
Child.gd

signal child_done_moving
var velocity : Vector2 

func _physics_process(delta):
     var collide = $KinematicBody2D.move_and_collide(velocity * delta)

     if(collide):
          velocity = Vector2(0, 0)
          if(collide.collider.is_in_group("parent_walls")):
               emit_signal("child_done_moving")

The following is the Node structure of the scenes:

Parent.tscn
Node2D
|—> TileMap
|—>StaticBody2D
|—>CollisionPolygon2D
|—>CollisionPolygon2D

Child.tscn
Node2D
|—>KinematicBody2D
|—> CollisionPolygon2D
|—>Polygon2D

:bust_in_silhouette: Reply From: njamster

You’re moving the KinematicBody2D, but the child-node itself stays in place. Try this:

childToMove.get_node("KinematicBody2D").position

I’d recommend simply making the KinematicBody2D the root-node of your child-scene.

Thank you so much that worked! It makes so much sense, geez, can’t believe I missed it.

pmonson11 | 2020-02-11 12:47