Godot: "const" variables are shared between multiple instances of a scene.

Created on 10 Jan 2017  路  7Comments  路  Source: godotengine/godot

Operating system or device - Godot version:
macOS El Capitan (10.11) - 2.1.1.stable.official

Issue description (what happened, and what was expected):
const variables are shared between multiple instances of a scene. When you change the value of a const, it changes on all of the instances of the same scene. They behave like global variables.

Steps to reproduce:
Create a node, add a script to it. Add this code:

const thing = []

func _ready():
    randomize()
    var r = int(round(rand_range(0, 10)))
    thing = [r, r, r, r]

    print(thing)

Now export it as a scene and create multiple instances of that scene. Run and it won't print random thing arrays.

archived

Most helpful comment

Alright, this issue is tracked in #6221 already.

All 7 comments

Uhm, isn't the definition of a constant a variable that never changes once set and is thus indeed global?

To do what you want, shouldn't thing just be a normal var?

That's not a bug, you cannot change the value of a const variable. const means constant, i.e. "never changes".

@akien-mga I think it has to do with the issue of assigning to constants in GDScript...

i can confirm this

extends Node

const thing = ["\"empty\""]

func _ready():
    print("ready ", thing)

func _init():
    randomize()
    var r = int(round(rand_range(0, 10)))
    print("preinit ", thing)
    thing = [r, r, r, r]
    print("init ", thing)

output with two nodes that use that script

preinit ["empty"]
init [5, 5, 5, 5]
preinit [5, 5, 5, 5]
init [3, 3, 3, 3]
ready [3, 3, 3, 3]
ready [3, 3, 3, 3]

and expected should be this?!

preinit ["empty"]
init [5, 5, 5, 5]
preinit ["empty"]
init [3, 3, 3, 3]
ready [5, 5, 5, 5]
ready [3, 3, 3, 3]

Well I don't get this issue, it should not be possible to assign a value to a const at all apart from its initial declaration.

Well that is the issue, it is possible to do it.

Alright, this issue is tracked in #6221 already.

Was this page helpful?
0 / 5 - 0 ratings