#Enum skipping integers as keys in a Dict?

19 messages · Page 1 of 1 (latest)

blazing gust
#

hey all! I have a question that's killing me for 24 hours. Please help!

In my code I define an enum on a custom Resource:

extends Resource
class_name Currency

enum {
    HP = 0,
    CREDITS = 1,
    GUNS = 2,
    NINJAS = 3,
    HACKS = 4
}

# game-data.gd
extends Node
var atoms: Dictionary = {
    "Currencies": {
        Currency.HP: preload("res://data/currencies/hp.tres"),
        Currency.CREDITS: preload("res://data/currencies/credits.tres"),
        Currency.GUNS: preload("res://data/currencies/guns.tres"),
        Currency.NINJAS: preload("res://data/currencies/ninjas.tres"),
        Currency.HACKS: preload("res://data/currencies/hacks.tres")
    }
}

func _ready():
    print(var2str(atoms.Currencies)

# Output:
{
0: Resource( "res://data/currencies/hp.tres"),
3: Resource( "res://data/currencies/credits.tres"),
4: Resource( "res://data/currencies/guns.tres"),
5: Resource( "res://data/currencies/ninjas.tres"),
6: Resource( "res://data/currencies/hacks.tres")
}```

Notice how the keys in my Dict skip integers 1 and 2? Why does it go 0, 3, 4, 5, 6??? Checking on the enum, when I print their values (right after I print the above Dict): Currency.HP =0, Currency.CREDITS = 1. How come it seems to skip 2 numbers when I use it as keys in my Dict? Any ideas??
distant matrix
#

What do you get if you just do a normal print on it and if you print it's keys()?

twin juniper
#

What version of Godot are you using? When I test your code it seems to work as expected

blazing gust
#

I am using this version of Godot: v3.5.stable.official [991bb6ac7]

#

Here are the other prints I have tried, which confirm my original output. Man this is a weird one:

0: Resource( "res://data/currencies/hp.tres"),
3: Resource( "res://data/currencies/credits.tres"),
4: Resource( "res://data/currencies/guns.tres"),
5: Resource( "res://data/currencies/ninjas.tres"),
6: Resource( "res://data/currencies/hacks.tres")
}
Currency Dict (raw): {0:[Resource:1278], 3:[Resource:1283], 4:[Resource:1288], 5:[Resource:1293], 6:[Resource:1298]}
Currency Keys: [ 0, 3, 4, 5, 6 ]
#

And confirmed that the CREDITS enum is still 1 after printing the above:

#

When I test your code it seems to work as expected

Ok thank you! This is useful because it points to maybe something somewhere else in my code that is going wrong

distant matrix
#

@blazing gustwhat happens if you try to explicitly add the Currency.CREDITS key to the dict on its own?

blazing gust
#

Hmmm ok I haven't tried that! I'll try just about anything now, I'm murdering my code with print() functions 🙂

distant matrix
blazing gust
#

ok now it's even more strange, like I must be looking in totally the wrong place and I can't even explain it...

here's the new code (I think this is what you meant to try):

    "AssetTypes": {
        AssetType.AGENT: preload("res://data/asset-types/agent.tres"),
        AssetType.SITE: preload("res://data/asset-types/site.tres")
    },
    "Currencies": {
#        Currency.Type.HP: preload("res://data/currencies/hp.tres"),
        Currency.Type.CREDITS: preload("res://data/currencies/credits.tres"),
#        Currency.Type.GUNS: preload("res://data/currencies/guns.tres"),
#        Currency.Type.NINJAS: preload("res://data/currencies/ninjas.tres"),
#        Currency.Type.HACKS: preload("res://data/currencies/hacks.tres")
    },
    CurrencyWad: preload("res://scripts/resources/currency-wad.gd"),
    "Assets": []
}```

All but one of the Currencies keys are commented out, but in the Debugger I am seeing the same 5 key/value pairs I had before
#

(thanks for the reminder about breakpoints, very useful)

distant matrix
blazing gust
#

ohhhh i see

distant matrix
#

check if there's any other places in all of the code that refer to this enum

#

*dictionary

blazing gust
#

OMG I fixed it!! I tried your last suggestion and then it finally hit me: I thought the Currencies dict was created in that code at runtime, but elsewhere it was being overwritten by a saved Dict. I am an idiot 😛

#

Thanks so much for helping out though! I was about to give up I think

#

So, in the end the enums were from an old enum config I had that got saved to disk, and it was then being loaded at runtime ever since, overwriting what I was trying to hardcode