Simple question about editing arrays

Godot Version

globalvar.array=[[3, “String”, 3, 4, “String”],[2, “String”, 3, 4, “String”]]


I think this is a very basic question, sorry about asking, but I have been stuck with this for hours. I’ve checked lots of different websites also, including the official docs, but I simply can’t get it to work.

So I have a 2D array defined as a global variable. It looks like the one above. I simply wish to change one value manually but I get this error message:

Invalid set index ‘0’ (on base: ‘array’) …

However, if I simply try

print (globalvar.array[0][0])

I get 3

I can also print the array fine.

So how do you simply change a value manually?

I’m using godot 4


The sample looks good, what does the actual code look like?

1 Like

Thanks for replying so quickly. This would be the full snippet:

This is a card game. Essentially, I am trying to assign a specific index to each of the five cards that the player uses, whose characteristics are stored in a global array of arrays (GlobalVars.player_array). The index is stored in GlobalVars.player_battle_index and is also global. It is defined in a different script and passed to this one via the global variable.

var database = Database *#Database is a dict in a singleton that contains the characteristics of all cards*
var cardname = “Techie” *#placeholder name*

@onready var cardinfo = database.DATA[database.get(cardname)]
@onready var cardimg = str(cardinfo[4])

func _ready():

GlobalVars.player_array.append (cardinfo) *#add the player card that was just added to GlobalVars.player_array[]*
print ("PLAYER ARRAY IS ", GlobalVars.player_array) *#check, works!*
print ("CURRENT INDEX IS ", GlobalVars.player_battle_index) *#check, works!*
print ("BATTLE INDEX OF CURRENT PLAYER IS ",GlobalVars.player_array[GlobalVars.player_battle_index][10]) *#check, works!*


I get the error in the very last statement, where I try to substitute the placeholder index for the actual battle index

If this is the actual code then you have an extra closing square bracket

                                          # one extra here ^ 

But I do not believe this issue will result in the error described.

Yes, thanks. That’s just a typo. I’ve fixed it, but the problem persists.

What is the exact error message? Is it a stack trace? The only thing I could see interfering at this step would be a very strange setter function for player_array

Thanks for taking the time to look at this again.

If I do:


This is the full message I get:

Invalid set index ‘4’ (on base: ‘Array’) with value of type ‘Int’

I’ve been running some tests and it seems it doesn’t like the second index. If I do:


I get Invalid set index ‘4’ (on base: ‘Array’) with value of type ‘Int’

But if I simply do:


Then it replaces the whole of the first subarray within the global array with a 5 (which is not what I want, but at least it tells me that it understands the first index)

If I print the entire array, I get:

PLAYER ARRAY IS: [ [“String”, 5, 1, “String”, 1, 0, “Calls a friend into battle”, 5, 0], [“String”, 3, 1, “String”, 3, 8, “Replenish player hand”, 10, 0], [“String”, 3, 1, “String”, 3, 8, “Takes half damage”, 7, 0]]

Which seems correct to me!

EDIT. BTW, in case this helps, the global variable is initially defined as:
var player_array = [ ]

So it’s declared as a 1D array which I then populate with subarrays. Could this be the problem? If so, what would be the correct syntax? I have tried var player_array : Array[Array] = [] but it doesn’t seem to fix the problem.

For reference, running your initial example works for me on Godot 4.2.2

Maybe we can get farther from printing the player_array, or posting this GlobalVars script.

func _ready() -> void:
	var globalvar=[[3, "String", 3, 4, "String"],[2, "String", 3, 4, "String"]]
	print(globalvar[0]) # [3, "String", 3, 4, "String"]

	print(globalvar[0]) # [3, 4444, 3, 4, "String"]

(perfect awesome thank you, we think in synchronicity)

Again strangely everything seems OK, the 4th index should result in “String”

The definition is correct, var player_array = [ ] has no elements in it yet. append(cardinfo) will add a new Array to it assuming cardinfo is an Array. One thing to note it will be a reference to the cardinfo array so any modifications to player_array[0][x] will also modify the original values.

Yes. That’s the strange thing. I can declare a 2D array anytime, access any value within it and change it at will, just like you did in your example. And it works!

I can also print any value in my problematic 2D array with print array[ x ][ y ], and it also works, which suggests that the 2D array is well declared and populated.

But I can’t change any of its contents for the life of me, although I have the impression I am doing everything right. :frowning: Maybe I’m just tired and I’m overlooking something evident…

(cardinfo is indeed an array and modification is intended, thanks!)

Do you have syntax checking enabled in the Editor. Also check the Errors (Yellow Warnings) in the debugger tab. Something there will tell you what is happening. Maybe you are shadowing another variable.

Thanks very much for your suggestion. This is what I get. It doesn’t seem to point at this specific problem, though.

I think I do have the syntax checking enabled because every time I type something wrong Godot highlights it in red. I find it very useful, to be honest.

Ok, so the thing to do now is single step into the function (or code) that causes the problem. Put a breakpoint on a line before it. When you hit it check the values of each variables in the stack. Make sure you have the Stack Trace tab enabled so you can see your breakpoints and Stack Variables. This will tel you on each line the status of all the accessible variables. When you get to the line that causes the problem

I managed to fix it. It had to do with the dict that stored all the array values, which was declared as “const”. In my understanding, this means I wasn’t allowed to modify the values in the array. Thanks for your help!

1 Like