hey, can anyone explain to me why this works?

class_name FSBaseCharacter

enum attribute_type { STRENGTH, DEXTERITY, CONSTITUTION, INTELLIGENCE, WISDOM, CHARISMA }

var stats = {
	attribute_type.STRENGTH : 10,
	attribute_type.CONSTITUTION : 10,
	attribute_type.DEXTERITY : 10,
	attribute_type.INTELLIGENCE : 10,
	attribute_type.WISDOM : 10,
	attribute_type.CHARISMA : 10
}

static func attribute_string_to_enum(var name: String) -> int:
	return attribute_type[name.to_upper()]

I thought you couldn't call stuff like variables outside the scope of the static function in godot. furthermore, another static function is calling this static function, i thought you couldn't do that?

Interesting. You wouldn't be able to do this in C++ but I haven't needed static functions in Godot yet so I haven't looked into it that far.

I also haven't used static functions in GDScript, but it might be the code works because it's an enum, so maybe it gets special treatment compared to normal class variables.

For static functions calling static functions, that is normal behavior for it to work. A static function is just a function defined in a class that does not require an instance of the class to run. Because it doesn't have an instance, that's why you cannot access non-static variables, as the code has no idea where to look for it.

Right, but wouldn't:

var stats

Be an instance variable and thus inaccessible?

Oh, I see. Yeah, it's just an enum. Those are part of the class.

Yeah, I think its the enum part that is making it work with the static function. My guess is that Godot makes enum's static by default so they are accessible from different nodes/scripts for easier use in conditions.

I also noticed I could access constant member variables with statics as well. I would think the documentation would cover more about them. I'm having trouble finding specifics.

That makes sense though. A constant can't change, therefore all instances would have the same value (similar to a static var).

Ah, so that's the heart of it I bet

Yeah, well GDScript is a dynamic language and is more forgiving than C++. Many times you could do something wrong and there is not even an error message (but of course it won't work). The main reason for crashing would be if you try to access null variables, but for a lot of stuff it either silently fails or does something reasonable that might actually still work.

a year later