system
October 22, 2020, 7:33pm
1
Attention
Topic was automatically imported from the old Question2Answer platform.
Asked By
Malek
Is this a bug?
const thing = ["a","b"]
func _ready():
thing[0] = "c"
thing[1] = "d"
print(thing)
No error on output window, and the result is (c,d) !!!
system
October 22, 2020, 11:28pm
2
Reply From:
Rhelmar
From the GDScript Documentation :
Since arrays and dictionaries are passed by reference, constants are
“flat”. This means that if you declare a constant array or dictionary,
it can still be modified afterwards. They can’t be reassigned with
another value though.
system
October 22, 2020, 8:06pm
3
Reply From:
jgodfrey
It seems that’s as designed though potentially unexpected . See:
https://www.godotforums.org/discussion/23252/array-typed-as-const-is-not-constant
… and …
opened 09:06PM - 30 Jun 19 UTC
closed 12:56AM - 28 Oct 20 UTC
enhancement
archived
discussion
topic:gdscript
**Godot version:**
3.1 official build
**OS/device including version:**
Li… nux
**Issue description & Steps to reproduce:**
I'll start with quote from docs `Constants are similar to variables, but must be constants(...)`.
The issue is, constants in godot are `flat`, i.e. if the constant is set e.g. to `Dictionary()`, the constant itself is constant, however dict's internals are not.
Let's consider the example below (UTs using *Gut*):
```
extends "res://addons/gut/test.gd"
const FLAT_CONST = 123
const DEEP_CONST = {
'foo': 'bar',
}
func test_flat_cnst():
var pre = FLAT_CONST
# FLAT_CONST = 321 # Parser Error: Cannot assign a new value to a constant
var post = FLAT_CONST
assert_eq(pre, post, "Should pass")
func test_deep_cnst():
var pre = str2var(var2str(DEEP_CONST)) # make copy
DEEP_CONST['foo'] = 'baz' # compiles
var post = DEEP_CONST
assert_eq(pre, post, "Should pass") # fails
```
The second test fails. I suspect why this is happening and even if this is made like that on purpose, IMO, it's counter-intuitive. `const` should enforce `deep` copying etc. I've already ran into trouble as I've been assuming consts are really const (IMO deep).
After writing 1.5k LOC in GDScript this is the only **weird** issue I've faced.
It should either be fixed, or documented better.