+1 vote

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 MainScreen.gd:

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)

Screenshot

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!

in Engine by (16 points)

2 Answers

+1 vote
Best answer

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):
        slots.append([]) 
        for y in range(3):
            var piece = Piece.instance() # Declare a new instance of Piece
            piece.name = "%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
by (490 points)
selected by

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!

0 votes

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 mypiece = piece.instance()
my
piece.setowner("your main scene")
add
child(my_piece)

by (1,007 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.