0 votes

If I have a si singleton called global

[Updated]

I think I just found a bug.

so try this

var mylocalvar = [1,2,3]
global.myglobalvar = mylocalvar
mylocalvar = []
print(global.myglobalvar)

this will print([1,2,3])

but if you do:

var mylocalvar = [1,2,3]
global.myglobalvar = mylocalvar
mylocalvar.clear()
print(global.myglobalvar)

this will print([])

Godot version 3.3
in Engine by (126 points)
edited by

2 Answers

0 votes

It doesn't. I just tested these four lines both with Godot 3.2 and 3.3 (with a global.gd containing nothing but var my_global_var), and the result was always "1" (as expected). There must be something else going on in your project.

by (1,648 points)

thanks for testing it, yeah I cannot understand what is happening is like the parameter is by reference and not by value , so changing the local value changes the global value, but I am making only 1 assignment and then changing the value of the local variable so I cannot understand what is happening. I will update If I can fix it

I think I just found a bug.

so try this

var mylocalvar = [1,2,3]
global.myglobalvar = mylocalvar
mylocalvar = []
print(global.myglobalvar)

this will print([1,2,3])

but if you do:

var mylocalvar = [1,2,3]
global.myglobalvar = mylocalvar
mylocalvar.clear()
print(global.myglobalvar)

this will print([])

0 votes

Answer to the updated question: That's not a bug, but a documented feature of the Array class: "Arrays are always passed by reference. To get a copy of an array which can be modified independently of the original array, use duplicate."

https://docs.godotengine.org/de/stable/classes/class_array.html

by (1,648 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.