This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

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?

in Engine by (28 points)

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

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.

1 Answer

+2 votes
Best answer

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: https://github.com/godotengine/godot/issues/5277

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)

by (29,510 points)
selected by

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

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 Frequently asked questions and 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 [email protected] with your username.