I’m new to unreal so sorry if this question sounds dumb or makes no sense but let me try to explain, I have an inventory system with pickups that are all derived from a data table and every item type has an ID. I was wondering how I would add functionality in c++ based on the item i picked up. Like for example I’m trying to add functionality to a gun so when i pick it up a gun animation shows that im holding a gun. But i want to do that only if its matching the right ID
you can think of a DataTable as a TMap<FName RowName, FMyDataTableRow Row>
so as long as you have a “meaningful” way to derive the FName RowName
from your item that exists in the world (or container) you should be able to do a MyDataTable->GetRow(FName RowName)
whether that be giving your item’s struct a FName that “just so happens” to be the same FName that is the RowName, or if the RowName is your ItemID
that part is up to you.
remember that FName must be “Exact” otherwise it will fail to resolve.
for example in one of my projects I have my items in a DataTable (all the way down to the SoftClassReference), and the FName RowName
is a combination of and int32 and a Uint8 enum (so I could have variations when I needed without stacking to much stuff into one row).
I created a FName ResolveRowName(int32 ItemId, EMod(uint8) inMod)
- it works out that
FName
has a constructor that takes in a pointer to aFString
andFString::FromInt(inInt)
, so if you could have an int32 ItemID->FName RowName with something like:return FName(*FString::FromInt(inItemID));
then to get the actual row:
bool TryGetItemFromTable(int32 inItemID, EMod inMod, FMyItemStruct& outItem)
{
if( FMyDataTableRow* row = MyDataTable->FindRow<FMyDataTableRow>(ResolveRowName(inItemID, inMod) "item Details"))
{
OutItem.SetItemByRow(*row);
return true;
}
return false
}
Hey sorry for the late reply, I’ve been trying to understand your reply and I think I kind of understand but I’m not sure what EMod and inMod is exactly? i tried copying that into my code and got an error for EMod. Also thanks for the reply
The EMod is a specific Enum in my game, you can get rid of it if your project doesn’t need it.
you will also need to implement something similar to ResolveRowName()
and it only really works if the RowName is going to be specifically numbers.
so a better example for
FName ResolveRowName(int32 inItemId)
{
return FName(*FString::FromInt(inItemId));
}
bool TryGetItemFromTable(int32 inItemID, FMyItemStruct& outItem)
{
if( FMyDataTableRow* row = MyDataTable->FindRow<FMyDataTableRow>(ResolveRowName(inItemID) "item Details"))
{
OutItem.SetItemByRow(*row);
return true;
}
return false
}
and an INLINED version saving a reusable function call would be something like
if( FMyDataTableRow* row = MyDataTable->FindRow<FMyDataTableRow>(FName(*FString::FromInt(inItemId)), "item Details"))