Bug on Singleton when assigning values?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Pelli

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()

:bust_in_silhouette: Reply From: Thomas Karcher

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.

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

Pelli | 2021-04-22 22:29

I think I just found a bug.

so try this

var my_local_var = [1,2,3]
global.my_global_var = my_local_var
my_local_var =
print(global.my_global_var)

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

but if you do:

var my_local_var = [1,2,3]
global.my_global_var = my_local_var
my_local_var.clear()
print(global.my_global_var)

this will print()

Pelli | 2021-04-22 22:45

:bust_in_silhouette: Reply From: Thomas Karcher

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.”