0 votes

I just found out that, if I create 2 matrix using this method:

var cell_code=[]
var cell_type=[]

func create_matrix(row,column):
    for x in range(column):
        var col=[]

the two variable cell_code and cell_type will be forever linked in a magical way.
As a matter of fact, the above function will print "hello".

Is this a wanted behavior? How can I overcome it? Is there a better way to create matrix?

in Engine by (1,432 points)

1 Answer

0 votes
Best answer

The behaviour must be because you are appending the same array to both of the arrays.
Try appending a different array for cellcode and celltype separately.

by (3,873 points)
selected by

Appending the same array is indeed the reason of the problem, and doing as you say solves the issue.
However, since i want to have many matrix (one for each property of the cell), it is quite uncomfortable to have so many col variables, one for each matrix.
So, do you know an easier way to create many matrix?

PS: just for the sake of knowledge, I would like to know what is happening in the code i wrote previously, why is it behaving like that?
It seems like, while writing cell_code[1][1]="hello" I am actually writing inside col[1], and this would explain why cell_type get modified too.
However if that´s the reason, remember that every row of the matrix is a copy of col, meaning that cell_code[1][2] and cell_code[1][3] and cell_type[1][2] and cell_type[1][3] should be overwritten as well, but that´s not the case.

I´m really puzzled!

It because an assignment to a non primitive type usually means it holds a reference to that object/array instead of copying the value. The behavior is the same with other languages, with a few exceptions like c++ (you can still achieve this in c++ by using pointers)

var a = []; (means "a" hold a reference to the array)
var b = a; (means "b" hold reference to the array a is pointing to)
b.append(1) means that the array that they both reference is now [1]

contrast this with

var a = 1;
var b = a; (the value of a is copied to b)
print(b+1) //will be 2
print(a) //will be 1

Thank you very much! Understood, it´s an important thing to know while working with arrays.
The only question remaining is if there is a better way to create a matrix without encountering this issue: do matrix exist on gdscript or they are only a sequence of mono-dimensional arrays (impossible to separate from the arrays they originate from)?

I just finished reading back in the docs and I got to an interesting part.
Apparently, dictionaries can be used to create a matrix, and are possibly more efficient to use and sample from.

var mat = {}
mat[Vector2(1, 2)] = 1.0 #sets the third element in the second column to 1.
var s = mat[Vector2(3, 0)] #retrieves the first element in the fourth column; which would be null by default.

I tried it in my project and it works flawlessly!!
That´s exactly the kind of easy matrix management i was looking for, thanks you sooo much!!

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.