[Solved] My inventory. How do I improve it?

Hello. I am experimenting with a new way of setting up the inventory in my game that doesn’t rely on the unconventional use of an extension, as it currently does. My prototype (below) creates a row of items based on what the player has collected in the game world. It works, but it is long-winded and unwieldy. I struggle to get my head around ‘for each’ loops, but I think that is probably the key to making my inventory neater and more concise. I’m hoping for advice on how to improve my events. Replies from users with more advanced knowledge of GDevelop are much appreciated: folks who know how to use and manipulate loops and instances. I’d love to make the leap from long-winded events to more concise ones, but that moment doesn’t seem to be happening from studying examples or reading the Wiki. Many thanks.

How it works now: An object is created in an arbitrary location and its animation is set. I assign an order number to the instance and an id (not used yet, but I plan to). The order number is based on a simple counter that is added to each time an item is created. Once all items are created, I run a series of events to order the items neatly in the scene without gaps. (there are 5 items in the prototype, but I cut the screenshots down to three)

image

Here’s one way. For every child loops through a scene variable. It can be an array or structure. The first parameter is the scene variable name. The 2nd is the child and the 3rd parameter is the optional child’s name. It works a lot like a for each object except it gets the value, it doesn’t create a sort of refence like the object version. The 3 parameters can be any variable name. It helps to declare or set them up in the variable screen.

.
scene variables

You can use this for array or structures of structures just use the variable like child instead of the variable name. Although, unlike for each object you can’t change the value by using child. You need to use the variable name. Although, you can use the child’s name to point to it,

For example, this would number the children:

Afterwards:
image

If it was a structure of structures or an array of structures then you could use child (dot) the child’s name like child.count.

console output (debugger)
image

Variables:

To change the values, you could use:

console output: An x is added to the color and the count is multiplied by 10.
image

1 Like

Thank you for taking the time to put that together. The screenshots are helpful and appreciated. Upon first read there’s a lot I don’t understand because I haven’t used these techniques before. But I can experiment with the things you have shown and hopefully have some “eureka!” moments. Up to now I have only used ‘top level’ variables (if that’s the right phrase), and perhaps a few structures with only 1 level of children, so referencing deeper variables is something I haven’t done or would know the syntax for yet. If there’s one thing I want to achieve next year it’s going from “amateur who gets things done but takes the long way around” to “intermediate person who can do things more efficiently”. Thank you very much.

2 Likes

I’m not sure that I correctly understood what kind of inventory system you are aiming for, but I just made one as an example - it is not very elegant, but it is effective and allows you to create inventories of any size that are multiples of N, which may be useful.


Inventory_example

1 Like

Thank you for putting that together. My inventory is much simpler than your example, and would just fold out in a row of maybe 5 items. It would not need to be interactive. It’s more like the inventory in Life is Strange, which simply shows the player that they have a CD, a camera, a can of cola etc. but doesn’t do much more than that. My game will be keyboard/controller, and not mouse controlled. I saw the GDevelop inventory example, which is more like your example, but couldn’t work out how to simplify it for my little inventory, so I started from scratch.

This is the one in my main project, but it uses an extension in an unconventional way and isn’t at all dynamic, hence coming here today with a new-but-basic prototype. Both my main and new inventory work but I want to do things much better.

image

I will study the prototype that you kindly put together (I love your icons!) and try to understand how it works. It’s helpful for me to do that, so you didn’t spend time on it for nothing :slight_smile: It’s very helpful and very appreciated.

I hope you don’t mind the additional reply. I just wanted to let you know that I have experimented using your suggestions and had my “eureka!” moment :slight_smile: Thank you very much for helping me understand ‘for every child’. My inventory now only uses 1 event instead of 10. Brilliant! I might even make a tutorial about it, since several people have asked me how to make this kind of simple inventory on my yt channel. Thanks again.

1 Like

You’re welcome. I don’t mind the replies. Glad to help.