How to set separate properties of multiple instances?

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

Before anything, I’ma noob who learned about this engine 'bout 3 weeks back. Sorry!
Anyway, I’m trying to make a naughts-and-crosses-type game, so I’m want to create 9 instances of a piece scene in a rectangular fashion

Contents of

func _ready():
	var piece = preload("res://pieces.tscn") #Preload the piece scene
	var slots = []
	for x in range(3):
		slots.append("[]") #Now we've got slots[][]
		for y in range(3):
			add_child(piece.instance()) #add that to the tree
			get_node("piece").position = Vector2(300*x+92, 292*y+92) #Set the position of the newly added node
			slots[x][y] = get_node("piece/AnimatedSprite").animation #Get the name of current animation for game logic purposes ("black" rn for demonstration only, normally would be "default") 

The problem is, the code is changing the position of only one of nine children, when it supposed to modify each one separately! So now, the first piece is where the last is supposed to be, and every-single-other-one has defaulted to, erm, the default position! (0, 0)

So, I’m definitely doing something wrong, but cruising thru the documentation and googling to the best of my ability, I can’t really find a solution to this here problem. Also, I’d like to further change the properties of these scenes (the pieces) later in code (if it’s important).
Again, I apologise for being a noob, but any help is appreciated!

:bust_in_silhouette: Reply From: magicalogic

Try storing the instanced piece to a variable then set the main scene as the owner of that piece then add it to the tree. This way:

for y in range(3):
var my_piece = piece.instance()
my_piece.set_owner(“your main scene”)

:bust_in_silhouette: Reply From: AndyCampbell

I believe the problem is that you are not saving multiple instances of piece outside your _ready function. Try this example

extends Node2D

var slots = []		
# Declare this at the top so it is persistent, not only inside the _ready function

func _ready():
	var Piece = preload("res://pieces.tscn") #Preload the piece scene
	for x in range(3):
		for y in range(3):
			var piece = Piece.instance() # Declare a new instance of Piece = "%s : %s " % [x,y] # Give it a user friendly name (optional)
			piece.position = Vector2(300*x+92, 292*y+92) #Set the position of the newly added node
			slots[x].append(piece) # Store your instance in the slots array
			add_child(piece) #add that to the tree

func _process(delta):
	# Now you have an array containing each of your objects in it and you can act on separately, for example:
	for x in range(3):
		for y in range(3):
			var cur_pos = slots[x][y].position
			var new_pos = Vector2()
			new_pos.x = cur_pos.x + rand_range(-5,5)
			new_pos.y = cur_pos.y + rand_range(-5,5)
			slots[x][y].position = new_pos

Ahh, I see. So the piece contains a PackedScene and the array “slots” is now filled with Nodes that can by modified by calling the appropriate cell in array, right? Anyway, it works (finally!), and i think I start to get how gdscript works now. Thank you very much for help!

dawdejw | 2020-11-23 12:49