Dynamically adding structures to structures for inventory

Hi guys. Previously I was using the inventory extension, but I decided to switch to a structure variable instead.

However, I’m having serious trouble figuring out make this system dynamic. Essentially I want to be able to add structures to the Inventory structure. Like this for example:

Inventory (structure)

Item (structure)

Quantity (number)
Weight (number)

But I want to do this dynamically in events so I can add and remove items. Otherwise I’d have to have pre-existing items, which would mess up the way my inventory is currently built (items that exist in the inventory are displayed)

What part is giving you problems?

To add structures dynamically, you put the structure name in braces like

Item to “Corn”
Change the variable Inventory[Item].Quantity = 11
Change the variable Inventory[Item].Weight = 111

Here’s a little test project using for each child to cycle through the inventory and display the values in a text object.

Scene variables (Structures was the scene name)

Events

Result

It would simplify things if you created an extension with functions to do things like setting and checking inventory.

Thanks for the reply. I still don’t really understand how this works. Sorry my brain really isn’t working rn lol.

As of now I’m using an array to store the name of each object in the inventory, and the inventory extension to keep track of the quantity. I wanted to switch to a structure variable because I assume that remove the need of an extension and allow more item data

This is how items are added to the inventory in my current system:

And this is how they are displayed:

What I want to do is multiply the weight by the quantity and get the total weight of the inventory. I’m thinking I might have to make some kind of index to define the weight of each type of item.

I also want to have multiple limited stacks of an item, but this system only works for a single stack, since the inventory extension doesn’t know how many are in each stack, only the total amount of that item.

I was considering using the Slot System extension by @infokubarcade, which seems to have the functionality I need, but I have no idea how to use it.

Maybe I could store the item data in variables on the itemframes? Idk smoke’s coming out my ears lol

Variables can be a difficult concept. IDK your experience. A structure could simplify and organize things.

See:
https://wiki.gdevelop.io/gdevelop5/all-features/variables/structures-and-arrays/

Variables are very flexible. You can mix and match different things like an array or structures or a structure of structures. The structure can contain other structures or arrays.

Structures and arrays need to be consistent. An array of structures should all have the same children in every structure. For example, each structure could have a weight and quantity child variable. They should all be numerical.

If you had an array of structures then you could have children named, item, quantity and weight.

If you had a structure of structures then the structure name could be the item name with children named weight and quantity.

Its the difference between

Inventory[0].Item=“wood”
Inventory[0].quantity = 10
Inventory[0].weight = 10

And

Inventory.wood.quantity =10
Inventory.wood.weight=100

If the individual weight is fixed then weight could be in a structure and you could just store the quantity. You could then multiple the quantity by the weight from the structure.

The weight structure could be a variable like weights with the children names of the items.

You could then use for each child to add up the weights using weights[Item] where the variable item could be the item like wood or whatever.

I’m not sure what your goal is. The structure could include a structure that included cost or a description or any other detail.

If you’re new to the concept of structures then maybe it would be easier to create some test projects that use structures. Just to get a better understanding in a simplier environment.

I can clarify anything I posted. I went on a bit and it might not all be applicable.

Thanks so much for the help.

In this event for example, how would you replace my system with a structure?

It doesn’t seem to be
“Add the value Item.AnimationName() to array variable Inventory
Change the variable Inventory.[Item.AnimationName()].Quantity: add 1”

If your making an array of structures then you can build the child and then add it to the array.

The problem with that would be it would be tough to combine items of the same type.

A structure of structures would simplify it.

With a structure named inventory

Inventory[animation].Quantity =7

To get the value just

Inventory[animation].Quantity

If the items were in an array you could use for each child, if theyre objects, you could use for each object.

If you’re using the inventory system then the inventory can be saved to a variable and then you could use for each child. I haven’t messed with the inventory lately. I don’t recall the particulars.

You can use the condition child exists to check if an item is in inventory.

1 Like

This was pretty interesting and shows the flexibility of the variable system that you mentioned. I tried this with an empty structure scene variable with no children called Structure, and the only item I had in the scene at the moment, a tree. I wrote it as: change the value Structure[Tree.Animation::Name()].Quantity add 1 and it created the child structure of Structure with the Tree animation name and gave it a child Quantity of 1.

I tried “Change the variable Inventory.[Item.AnimationName()].Quantity: add 1” but it won’t accept that. To clarify the “Inventory” structure is empty to begin with, which means I’d have to add a structure (Item-Quantity,Weight) to the Inventory Structure. I can’t seem to figure out how to do that.

adding items 2

Try the syntax

Inventory[Item::AnimationName()].Quantity

Ah yes I just realized I had a pesky period in there. Thanks!

@Lucky-j @Keith_1357
I have another problem:

So the first event should do what it’s supposed to. It adds a variable to the structure with the name of the item’s animation. However, the debug “Inventory[Item.AnimationName()].Quantity” does not show the quantity of a specific item (in this event the item the cursor is hovering over) instead it shows the total quantity of all items in the inventory. I don’t know why this is and what I did wrong. The debug “ToString(Inventories::ItemCount(“Inventory”,Item.AnimationName()))” using the Inventory extension does function exactly as intended though, but obviously I don’t want to use that.

[EDIT: I moved the line that adds 1 to a separate even as I should have done originally. But still not working as it should. Busy trying to figure out why.]

(post deleted by author)

Yes, currently just trying to read or reference a variable will create the structure with a matching default value of say zero. The way GD evolves though, I don’t know if it’s safe to assume it will always be the case.

It might be safer to use the child exists condition first. If it doesn’t exist create it. If it does exist then add it.

@Keith_1357
Hi, sorry to bother you again! So the code I’ve got now will add a child each time an item is picked up that doesn’t already exist in the inventory. Another event changes the quantity variable of that child (structure). But this isn’t functioning properly. Quantity is returning with the total number of items (all children and their quantities), not the number of the specific item referenced. I have no idea why.

Somehow I stumbled on this system which works. It uses both an array and a structure.