I have a vboxcontainer that scrolls across the screen using a tween. When it's finished, I'd like to reset it back to its original starting point. Other than using another tween, is there a simple way to move it back in script?
How do you set the position of a node in script?
I've always just stored the initial transform/position to a class variable in the _ready
function. Then when I need to reset, I just set the transform/position back to the stored class variable. This causes it to suddenly snap back into its starting position, which may be undesirable, but I find it works fairly well.
Thanks for the response. Could you show me the function you use to set the position? I’ve tried set_position(), set_pos(), node.position, position(node).... I cannot seem to get it to move.
Thanks.
Sure! Here's the type of code I use for resetting a node:
var starting_transform;
func _ready():
starting_transform = global_transform;
func _physics_process(delta):
if Input.is_key_pressed(KEY_R):
global_transform = starting_transform;
It should, in theory, work with both 3D and 2D nodes. The code above is from a 2D prototype project I made, and at least in the project it works as expected.
I get Parser Error: Identifier not found: global_transform in the _ready function.
Which version of Godot are you using? The project that the code comes from is using Godot 3.0.6.
Thinking about it, it could also be the node. The code snippet above comes from a KinematicBody2D. If you are using a Control node, then you'll need to replace global_transform
with rect_global_position
. The code should work with Spatial based nodes, since they have a global_transform
property.
I’m using 3.0.6. The node is a vboxcontainer. I found a workaround... If I remove the current instance then create a new instance, it resets the starting position. I will try your suggestion to use the rect_global_position as well. Thanks.