Inheritance and Exported Variables

Godot Version

4.2.1

Question

When inheriting a base class that defines exported variables, the values of those variables set in the editor are ignored. Is this expected behavior? I can only find one other topic on the forums expressing a similar issue but the solution posted there doesn’t work in my project. Here’s an example from my project:

Is this a bug or is this intended behavior? If it’s intended behavior, what would be the proper way to structure this sort of inheritance where there’s an “abstract” base class that defines the properties themselves but the classes that extend it choose their values?

I should mention that I’ve tried moving these exported properties to an external resource file and the result is the same when putting that resource object on the base “BattleAction” class, so it doesn’t seem to be an issue with the storage method, but rather the architecture of where I’m putting the properties.

The exported variables get their value after _init() so you’ll need to use another lifetime hook like _enter_tree() or _ready()

While that may be true and may be an issue in my screenshots that I produced to showcase the issue, it doesn’t seem to be the real problem. Here’s an example in the extended game code where an “EnemyEntity” creates an instance of that “Attack” class and then prints the “display_name” property from it during its own _ready method:

The output still says “Placeholder Battle Action” instead of the value defined in the Attack scene. You can see at the bottom that this continues to be the case when the entity executes that Attack action later in the game loop.

Well, I may have finally found the “answer” to this on GitHub: Object.new() ignores scene settings if script has a class_name · Issue #42640 · godotengine/godot · GitHub

For posterity, here’s the important bit:

“This is a missing feature. When you use new() on a class name, it creates an instance of the class, not the scene. Class has no information where it’s attached.”

So, by using Attack.new(…), I’m not making an instance of the scene but rather an instance of the class without its corresponding scene. So I suppose I have to preload the scene and instantiate it from its PackedScene object in order to retain the settings I apply in that scene.

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.