Put item DIRECTLY into inventory (SOMEWHAT SOLVED)

Greetings forums,

I’m in a bit of a pickle right now with my inventory reference.

I made a POC of the inventory example, but modified a bit to match what I want from my game. It’s quite simple, the pink character walks over one of the items (The potato, the poop, and the banana), and it will appear in the inventory, finding the first empty slot, and displaying the correct amounts.

This is where I hit a roadblock. I’m trying to add a mechanic that directly adds one of these items straight to the inventory, without having to pick it up. In this example, clicking the red button should give the player 1 potato (Named “poto”).

When a slot is already occupied by a poto, it works just fine. But if poto doesn’t exist in the inventory, it does not work correctly, instead adding a banana to each empty slot every time I press the button for some reason.

Here is the code I use to add items to the inventory by touching them:

(This second screenshot is what’s inside the include link)

And here’s the code for trying to add items to the inventory directly (When the button is pressed):

I feel like I know what’s wrong with my code here, but I have no idea how to approach this issue. Can somebody please help me figure out what changes I have to make to be able to add items to the inventory via touching the item AND pressing the button? I can send you the example if you need it.

And help is appreciated. Thanks

What’s the subevent doing?

How does it know which inventoryitems item to use? Add a condition to the first repeat, that checks inventoryitems.VariableString(name) = VariableString(pickeditem) That should get rid of the banana always getting used.


The new code prevents the animation from changing to it’s corresponding item, even when picking items up manually. When I include the disabled code bit, it just causes the same problem with the banana

When I press the button to add an item directly, it’s not referencing the item group, but instead, just choosing the item’s name and adding it in, which I believe is causing the problem since this code is checking for the last item I manually picked up (According to debugger, the item still exists in the inventory, but the animation and amount is not shown until I manually pick up an item).

I feel like I know what the problem is, but I have no idea what to do to fix it

Why are you looping through all invslot instances, but not linking it to the objects in the condition? How does GDevelop know which invslot top use?

You need to include the invslot.itemId = pickedItem that you disabled in the event before the repeat.

Alright, I shut off the disable. The problem still persists though.

I did some testing, and the banana is somehow the problem. With this current code, pressing the button adds a poto to the inventory as normal, but the moment I collect the banana manually, it will reveal that the same amount of bananas were secretly added to the inventory as well. How can I make it so the code prevents adding the extra item?

No, that won’t do anything.

Think about what you’re trying to do, and what the events are doing.

This image
has no effect on the repeat. It just filters a list of inventoryitems for that event and any of it’s sub events. In the next event (which is the repeat), the inventoryitems list s reset to all those in the scene.

You need to include the above condition in the repeat’s conditions, so it’s with the inverted pickeditem = “” (btw, instead of inverting the condition, you can just use not equal to).

Alright, I added the code back to the repeat and changed up the code

When the button is pressed, it adds 1 poto as normal, but every time afterwards, it adds 2 potos for some reason. Also, if there is anything else in the inventory, it adds the poto to another empty slot with the same amount, which shouldn’t be happening.

It also still adds a banana.

I’m going to try and pinpoint the issue in the meantime, but if you see something I missed, please let me know

Looking at this a bit deeper, it needs a refactor.

Here are the initial events that check if an item has been touched. Note the delete has to come last, otherwise you are referencing a deleted item in the linked events :


Next is the external event additemtoinventory . The method you’ve used to pick an empty slot (in order) isn’t ideal, but is easiest to understand (a more correct way would require more set up). So I’m sticking with your method to avoid confusion. I haven’t tested this bit of code, but I think it should be ok :

What it does is adds the picked item to the inventory, then checks if the picked item is in invslot, and adds it if not. Finally it finds the invslot for the picked item, and updates the displayed count.

Woa, this code sure is a lot more compact, and it works just like the old code did!

It almost works! It now adds a poto to the inventory directly even when a slot is empty! No more hidden bananas either! I tried this out with the other items too (Changing the button’s variable to give a different item), and that works too!

Although, notice how I said it almost works. There’s a new problem now.

Once I have all three current items in my inventory (The potato, the poop, and the banana) (ONLY when picking up the items manually BTW, I don’t know why it’s so specific), pressing the button no longer increments the counter, not even in the debugger. This only fixes itself once I change scenes, which isn’t ideal.

Perhaps there’s an issue where we forgot to reset one of the variables before the button is pushed?

I’ll tinker around, but please get back to me when you have the chance.

Here’s a screenshot of the new code just in case. It’s mostly the same as the one you provided

Edit: Turns out that the very first repeat event “quantity” was the likely culprit (Still needs a bit more testing, but it’s boiled down to that). Is there any way to make sure I can input certain quantities, or did I type it wrong?

Use “Repeat for Variable(pickedcount)” instead of inventoryitems.Variable(quantity).

Does that external event get called up from anywhere else? Because it assumes the variable picketitem and pickedcount have values in them

Also, have the events for the button click been updated?

Yeah, the external event is called here (Picking up the item manually),

…and here (Clicking the button to get the item directly. Button variable has item’s name btw).

I’m planning on turning this “add to inventory” mechanic into a function (Somehow), but for now, this is what handles putting items in the inventory

Have a look at my first screen snip on how to get the pickedcount on player/item collision.

And the problem with the button click calling the external event is that inventoryitems isn’t set. So who knows which one it picks out.

Do you have a quantity variable on the button? If so, use that to set pickedcount.

If not, put an extra condition on the left mouse button was released that reads "The text of variable name of inventoryitems = button.VariableString(item), and set pickedcount as per for the player/item collision a couple of posts back.

Okay, I added the condition in

And I changed the repeat event back to pickedcount

Unfortunately, this prevents items from being added to the inventory at all. Should I just go with the quantity variable for the button, or should I make a function that defines a quantity before passing the information to the external event?

In the event where you set pickeditem, you also need to set the variable pickedcount. Otherwise, as you’ve noticed, nothing gets added.

It’s up to you whether you use a variable on the button or some other method. Which ever you feel most comfortable with.

Like so?

I tested this out, and it actually works, both for the button and picking up items. Thanks.

However, for a larger scale game, one of my fears is that if I have other ways of adding items directly (Through NPC dialogue, through completed quests, etc), hard coding stuff like this might become less and less ideal, and I’ve already lost one of my projects to bad coding practices before.

I feel like I want to try and make this more reusable, but I don’t know how. I tried making a function call that adds the item name and quantity to reference the inventory, but it didn’t work

Did you try creating a function that took 2 parameters - item and quantity - that then added the quantity number of items to Inventory?

Here, I’ll show you the function

Here’s the new code for the button, trying to call the function (Here, I’m trying to put 3 potos in my inventory when I press the button)

When testing this function, nothing happens when I press the button. I press the button over and over, but 3 potos never get added to my inventory. Surely there’s something I did wrong, but I’m totally stumped at what it could be

If we can get this thing off the ground, I should be able to just call this whenever I need items directly added to the inventory

You can’t refer to the function arguments like that. Use
GetArgumentAsNumber("quantity") instead of “quantity” and GetArgumentAsString("itemName") instead of “itemName”.

See the wiki on functions.

1 Like

Looking at it a bit more closely, I’m not sure it’ll work. I don’t think a function can see or access any of the objects in the scene unless they have been passed as a parameter. So it won’t update the invslots in the scene unless you pass them as a parameter.

I’d suggest you start a new thread on this, and ask the community for help or suggestions. I haven’t dealt with functions enough to understand their nuances.

Okay, I have confirmed that the new code doesn’t work.

I’ll go start another thread of this. Meanwhile, you still helped me get the button working, and cleaned up my code a bit.

Thanks for that ^^

1 Like