I had a similar problem yesterday and this thread is the top result when web searching “godot how to append to an array in a dictionary”. Figured I’d come back here and share the solution to my issue in case others have a similar issue.
I had the following, which looked okey on the surface:
# a dictionary
var attack_indicator_queue: Dictionary[String, Array]
# returns id
func create_attack_indicators(entity_pos: Vector2, attack_pattern: Array[Vector2i]) -> String:
var new_id: String = generate_word()
for pos: Vector2i in attack_pattern:
var ai: Node2D = ATTACK_INDICATOR.instantiate()
level.attack_indicators.add_child(ai)
ai.global_position = entity_pos + (pos as Vector2 * 16)
attack_indicator_queue[new_id].append(ai) # throws error here
return new_id
However on
attack_indicator_queue[new_id].append(ai)
throws error:
invalid access to property or key ‘dqfxizmbiseqmljgbygv’ on a base object of type ‘Dictionary[String, Array]’.
Not very informative, so I could not make sense of it for a while, however, eventually I tried
func create_attack_indicators(entity_pos: Vector2, attack_pattern: Array[Vector2i]) -> String:
var new_id: String = generate_word()
attack_indicator_queue[new_id] = [] # initialize
for pos: Vector2i in attack_pattern:
var ai: Node2D = ATTACK_INDICATOR.instantiate()
level.attack_indicators.add_child(ai)
ai.global_position = entity_pos + (pos as Vector2 * 16)
attack_indicator_queue[new_id].append(ai)
return new_id
And now it works. So, it probably was because arrays in dictionaries are not initialized?
Anyway, hope this spares someone the time it cost me.
Cheers.