• Godot Help
  • Preloading a scene with a deeply inherited script fails to initialize variables.

Hi! I've been restructuring my project and have run into a serious issue that I can't seem to understand why it's happening and haven't seen much online about it. I'm hoping someone will give me some sort of insight as to what's happening.

So I have been setting up classes and using inheritance to organize them nicely. They are as follows: -Saveable extends Node2D#For anything saving and loading -Interactable extends Saveable#For anything to be interacted with -BaseItem extends Interactable#General type for items -Snappable extends BaseItem#For anything that should snap to other objects -Farmplot extends Snappable#Handles behavior and data for the farm plot item

It's nothing too complicated. And this works fine when I place farm plot instances in the world and run. My issue arises in my autoload script.

In this script, I have an array that keeps track of the preview image, name, price, and a preloaded packed scene for every unique item in the game. itemArray = [ ItemData.new(preload(-img preview path-), -name-, -price-, preload(-packed scene path-)), ... ] (ItemData is a class that contains the corresponding data. I do this instead of an array of arrays for ease of access)

When I run this with the array filled, the preloaded scene for the farm plot item does not load properly. When instancing the preloaded scene and following it through the debugger with a breakpoint, the stack variables sections does not show the variables related to Interactable and BaseItem(the parent scripts to snappable). Because of this, the program attempts to initialize the variables in those parent scripts but initializes them onto the snappable variables, like there is some indexing issue... this is the part that puzzles me. When the program continues to initialize everything else, the snappable variables are set correctly. But this means that the variables in the parent classes are not initialized, so the game crashes when you attempt to reference them. And to make it even more confusing, this only happens when the snappable that is being preloaded is not the first one. If I move the farm plot item to be first in the array, it initializes fine.. but when an item is preloaded before that, it has this initialization issue.

I can provide pictures if my novel doesn't paint a clear enough picture. Any thoughts will be appreciated.

Thank you!


Check if the evaluation in the class constructor is causing an issue.
This is a shot in the dark but I vaguely recollect something about that.
var p:PackedScene = preload(...)
...ItemData.new(p, ...

Also try it as a variant rather than type.
Again this is a real shot in the dark.

    LeslieS
    I have provided a screenshot of the ItemData script to see how the constructor is set up. I do not believe it has anything to do with the ItemData class(Please correct me if I may be overlooking something here).
    And I removed the packed scene specification and ran it, but It had no effect, unfortunately.

    Thank you!

    Also, I am working in 4.0.2, but I copied the project and updated to the latest version and still encountered the issue. So it's not a version issue.

    • xyz replied to this.

      jest0pher Putting some print statements inside constructors may work wonders in resolving stuff like this.

      See if it is a single class or inheritance level that is causing grief.
      Comment out all the class contents except the base and first child.
      If it runs without issue, then uncomment the contents of the third child and repeat.

        LeslieS It's something with the inheritance I believe. When the farm plot's parent is changed to BaseItem(instead of Snappable) it runs fine. But now I dont have the snappable functionality.

        With the farm plot having snappable as it's parent, I tried commenting out all the variables and functions so all that was left was "class_name Snappable extends BaseItem" and this had pretty much the same issue. It doesn't see BaseItem and Interactable variables in the stack variables list.

        Hello all, I have found the issue, yet it makes no conceivable sense, to me.
        And for those who have downloaded the test project, feel free to try it yourself.

        We have found that the issue stems from the controller UI script. There is a single if statement in SetButtons that checks to see if the interactable is a snappable. For whatever reason, if you remove this/ swap it with something like has_method to indirectly check if it's a snappable, it works just fine. View screenshots below.