Spawn Actor From Data Table FilePath

Hey mates.

I am trying to spawn an actor by reading it’s filepath from a Data Table file . How to approach this ?
Basically if player drops an item from character inventory it will be spawned in front of the character and what item to be spawned will be read from Item Data Base file. ( This is done in Game Instance)
I got the spawning part alright with SpawnActor<>. But how do I get a reference/filepath from a Data Table and use it to set as spawn template ?

So far here is a bit of code and screenshots to better explain matter. Code gives no errors.
APickUpBase is just a class I derived from AActor. It does not do anytihng. Just a visual represantation of item to be picked up in the game world.
Thanks

SPR7GameInstance.h

// Trying o use this to spawn dropped item
    UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
    TSubclassOf<AActor> ThingToSpawn;

SPR7GameInstance.cpp

void USPR7GameInstance::DropItem(int32 IndexToDrop)
{
    //Getting the reference/filepath as a FString
    FInventoryItem DroppingItem = Inventory[IndexToDrop];
    FString ItemAdress = DroppingItem.PickUpBaseFilePath;


    // Get player location and rotation, SpawnInfo
    FRotator Rotation = GetWorld()->GetFirstPlayerController()->GetPawn()->GetActorRotation();
    FVector const Location = GetWorld()->GetFirstPlayerController()->GetPawn()->GetActorLocation() + GetWorld()->GetFirstPlayerController()->GetPawn()->GetActorForwardVector() * 100;
    FActorSpawnParameters SpawnInfo;

    // Now how to make ThingToSpawn read the filepath so it can be used below ?
    APickUpBase* DroppedItem = GetWorld()->SpawnActor<APickUpBase>( ThingToSpawn, Location, Rotation, SpawnInfo);



    //Finally remove it from inventory
    RemoveItemFromInventory(IndexToDrop);

Data Table For Items

You need to make a pointer to your datatable exposed to Blueprint so you can select the correct datatable

UPROPERTY(EditDefaultsOnly)
class UDataTable* MyDataTable;

then in your code:

if (MyDataTable)
{
    FMyDataTableStruct* MyDataTableRow = MyDataTable->FindRow<FMyDataTableStruct>("RowNameHere", "ContextString"); 

   if (MyDataTableRow)
   {
            MyDataTableRow->Class
   }
}

If you want to get all rows you can do the following:

if (MyDataTable)
{
			TArray<FMyDataTableStruct*> OutRows;
			MyDataTable->GetAllRows<FMyDataTableStruct>("ContextString", OutRows);
			for (FMyDataTableStruct* Row : OutRows)
			{
				Row->Class;
			}
}

Hope this helps

1 Like