How do I…
How do I reference specific variable information from the children of a structure?
What is the expected result
So, I want to try my hand at making a crafting system (as I wait for the next part of the official GDevelop Inventory tutorial). Thanks to a different tutorial, I’m pretty confident that the information to, say, craft a sword needs to be in variables. Example…
Condition:
The variable Quantity of Object 1 is equal to or greater than 3
The variable Quantity of Object 2 is equal to or greater than 4
Action:
Create Object 3 at X() & Y()
The variable Quantity of Object 3 = 1
Or something along those lines.
What is the actual result
The problem I’ve run into, though, is that I have an inventory structure that the items and their quantity information will be pulled from, but I have no idea how to specify what quantity is related to which item. Below is what I mean: I have a global variable structure called “Inventory” and inside that there are “SlotNumbers”, and underneath those there is a “Name” variable and a “Quantity” variable. Specifying that two of the inventory slot Names needs to be “Wood” and “Stone” makes sense to me, but how do I specify that I need “1 wood” and “2 stones”, for example?
[P.S. The events are incomplete because I hit a wall and didn’t know what to do…]
(sorry for multiple edits)
Are you increasing the SlotNumber variable? Also, it’s checking if the same slot is both wood and stone. It can’t be both. You would to use seperate subevents. One to check the wood and another to check for the stone.
This is off the top of my head, untested. There might be a simplier way like using a structure of structures. But this is my first thought using your existing method.
Create local variables
SlotNumber =0
WoodIndex = -1
StoneIndex = -1
Repeat child count times
… Inventory[SlotNumber].Name = “wood”
… Inventory[SlotNumber].Quantity >= 1
… Action : WoodIndex = SlotNumber
… Inventory[SlotNumber].Name = “stone”
… Inventory[SlotNumber].Quantity >= 2
… Action : StoneIndex = SlotNumber
… SlotNumber +1
After the repeat
WoodIndex ≠ -1
StoneIndex ≠ -1
Actions
Inventory[WoodIndex].Quantity -1
Inventory[StoneIndex].Quantity -2
As far as the hammer, you would need to either add a hammer or if you can have multiple hammers then you could check and increase an existing quantity.
Another approach would be to use the slot extension or a seperate structure that used the item as a child name.
Example
Inventory.Wood.Quantiy
Inventory.Stone.Quantiy
You could then check the quantity directly.
1 Like
Darn it! I know it can’t be both, but I didn’t think that it would try checking the same slot… Make that two related problems then…
I also just added the event to increase the SlotNumber variable.
I just have no idea how to specifically reference “x-amount of item A” & “x-amount of item B” from a structure like this. Should I be using object variables instead?
If the slot has an object with an image of the item then I guess you could use the animation name or an object variable
Slot animation name is “wood”
Variable Quantity of slot => 1
pick all slots
Slot animation name is “stone”
Variable Quantity of slot => 2
The actions would be
Subevent
… Pick all slots
… Slot animation name is “wood”
… Variable named quantity of Slot -1
Etc…
There are so many different ways to do things. Everyone has their preferences and everyone has their own knowledge level.
1 Like
Okay, thanks so much! This has helped a lot!
I feel less lost 
1 Like
I got it working (specifically adding the hammer)! It took some time to understand the “why” of your instructions, but I think I understood in the end.
Thank you again!
2 Likes