Topic was automatically imported from the old Question2Answer platform.
Asked By
rustyStriker
Old Version
Published before Godot 3 was released.
I got a car that i want to place in a 2d race, and i want to spawn if via code(so i could scramble game modes and maps) and as the title say how can i spawn it in a specific x and y(lets say 300,230)
var car_resource = preload("car.tscn")
var car = car_resource.instance()
car.set_pos(custom_position) # use set_translation() if you are in 3D
parent.add_child(car) # parent could be whatever node in the scene that you want the car to be child of
As a suggestion, I would not spawn cars in hardcoded positions. Instead, in every map I would place spawn points (Spatial or Node2D nodes) to know where to position the cars.
do i need to make the instance first or i can set the position whenever i want to?
rustyStriker | 2016-09-16 12:38
You have to make an instance first.
atze | 2016-09-16 12:55
You can set the position on the instance only because car_resource is a resource, not a node. You can set the position before or after add_child, but I prefer before because the node’s _enter_tree logic will be able to use it, in case it needs to.