Scene position

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

I have created a new Scene and want to instance it at my main scene
In my main scene i added the following script

for i in 10:
	var node = load("res://GameNode.tscn").instance()
	node.position = Vector2(100 * i,100)
	add_child(node)
pass

For some reason node.position is not workinf for me. Is there a way to acces variables in another scene during the instance?

is there any code or AnimationPlayer for setting position inside GameNode.tscn?

volzhs | 2018-04-24 18:15

My GameNode.tscn only has a Node2D node with a _draw function.

Julian11029 | 2018-04-24 18:37

:bust_in_silhouette: Reply From: Enquest

you are prob. setting a Rigidbody2d when you instancing in you _process(). That won’t work whenever you instance it there it will shoot back in the original position of that scene. I puzzled on this for a while.

func _ready():
	set_position(get_node('../some/node').get_global_transform().origin)

So when I instance this I get the position in the node where I want to launch the body. On the screen a placeholder is shown of the body.
When the instance is added the placeholder is hidden and the body is launched at the correct position. I didn’t find any other solution.

To be more clear:
The problem that I had is that a object was moving a Rigidbody2D in place to launch. However the rigidbody2d was activated it jumped back to the scene position. So I replaced the Rigidbody with a sprite to follow the object and when launced deleted sprite and instantated the Rigidbody2d

I found a way to set the position by using

var Scene_1 = load(“res://GameNode.tscn”)

func _ready():
	for i in 1:
		var node = Scene_1.instance();
		node.position.x = 100
		node.position.y = 100
               add_child(node)
   pass

This works!

But i also want to pass a variable to the scene called “color” (int)
node.color = 2 doesn’t seem to work…

my GameNode has the following code

var color
 func _ready():
   print(global_position.x)

Julian11029 | 2018-04-26 07:19