Kehom / GodotAddonPack

A collection of pure GDScript addons for Godot
http://kehomsforge.com
MIT License
183 stars 15 forks source link

[Inventory] Invalid call when using add_item() function for the InventoryBag node #29

Open KometFox opened 3 years ago

KometFox commented 3 years ago

While I tried to follow some of your Inventory addon tutorials I stumbled upon some issues, I managed to rewrite the "_generate_item()" function as "_get_item(id, category)" so that its being used to load valid (I think?) item entries that gets added to a dictionary variable, now when I use this "_get_item(id, category)" function to add a item to the InventoryBag node I get this error: Invalid call. Nonexistant function 'disabled_slots_occupied' in base 'Nil'.

When I use the "add items" code example and try to add the item that way I get the same error.

Debugger output:

0 - res://addons/keh_ui/inventory/bag.gd:948 - at function: _get_colliding_data
1 - res://addons/keh_ui/inventory/bag.gd:1029 - at function: _find_fitting_spot
2 - res://addons/keh_ui/inventory/bag.gd:164 - at function: add_item 
3 - res://System/Inventory/Init.gd:19 - at function: add_items
4 - res://System/Inventory/Init.gd:14 - at function: ready 

Node2D, InvDemo.gd script: https://ghostbin.com/paste/km8Ek

Node, Init.gd script: https://ghostbin.com/paste/8yRFk

Kehom commented 3 years ago

OK. That's probably a big problem of how I "faked" static variables.

Just to test things, could you please test deferring the call to add the item into the bag? Either by

call_deferred("add_items", item)

in the _ready() function or

InvBag.call_deferred("add_item", idata)

in the add_items() function?

Problem here is that the bag (and the special slot) require internal initialization that is deferred because the tree is not fully built when the code is run. The result of that is the internal variable is null until the next main loop iteration.

Now, even if this works, please just notify me without closing the issue. I will use this as a reminder that I need to review the code and try to find a better solution related to the initialization in order to avoid this kind of problem.

KometFox commented 3 years ago

@Kehom The call call_deferred("add_items", item) fixed it and now the item shows up correctly and so far it can be used now, thanks for the quick reply I appreciate it. It did made me wonder because when I looked at your original invdemo.gd script the button function that adds the item is not relying on this function.

Kehom commented 3 years ago

That happens because when the button is pressed it's almost certain that the initialization will be completed when it is pressed. I doubt someone will be able to press it on the very first frame of execution!

The thing is, the initialization of the inventory system only finishes at the end of the very first frame in which it was loaded. This means that certain operations will fail if executed before that initialization could occur. Buttons rely on events that occur after this initialization.

I will review the code and try to find a solution that removes the requirement of delaying the call. And if I can't find one, try to make it more clear about this limitation.