Exported Dictionary not behaving as intended

Godot Version

4.2.2

Question

Hello all!

I’m having trouble with exporting a dictionary to the inspector. When I hard code it, it works fine, but I’m trying to be a bit smarter with my code.
I’m trying to have a dictionary as the base for other similar dictionaries, so I don’t Copy/Paste code, in case i need to change it, I only have to change it in one spot.
My code:

extends Node2D
class_name Critter_Base
var Stats = {
	"HP" : 1,
	"ATK" : 0,
	"DEF" : 0,
	"SPD" : 0,
}

@export var GVs = Stats.duplicate()

and in the inspector, the GVs dictionary shows up, but with a size of 0… I’m a bit confused, I thought this was exactly the kind of thing that duplicate was for. Is it getting weird because of export? or maybe I’m using duplicate wrong? I’m a bit confused why it’s happening in the first place.

I would greatly appreciate if anyone could share some light on this. Thanks for reading!

I suspect that the duplicate function isn’t being run in the editor?

You can try closing and opening the scene with the script to see if it’s an editor bug.

Also try to static type the variables to Dictionary. So the export really knows what it is.

As PennyLoafer says the variable Stats doesn’t exist until the code is run (so not in the editor).
I am not sure what you are trying with duplicate() here but it is irrelevant as this won’t work without it either.
Just code it in.

@export var GVs:Dictionary = {
	"HP" : 1,
	"ATK" : 0,
	"DEF" : 0,
	"SPD" : 0,
}

Going by the name Critter_Base I guess this is going to be used as the base class for a number of critters.
So for CritterTypeB just extend Critter_Base and it brings with it the GVs you coded in the base class. That is efficient enough.

extends Critter_Base
class_name CritterTypeB

However this is probably best done as a resource.

extends Resource
class_name Critter_Base
@export var GVs:Dictionary = {
	"HP" : 1,
	"ATK" : 0,
	"DEF" : 0,
	"SPD" : 0,

Then just select new resource at the top of the inspector, navigate to Critter_Base as the type, edit the exported values, save the resource and you have a new critter resource.

What I was trying to do with the duplicate is get the GVs dictionary to inherit the values from stats. I want 3 different dictionaries to have the same key value pairs as stats, it feels kinda messy to have every dictionary just copy/paste of stats. I wanted to see if there was a better way, y’know?

Resources seem like a bit of work for what I want to do. I keep meeting trouble when I try to understand and implement them. Inheriting from a custom class just seems simple and easy.

Then just create a stats class:

class_name BaseStats
var hp:int = 1 
var atk:int = 0 
var def:int = 0 
var spd:int = 0

And instantiate it inside your critter class and where ever else you are using it.

extends Node2D
class_name Critter_Base
var stats:BaseStats = BaseStats.new()