Custom Child-Resources for storing named items

Godot Version



In my game I have resources for the different Goods in the game. I have a parent class called Good. I also have child classes of Good such as Food, Ore, etc.

I want every Good to have a good_name, so in the Good class I have

extends Resource
class_name Good

static var good_name:String = "Default Good Name"

and in the child class I have

extends Good
class_name Food

func _init():
	good_name = "Food"

In other parts of the code, I want to get any Good Resource and the name of that resource.
But if the variable is just a resource or reference to the Food Resource then in order for good_name to be “Food” and not “Default Good Name” I need to call, just so that is “Food” and not “Default Good Name”. But do I really have do call .new()? That is not efficient. Is there a way to have a static var for a Resource that is different for different sub classes?

In GDScript static variables are shared amongst all instances so I don’t think making the name static would work to begin with.

You could make a static get_name function, like:

static func get_goods_name(resource):
    if resource is Food:
        return "Food Name"

But yeah, I’d be interested in better solutions to this problem, too.

Thank you! This solves my problem.
I’ve changed your solution slightly, so that each child class of Good has its own version of static func get_goods_name() that simply returns the name of that Good.