Hey everyone, I am making a survival game. I have a structure array for inventory items. I basicly want that the player can use a bottle of water 5 times. So I declared a variable in my structre. I want to -20 when the bottle used.
FItemData ItemData;
if (ItemSubClass)
{
if (AItemD* Item = ItemSubClass.GetDefaultObject())
{
Item->Use(this);
float Usedd = ItemData.UsedSca;
Usedd -= 20.f;
}
}
there is my code. but i can only change the defaults of the variable, how can i change the structre members “UsedSca”
You need to access an exact structure from array, not to create a new one.
For example, create an int variable inside your inventory class (or character, if you creates inventory logic inside character) with name… for example, UsedItemIndex and save to it an index of selected item. After that, then you use youe item, just access an array element and change or chech it values.
For example:
void UInventory::GetItem(AItem* Item){ // Item is an item you want to move to inventory
FItemData NewItem; // create a new structure
NewItem.SomeValue = Item->SomeValue; // set data from Item
//repeat for each required variable
InventoryArray.Add(NewItem) // save new structure to class variable TArray<FItemData> UInventory::InventoryArray;
}
void UInventory::UseItem(int Index){ // Index is an index of selected item in array
if(InventoryArray.IsValidIndex(Index)){ // if index OK (prevent segfault)
InventoryArray[Index].SomeValue-=20.f; // change value of structure of used item
}
}
I didn’t take into account your code because I wanted to focus on changing a struct value of used item.
About your code, notice that:
float Usedd = ItemData.UsedSca;
Usedd -= 20.f;
creates a new variable Usedd and copy value from ItemData.UsedSca, after that changes value of Usedd, not ItemData.UsedSca. You can change struct value in this way: ItemData.UsedSca -= 20.f;