Appending to a subarray

Godot Version
Godot 4.4.1 stable

Question
I’ve been using Godot for a little while, following an amazing tutorial on YouTube (it’s making a card game).
In this game, I have slots for the cards to go in, but the cards can be stacked on top of each other. I’m still pretty early on in everything, but I’ve been trying to make an array with arrays (something already done with the deck database, containing an array of cards that have an array of its info) for the ‘team.’ I want the player’s team to be able to store each card in each slot, so that I can combine them (eventually). I can make the 2D Array, but I can’t seem to find how to append something to one of the specific arrays.

You’ll need the index of the array you want to append to and Array.append() into it:

var team_stacks = [
    ["card_0", "card_1", "card_10"],
    ["card_11", "card_3", "card_9"],
    ["card_52", "card_8", "card_2"]
]

team_stacks[0].append("card_14")    # The array in index 0 is now ["card_0", "card_1", "card_10", "card_14"]
team_stacks[1].append("card_6")     # The array in index 1 is now ["card_11", "card_3", "card_9", "card_6"]
team_stacks[2].append("card_7")     # The array in index 2 is now ["card_52", "card_8", "card_2", "card_7"]
2 Likes

Thank you! This is what I thought, but apparently my issue was I was putting things in the right variable, but it wasn’t set correctly. I have two team vars, one for the player and one for the opponent. I had made the opponent’s team into a 2D Array, but I forgot to set the player’s team to a 2D Array (so it was just a regular array). Thank you again!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.