I created inventory and adding items to inventory into CPP. Now i have problem. player has 2 types of inventory. The 1st is inventory, witch includes using weapons, i mean weapons, witch Player can change by spinning the mouse wheel. The 2nd - inventory, witch includes weapons? that was taken by player and isn’t used by him. I wanna to make mechanic, that make items, if LMB pressed on them, to attach to the cursor and if LMB doesn’t pressed on them? items checked? is under them another grid, and is under them slot-place? they set on this slot. I heard it must be made by Drag & Drop, but i didn’t find normal tutorial about this mechanic . Can u explain to me, how can i do it using CPP, or give me a course? that includes this themeб or vidio, or any article? thank se for response
that is a big one, and more along the lines of a tutorial then a Q&A, mechanics like this do not materialize fully formed, and will have several iterations.
I would hope that these 2 inventories are holding the same ItemType
- if not then you will need some interchange for the type conversion
- swapping items between 2 lists means accounting for 2 different lists, and maintaining proper indexing between the 2 of them, and I hate it just thinking about it.
you can also accomplish this with only 1 list for the Items, and then a second list of indexes (for small number of Equip slots you could do this with floating int32
but that would be manually traversed while a TArray<int32>
can be iterated over in like a for-Loop ) to the item-List. This would probably mean your Items would have members for like bIsEquipped
and maybe even some kind of enum for EquippedAt
this way you only need to signify that this item needs to be swapped with this other item, and it can be accomplished by just passing 2 indexes to like an Inventory-Manager ActorComponent
and the Inventory-Manager is more or less a wrapper around a aTArray<InventoryItem>
and TArray<int32>
with a host of helper functions for like AddItem(InventoryItem& inItem)
and EquipItem(EItemSlot inSlot, int32 itemToEquip)
and instead of stepping through the entire array you would jump to the indexes in the Equipped list (you would still want to validate the list every so often as inserts, removals, and Sorts happen)
for the UI_Widget integration you would step through the Array skipping the items where bIsEquipped==true
and putting them into the grid (displaying the ones you skipped separately)
for picking up items to move them focus first on throwing them from one slot to another, and you could presume your players will understand “Highlighted item is selected” or you could put a graphic over it.
to look for tutorials/guides try searching “unreal Engine C++ Inventory” you could add “5” into the search as well, but anything from 4.27 and up should mostly be applicable.