How can I make Expirable Items in slot-based Inventory?

I made an Inventory and two Chests that the player can use to store items around the map. It is completely structure-based, has a Default Slot and five Slots in the inventory that are duplicates of the Default Slot.

How can I make the Items Expirable? I want a progress bar to decrease in the slot when it appears, and remove itself from the inventory or chest when the meter reaches 0. I have done things like these before but never through a Structure System and I am bit lost.

You could have a lifetime variable, also possibly part of the inventory item structure. It’s the number of gametime seconds the object is allowed to exist for.

Where precisely you implement the deduction is up to you. It might be in the level BP or player BP or elsewhere, wherever is make sense.

In the BP just have a SetTimerByEvent which decrements the life variable ( once a minute maybe ). Once it reaches zero, the item gets removed from the inventory. You might want to use GetGametimeInSeconds or just use the timing from the SetTimerByEvent node.

I managed to do what you describe, only problem is when I pick up two expirable objects, the progress bar resets and they both run together from the top again. Any idea what I’m missing?

You have to write the code to adjust the expiry time on each item separately, as you go around the loop. ( maybe you are giving both items the same time? )

Otherwise, can you put your code up here?

The only way I could think of doing it is spawn an invisible actor called Destroyer in the level. In the picture this is the function I use in a timer that runs 3 times a second. I attempted it like you see it as well as with getting a reference to the index on its event begin play (index coming from the last item that was added in the inventory). However, both ways reset the first object’s progress bar to the new one’s. At some point, I made them reset to the first object’s progress bar (the depleted), but that’s it. I tried with message event, still the same. Also, some items MUST have the same time as they are the same as Items. I want the expirable Items to not be stackable so the player can see their Lifetime Progress Bar (however in the video I tried separate items and separate times and still the same). I’m confused…


I had to set some references I hadn’t and now it works with the Destroyer! Each Item has its own decreasing Progress Bar. However, would there be a more efficient way to do it? Something that will help with saving Item progress bars when changing levels? The Destroyer method seems like a hack-method to me, do you know of a way to do it more correctly?

I think the destroyer method is correct, it’s just a process that decrements life units… :slight_smile:

If you wanna save the progress when moving between levels, then you also need to store a copy in the game instance.

Bit busy at the mo, but will take a look at this code a bit later. It does look a little complex :slight_smile:

Ok, looked at it better. It’s nice and clear. The thing about structures is you will always be breaking them open and packing them back up again. So you get really elegant, minimal code in some parts and then spagetti junctions in other parts.

Nature of the best I’m afraid.

If you want to avoid this kind of stuff then you have to do anything with an array of ints :slight_smile:

Array for health, array for each kind of ammo, array for blah blah blah. But the downside of that is the code is not as clear because the only differentiating concept you have is the name of the array.