And I have made my skill class for custom resources as the following:
extends Resource
class_name SkillClass
@export var name: String
@export var power: int
@export var cost: int
@export var accuracy: int
@export var active: bool
@export var description: String
@export var skill_id : SkillDB.Skill_id
func _ready():
_set_skill_id()
func _set_skill_id():
skill_id = SkillDB.skill_dictionary[name]
func get_skill_id() -> SkillDB.Skill_id :
return skill_id
The _set_skill_id() method is called to initialize the skill_id accordingly to the SkillDB.
However when I checked the result, the skill_id is always set to 0 and not to the skill I created the resource for.
Why is this happening?
Thank you for the comment.
I found another way around and changed my code as the following:
extends Resource
class_name SkillClass
@export var skill_id : SkillDB.Skill_id
@export var name: String :
set(value):
name = value
_set_skill_id()
@export var power: int
@export var cost: int
@export var accuracy: int
@export var active: bool
@export var description: String
func _set_skill_id() -> void:
skill_id = SkillDB.skill_dictionary[name]
But somehow, this code only makes my second skill_id correctly to 1, and the third skill is still set to 0.
The only way I can work this around is by calling the setter function everytime I load the skill resource to some other node, but I don’t want it to be this way.
I want the skill_id to be set when the resource is first created.
You basically have two separate references to the same thing - one with the enum, and one with the name String.
I would suggest you to do this reference only once - ideally the enum version of it to keep it type safe.
Then if you need the name String somewhere in your code, you can retrieve it with a getter, don’t bother with the setter.