Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arrays and Dictionaries with initial value are shared #48038

Open
iiiCpu opened this issue Apr 20, 2021 · 1 comment
Open

Arrays and Dictionaries with initial value are shared #48038

iiiCpu opened this issue Apr 20, 2021 · 1 comment

Comments

@iiiCpu
Copy link

iiiCpu commented Apr 20, 2021

Once upon a time: #24134

Today: Godot Engine v3.2.3.stable.official

extends Node

class Wave:
	extends Node
	export var spawn_data = {}
	export var flag = false
	# # Uncomment to fix
	#func _init():
	#	spawn_data = {}

func reset_waves():
	if true:
		var wave = Wave.new()
		wave.name = "Wave_1"
		wave.spawn_data[ 50] = ['L1']
		wave.flag = true
		$Map.add_child(wave)
		wave.owner = $Map
		print(wave.flag, wave.spawn_data)
	if true:
		var wave = Wave.new()
		wave.name = "Wave_2"
		wave.spawn_data[111] = ['L1']
		$Map.add_child(wave)
		wave.owner = $Map
		print(wave.flag, wave.spawn_data)
	return

func _ready():
	reset_waves()

Expected:

True{50:[L1]}
False{111:[L1]}

Got:

True{50:[L1]}
False{111:[L1], 50:[L1]}

I expect initial value to be constant and all instances would have it's duplicate. Turns out I expect too much, as initial value is shared until 'duplicate' is called. Maybe we should add special keyword for this behavior? Like, I dunno, 'static'? Sounds legit to me.

@Calinou
Copy link
Member

Calinou commented Apr 20, 2021

I didn't even know this syntax was allowed (in contrast to class Wave extends Node)…

class Wave:
	extends Node

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants