Skip to content

Can't use enum to iterate through Dictionary[enum, ...] - seems to be passing in strings? #107903

Open
@laffcat

Description

@laffcat

Tested versions

Reproducable in 4.4.1

System information

Godot v4.4.1.stable - Windows 10 (build 19045) - Multi-window, 2 monitors - OpenGL 3 (Compatibility) - NVIDIA GeForce RTX 2070 SUPER - Intel(R) Core(TM) i7-10700 CPU @ 2.90GHz (16 threads)

Issue description

Using for each in enum: and trying to access a Dictionary[enum, ...] with dict[each] crashes the project back to editor, with the following error (generated by MRP):

E 0:00:01:729 stats.gd:23 @ leveled(): Attempted to getptr a variable of type 'String' into a TypedDictionary.Key of type 'int'.
<C++ Error> Method/function failed. Returning: false
<C++ Source> ./core/variant/container_type_validate.h:103 @ validate()
stats.gd:23 @ leveled()
stats.gd:27 @ _ready()

Steps to reproduce

extends Node

enum Stat { HP_MAX, ATK, DEF, MAG, WRD }

var stats_base : Dictionary[Stat, int] = {
	Stat.HP_MAX : 100,
	Stat.ATK : 50,
	Stat.DEF : 60,
	Stat.MAG : 20,
	Stat.WRD : 10,
}

var stats_growth : Dictionary[Stat, int] = {
	Stat.HP_MAX : 30,
	Stat.ATK : 10,
	Stat.DEF : 20,
	Stat.MAG : 5,
	Stat.WRD : 10,
}

func leveled(lv : int) -> Dictionary[Stat, int]:
	var dict : Dictionary[Stat, int] = {}

	for s in Stat:     ## <-- Swap first "for..." w/second 
	# for s in 5:     ## <----- to make this functional
		dict[s] = stats_base[s] + (stats_growth[s] * lv)
	return dict
	
func _ready() -> void:
	print(str( leveled(1) ))
	print(str( leveled(15) ))
	print(str( leveled(70) ))
	print(str( leveled(200) ))

Minimal reproduction project (MRP)

dict_enum.zip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions