How to check for specific items in my inventory ?

Hi there,

i followed the UnrealEngine Tutorial on how to make an inventory.

Everything works fine as shown and i really want to expand on it but i don’t know how because im a noob.
What i want to do is, at a door, check if the player has 4 Keys in the inventory and only then open it.
I was thinking about a check inside the interactable door blueprint but i don’t know how to do the itemcheck with all the arrays and blueprints or access the inventory.

Thanks !

how about a for each loop that checks for a specific thing.lets say your key is an actor in this case and your inventory is an array of actors and you need 4 keys. you could have a for each loop that cast each element to the actor type key and if it succeeds then you increment a variable and if fail do nothing. then when the loop is done just check the value of the variable and it will tell you how many keys you have.

Also if you wanted this script on the door bp then all you would need to do is to cast to the player character at the start and off the cast get the inventory.

Hi ThompsonN13 ,

much thanks for your detailed answer, i really appreciate that !
Unfortunately i didn’t make clear what i want to do, sorry.
I want to use 4 different keys which are all different item actors.

At the door, i want to check if the player has all of the keys and then open it.
Anyways, the for each loop has already become much more understandable through you answer ^^

By the way, is it true, that the foreachloop to the Cast To Key only increments the Numvalue for each KeyActor ?

I didn’t know that was possible

So i found out that you can check if an array contains an item. So i ended up trying this inside a cubeactor for testing purpose:

The problem i am having is, that i do not find a way to reference each key from the level to make that InventoryStruct to check for.

if you want to use different actors for this its not too hard to do either. you just need to compare the inventory against a list of keys. to do this we will use the find node which takes in a actor reference and returns its index in the array, it should also return -1 for items not in the array. so working from here we just do as i did before and increment a variable when we find one of the keys. once we are done with the loop we just compare how many keys we were searching for to the variable and that will tell us if we found them all.

note i try to make things as modular as possible so the same blueprint can be reused over and again. you could accomplish this without the loop and just use a series of branches like you have pictured but then you are limited to only having 4 keys whereas my method you can have as many as you like.

in your picture i see your using an array of structs and a contains node which may be a little problematic. while i havent tried your implementation, based on my experience the issue i would forsee is that generally when trying to do something like that you need to fill in all the fields of the make struct and they need to perfectly match to the struct in your inventory. so the value of item, item thumbnail, pickuptext, and action text need to be exactly the same as the values stored in the inventory. to combat this you may want to look into using a data table to store the values of things then just searching for the DT values.

Hi again and thanks for your quick answer.

I will try your solution but do you also know how i could get my attempt to work ? How do i get the keyreferences in there ? This could be helpful in future.

Thanks !

Nevermind, i just saw you already gave the answer :smiley:

basically your just camparing data here. so you are comparing the struct information contained in the inventory with the information in the datatable and if they are the same it sets the bool to true. so the easiest method to have them be the same is to just get a reference to the datatable in the construct script of the item pickup and set the value of the struct within there. i dont have time at the moment to go into details but thats the basics.

Ok so i created a Datatable for the keys with my InventoryStructure, how do i link the keyactor to the
datatable ?

Because at the moment, i dont see how the actual keys are in correlation to the datatable data.

Okay, no problem.

I dont really understand how to set the value of the datatable row keys because in the event pickup i only get “getter” nodes.

It also confuses me, that in the DataTable, you can not set an actor. So when i am checking if the Inventory Array contains the “Row Name” it basically can never return true because it is missing an actor in the comparison.

At least that is my point of understanding.

ok so in the tutorial in [section 8][1] at about 22:30 you set the values for the item manually. so now instead of setting random values you set the values from the data table as shown below. then when you copy the values to go into the inventory array they will be copies of the same data and will match.

now im not exactly sure what you mean when you say “set the value of the datatable row keys” if you mean when do you fill in the data fields then that would be in the datatable asset itself, if you mean you want to set those values at run time then you cant actually do that.

honestly you dont really need the actor value since you wanted to create separate actors for each key. the only time you would need something like the actor is when you were looking to add more than one item of the exact same type, for example if you wanted to add 3 of key1, then you would possibly need a way to differentiate them.

if i were personally building this type of system i would have either used an array of actors or one master actor with attributes filled from the table.

okay, so besides the keyitems, i would like to use different items of one actor. for example health pickups and batterys ect. so it would be nice if i could keep that system.

so ive been wracking my brain trying to figure this out for awhile now and i think i may have figured it out. instead of the actor object reference you want to use a actor class reference. this way you still can use the actor bit of your struct but instead of saving the specific reference of the item it will save a generic version of the item. this should work though you will need to use a add unique node instead of the basic add node.

Hey man, i really appreciate your effort on my problem, that is amazing !

I will try what you said, thanks a bunch !