Access/autocomplete variables from a non-instanced class

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By bloqm

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?

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

Inces | 2022-05-27 15:58