zcaliptium / gdinv Goto Github PK
View Code? Open in Web Editor NEWMore or less universal inventory system plugin for Godot Engine (3.3+). Data-driven.
License: MIT License
More or less universal inventory system plugin for Godot Engine (3.3+). Data-driven.
License: MIT License
At current state the parser supports only single definition per JSON config.
weapons/
iron_sword.json
-> iron_sword
bronze_sword.json
-> bronze_sword
potions/
small_health_potion.json
-> small_health_potion
large_health_potion.json
-> large_health_potion
small_mana_potion.json
-> small_mana_potion
large_mana_potion.json
-> large_mana_potion
It could be useful to implement functionality to load multiple definitions from one file:
Weapons.json
->
iron_sword
bronze_sword
Potions.json
->
small_health_potion
large_health_potion
small_mana_potion
large_mana_potion
After setting up everything following the Wiki, I added one .json
file representing an item, which is successfully loaded through the Singleton GDInv_ItemDB
, and everything seemed fine. However, upon closing the game, I stumbled across the console logging out that some Object
instances were leaked.
Therefore, I run again with --verbose
flag (in case you wonder how, from the editor go to Project settings
=> editor
=> run args
and put --verbose
in there). Here I show you an example of the output with the leaked instance:
As you can see, the GDInv_ItemDefinition
class was leaked. Hence, I checked out in the codebase where it was generated, and found out that in the Singleton class GDInv_ItemDB
inside the load_item
function, a GDInv_ItemDefinition
instance is created, which is a class that extends Object
, which however is not freed when the game is closed, leading to the warning above.
So I just changed it to extend a Reference
, which will be freed automatically when the game closes leading to no leaks, but I'm not sure whether this is right solution or not. Any thoughts?
It might even be that I have made a mistake along the way, and this is not supposed to happen usually. In case you think this is the reality, please let me know, thank you.
I'm getting an error saying The class "GDInv_ItemStack" couldn't be fully loaded (script error or cyclic dependency.)
And it seems to be the cyclic dependency because DB and Stack do in fact call on each other. Is this a bug or have I set something up incorrectly?
That can be useful if you have large project tree and don't want to add each directory you made.
I added the plugin as submodule to my project, started godot, enabled plugin, and then -
In GDInv_ItemDB.gd and the same in ItemStack (only it says singleton instead of class)
Also this gets printed to console:
modules\gdscript\gdscript.cpp:1412 - Condition "!named_globals.has(p_name)" is true.
Script does not inherit a Node: res://addons/gdinv/GDInv_ItemDB.gd.
editor\editor_autoload_settings.cpp:460 - Condition "!info->node" is true. Continuing.
I use Godot 3.2
Multiple calls to init() in GDInv_Inventory increase the size of GDInv_Inventory.STACKS altough GDInv_Inventory.MaxStacks stays the same. A size - check before the call to STACKS.append could solve this problem of different STACKS.size() and MaxStacks.
This was an issue for me because I dynamically created the inventory (with call to init()) and on tree_enter GDInv_Inventory.init() gets called again via GDInv_Inventory._ready().
This line crashes Godot.
for i in range (0, stacks_data):
i
becomes a boolean value and can't be iterated upon.
Changing the function to this fixes the issue.
(Notice the added new_stack.stackSize = stacks_data[i].stackSize
)
func from_data(json_data: Dictionary) -> void:
var stacks_data = json_data.get("stacks", []);
clear();
if (typeof(stacks_data) == TYPE_ARRAY):
# For finite inventory.
if (RestrictStackSize):
for i in range (0, stacks_data.size()):
if (i >= STACKS.size()):
break;
# Skip invalid stacks (null elements in array).
if (stacks_data[i] == null):
continue;
var new_stack = GDInv_ItemStack.new();
new_stack.from_data(stacks_data[i]);
STACKS[i] = new_stack;
else:
for i in range (0, stacks_data.size()):
print(stacks_data[i])
var new_stack = GDInv_ItemStack.new();
new_stack.from_data(stacks_data[i]);
new_stack.stackSize = stacks_data[i].stackSize
print(new_stack.stackSize)
STACKS.append(new_stack);
I think GDInv_Inventory
needs to extend Node
, because otherwise it can't work with static typing as all the get_node
kind of functions expect Node
s.
For example all of these give errors:
onready var inventory: GDInv_Inventory = $inventory
onready var inventory: GDInv_Inventory = $inventory as GDInv_Inventory
onready var inventory := $inventory as GDInv_Inventory
_ready():
var inv: GDInv_Inventory = $inventory
I believe this is because GDInv_Inventory
extends Object
and not Node
.
I'm thinking github project must repeat structure of "addons" directory("addons/gdinv"). It wrong installing by it...
It would be useful to be able to remove an item by id instead of having to find the slot index first.
Such method would just return false in case of failure.
Is there any prefered or intendend way to save the inventory?
I tried saving it by doing file.store_val($GDInv_Inventory.STACKS, true)
. But the whole object doesn't get saved, and it gives off errors getting read back.
The only way I found was to keep track of the inventory in a seperate dictionary, but it seems very convoluted to have to keep track of the inventory in a dictionary.
STACKS.erase is looking for the value instead of the index.
https://docs.godotengine.org/en/stable/classes/class_array.html?#class-array-method-erase
Maybe I just don't correctly understand how slots and stacks should work.
I would expect that by setting maxStack: 1
I would be configuring the inventory to only accept 1 item of that type (identifier) and refuse to add beyond that amount.
However the way it currently works, it just "spills over" the new item to a new slot.
This appears somewhat counterintuitive to me.
Let me know what I am getting wrong.
For case when items have different sizes (W x H).
I will think about it because its logic requires specific checks for volume when trying to put item in.
Sometimes 'stackSize' field could TYPE_INT rather than TYPE_REAL.
Lines 25 to 37 in 8a08eea
I often see that game developers use CSV to store all the item definitions.
The File
class has fancy get_csv_line
method that can be used to parse tables.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.