Instantiate node at a particular position

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

I intend to spawn collectible coins.They will be the child of a Node2D “Coin_container”.
If 5 coins are spawned I want each of them to appear after an offset from the previous.
Parent of coin scene is Node2D. After instancing a coin , i tried to give it an offset using set_pos() but its giving error-“Nonexistent function set_pos() in base Node2D”.

extends Node


onready var coin_scene = preload("res://Coin.tscn")
onready var coin_container = get_node("Coin_container2")
var coins_collected = 0
var next_pos = 0

func _ready():
  var first_coin = coin_container.get_child(0)
  var pos = (coin_container.get_relative_transform_to_parent(get_tree().get_root().get_child(0)))
   print(pos.x.x+10)
   spawn_coins(3)
   set_process(true)

func spawn_coins(num):
   for i in range(num):
	  var coin = coin_scene.instance()
	  coin.set_pos(450,0)
	  coin_container.add_child(coin)
	
:bust_in_silhouette: Reply From: kidscancode

set_pos() is not a function in Godot 3. The names were changed from pos → position, rot → rotation, etc. Note also that your command wouldn’t have worked anyway, because in Godot position functions require a Vector2 argument, not two integers.

However, even better, you don’t need to use set/get functions, you can just access the property variable directly like this:

coin.position = Vector2(450, 0)

Thank you…it worked.

Akhileswar V | 2018-03-24 05:55

This doesn’t work for me. When I attempt to access set_pos or get_pos, or set_position, or get_position on the newly instantiated scene, like this:

att_ob = att_scene.new(arg1, arg2) att_ob.set_position(self.get_position())

I get something like the below error for each of the functions:

nonexistent function 'set_position' in base

set_position and get_position work for me in other scenes, and I don’t know why they work there and this doesn’t work here.

When I use position instead like this:

att_ob.position = self.get_position()

I get this error:

Invalid set index 'position' (on base 'Node (scene.gd)') with value of type 'Vector2'

2ndGarrison | 2019-04-13 18:38

Show your code, please.

kidscancode | 2019-04-13 18:42

extends KinematicBody2D

const GRAVITY = 100
const SPEED = 100
const UP = Vector2(0,-1)
var movement_vector = Vector2()
var new_target_location = Vector2()
var attack_amount = 5

onready var player_camera = get_node("Camera2D")

#to be set by the alphascene when instantiated
var battle_marker 
var attack_scene 
var attack_object 

var walk_mode = true
var battle_mode = false

func _ready():
	attack_scene = load("res://Attack.gd")
	#captain_marker_node.connect("body_entered", self, "_on_general_CaptainMarker_body_entered")
        battle()
	pass

func battle():
	if Input.is_action_just_pressed("ui_left"):
		new_target_location = Vector2(battle_marker.get_position().x + attack_amount, battle_marker.get_position().y)
		attack_object = attack_scene.new(attack_scene.attack_type.ATTACK_2, attack_scene.leader_type.OPP_GENERAL)
		attack_object.position = self.get_position()
		#print(attack_object.get_node().get_position())
		#print(self.get_position())
		print(attack_object.attacker)
		print(attack_object.leader)
	else:
		pass

2ndGarrison | 2019-04-13 18:54

That error says you’re trying to set position on a Node object. Nodes don’t have position, only Node2D-derived nodes do. Presumably attack_object is a Node.

kidscancode | 2019-04-13 19:04

You’re correct!

I actually tried changing the node to a node 2d in the gui, but in the script it was still just extending Node. Not sure if there’s some refreshing mechanism I’m supposed to do to watch out for this sort of thing.

Anyway, for the scene which I was instantiating, I set its script to extend Node2D, works just fine. Thank you!

2ndGarrison | 2019-04-13 19:11