New instance gets added only once [Solved]

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

hello,
I try to make a 3d endless runner and i started with creating the floor and the generaton of the next floors. My approach is when i go through the area it creates a new floor and move the area with it. it works fine the first time I go through the area but on the next area so the moved one it doesnt work.

Here is the code i have:

func _on_Area_body_entered(body):
if body is KinematicBody:
	get_node("Area").translation += Vector3(0,0,-7)
	var newf = floorinstance.instance()
	newf.translation += Vector3(0,0,-5)
	get_tree().get_root().add_child(newf)

thank you for answers

edit: the area is changing its position but the floor generation doesnt work it only does it once.

:bust_in_silhouette: Reply From: jgodfrey

Just guessing, but since you never actually give the newly instanced floor a position, it probably starts at 0,0,0 each time (?) and translates from there, which I’d guess is correct on the first iteration, but probably not after that.

Instead of applying a translation to the newly instanced node, just set it’s position directly to whatever it should be…

:bust_in_silhouette: Reply From: pillow

Ok I managed to solve this problem. here is what I did maybe it will be helpfull for someone.

var Position = 0

func _on_Area_body_entered(body):
if body is KinematicBody:
    Position += -5
    get_node("Area").translation = Vector3(0,0,Position)
    var newf = floorinstance.instance()
    newf.translation = Vector3(0,0,Position)
    get_tree().get_root().add_child(newf)

As I suggested above, I still think you’re better off setting the instanced node’s position to an explicit value rather than translatingit from some position that you don’t directly control. Assuming a newly instanced node always gets position 0, 0, 0 by default, what you’re doing works, but that default position is really an implementation detail of Godot itself.

While it’s unlikely to happen, if some engine update would cause a newly instanced node to get position -10, -10, -10 (for example), your code will no longer work as intended.

Setting the explicit position for the instanced node avoids that potential issue all together…

jgodfrey | 2020-05-11 16:30