0 votes

I want to create a class for my game that holds data for game characters:

extends Node
class_name AttributeSet

var max_health
var movement_rate

This class gets instanced so game entities can use it. However, I also want to refer to these variables in an interface-ish way, without having to access the instance. So far I've been using strings to refer to the variable name, but that leads to typos and isn't very elegant. I'd like something like this:

player.set_attribute(AttributeSet.health, 100) # this fails because 'health' is not a constant

I want to avoid this:

var player_attribute_set : AttributeSet = player.attribute_set
player.set_attribute(player_attribute_set.health, 100) # this works but it's cumbersome

or this:

player.set_attribute("health", 100) # leads to errors

Any ideas?

Godot version 3.5
in Engine by (194 points)

What kind of errors happen when using last method ?
ANyway, did You check ClassDB methods in documentation ? I guess You can get list of all class properties as strings, and then use them for set() function

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.