• Godot Help
  • GD3/4 Could not preload resource "res://inventory/Slot.gd"

To be clear I don't know that this is the cause of the error, it's just the first thing that jumps out at me.

When I changed from const to var I got the error "SlotClass is a variable but does not contain a type"

Preload has to use @onready

@onready var slot = preload("path/to/file")

Also, I'd have to check the docs, but load/preload are for resources, and it may be that a script is not a resource.

Thanks but It still gives out the preload error.

I will just try update the tutorial project to GD4 to see if it has this problem too.

Are you sure that file exists? I just tested and I don't see any errors.

You mean in my project? Yes, the Slot.gd is always there and working fine prior adding this particular code:
PlayerInventory.active_item_slot == slot_index

The Tutorial's project is working fine on 3.x but I convert its script to 4 as I follow along for my project. Maybe I will upload my project so ppl can have a check.

So the path is correct, because it will tell you if the file doesn't exist. Most likely, there is an error in the code, for example a cyclic reference.

So I quickly convert the tutorial file to GD4 and no preload error show when run. There is probably something wrong somewhere with my code, as you said. I will try checking my codes against his.

Wait no, I'm so stupid. I commented that line out. When I add the line in the converted tutorial file also gives me Could not preload resource Slot.gd error.
Maybe something changed in GD4.

Here is my project (tested with GD4a15). The original tutorial is GD3 for 2D but I converted to GD4 for 3D.

If anyone would like to have a look because I'm not sure if it's my coding mistake or this part of tutorial is just not compatable with GD4.

https://github.com/j3du/inventorytest

  • Slot.gd is actually attached to panel slot.

  • Slot.gd part that causes error:

    func refresh_style():
    #	PlayerInventory.active_item_slot == slot_index
    	if SlotType.HOTBAR == slot_type and PlayerInventory.active_item_slot == slot_index:
    		set('theme_override_styles/panel', selected_style)
  • Error appears at PlayerInventory.gd.
    const SlotClass = preload("res://inventory/Slot.gd")

Thanks for the help.

Looks like a cyclic reference. The scripts cannot access each other like that (one loads one, then the other loads the first one, again forever).

    @cybereality It works in 3.x so this just means GD4 not allow it anymore? What can I do to fix it.

    • I updated my github link to include relevant folders

      cybereality cyclic reference

      Yeah, those are never good.

      Gowydot It works in 3.x

      You've probably just gotten very, very lucky through sheer chance so far with it...

      Can I ask something odd... why does this const SlotClass = preload("res://inventory/Slot.gd") even exist? Just remove the line; I don't see it used anywhere in the code you've given. (NOTE: I have NOT checked the whole project). But, if there is some use to it, you can simply substitute it with some other resource, var, etc.

        No it didn't work in Godot 3 either, and most programming languages. It's probably just an error/bug in your code.

        I hope so because I wouldn't know how to change codes to fix it.

        Just checking, does my syntax look correct for GD4?

        GD3
        PlayerInventory.connect("active_item_updated", slots[i], "refresh_style")

        GD4
        PlayerInventory.active_item_updated.connect(slots[i].refresh_style)

        a month later

        So after all this time I managed to fix it by simply removing the preload and any reference to it... 🤷
        (Luckily only used to infer type)

        const SlotClass = preload("res://inventory/Slot.gd")
        #delete this
        
        ...
        
        func add_item_to_empty_slot(item: ItemClass, slot)
        #then delete  : SlotClass on functions

        I never understood why you would be preloading a script. Everything is in a global namespace in Godot, so you don't need to load anything to access other classes.

          cybereality Yeah I wouldn't know anything about that, I just want to learn some Inventory so I can use it for 4.x. I will follow the final chapter of this tutorial and see if anything will go wrong.

          I really should give a shoutout to ThinKing_2005 as he did gave the right answer for this problem 👍️ (back then I didn't understand the errors that came after deleting it)