Append method has no effect on singleton global array

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By dogman
:warning: Old Version Published before Godot 3 was released.

Hello,
I have a singleton that has a global array variable.

Whenever I try to append an element into the array from an another script, it has no effect, nothing is added.

My singleton is called global, and I am trying to access my variable and append like this: global.arrayvariable.append(“stuff”)
No errors, it just stays an empty array.

Any idea what’s wrong here?

Can you post the code of your singleton? Did you used auto-load? Nothing is clearing the array?

Zylann | 2017-02-16 02:34

My singleton has 2 lines:

extends Node 
var selected_units = Array();

I used auto-load yes, and it’s not being cleared since nothing is accessing the array other than the appending one in my other script.

dogman | 2017-02-16 09:59

:bust_in_silhouette: Reply From: Zylann

That’s a known issue with GDScript in 2.x, when you create arrays using their class name, it creates a COW mode one (copy-on-write). That is, it is locally copied as soon as you write to it, so you end up with changes not applied: COW mode · Issue #5277 · godotengine/godot · GitHub

This feature will be removed in 3.0 because a lot of users found it’s confusing. In the meantime, you can use this syntax, which will create the array in shared mode:

var selected_units = []

(no need for semicolon)

Thanks a lot. Haven’t seen this mentioned anywhere so it gave me quite a lot headaches

dogman | 2017-02-16 20:24