Iterate on DataType item from CSV imported file with C++

So first I Import my CSV file into editor, here is a sample of it:

,itemNumber,xpos,ypos,zpos,xrot,yrot,zrot,itemType,option01,option02,option03
0,0,1,1,0,0,0,0,wall,0,0,0
1,1,2,1,0,0,0,0,wall,0,0,0
2,2,3,1,0,0,0,0,wall,0,0,0
3,3,3,2,0,0,0,90,wall,0,0,0
4,3,3,2,0,0,0,90,wall,0,0,0

Then define this line to get the CSV data into my code:
.h file

UDataTable* ItemData;

.cpp file

static ConstructorHelpers::FObjectFinder<UDataTable> temp(TEXT("DataTable'/Game/Data/ItemData.ItemData'"));
ItemData = temp.Object;

And everything looks fine. Then I try to iterate through the items. And please jhelp me, not sure how to do it…

if (ItemData != nullptr) {
 //// ????????????
}

As mentioned in the thread, if you want to iterate through all the items, there’s a few ways to do it, the easiest is probably this way:

if (ItemData != nullptr)
{
   TArray<FItemInformation*> OutAllRows;
   FString Context; // Used in error reporting.
   ItemData->GetAllRows<FItemInformation>(Context, OutAllRows); // Populates the array with all the data from the CSV.
  for(FItemInformation* ItemInfoRow : OutAllRows)
  {
      ItemInfoRow->itemNumber; // do whatever with your data...
  }
}
1 Like

Many thanks … : ) , … it does the job and things works fine.

UE_LOG(LogTemp, Warning, TEXT(" Type is  %s on y position:  %s"), *ItemInfoRow->itemType.ToString(), *ItemInfoRow->ypos.ToString());

output log:

LogTemp: Warning:  Type is  wall on y position:  1
LogTemp: Warning:  Type is  wall on y position:  1
LogTemp: Warning:  Type is  wall on y position:  1
LogTemp: Warning:  Type is  wall on y position:  2
LogTemp: Warning:  Type is  wall on y position:  2

NOTE: If someone wonder of what is “FItemInformation” data type in the solution,
I already made a c++ class based on DataTable called “FItemInformation” and define same structure variables inside it`s .h file:Then #include “FItemInformation.h” to my main file.

pragma once

#include "CoreMinimal.h"
#include "Engine/DataTable.h"
#include "FItemInformation.generated.h"

USTRUCT(BlueprintType)
struct FItemInformation : public FTableRowBase
{
GENERATED_USTRUCT_BODY()

public:
FItemInformation() {

}

UPROPERTY(EditAnywhere)
FText itemNumber;
UPROPERTY(EditAnywhere)
FText xpos;
UPROPERTY(EditAnywhere)
FText ypos;
UPROPERTY(EditAnywhere)
FText zpos;
UPROPERTY(EditAnywhere)
FText xrot;
UPROPERTY(EditAnywhere)
FText yrot;
UPROPERTY(EditAnywhere)
FText zrot;
UPROPERTY(EditAnywhere)
FText itemType;
UPROPERTY(EditAnywhere)
FText option01;
UPROPERTY(EditAnywhere)
FText option02;
UPROPERTY(EditAnywhere)
FText option03;

};

UCLASS()
class NETWORKDIRECT_API UFItemInformation : public UDataTable
{
GENERATED_BODY()

UPROPERTY(EditAnywhere)
FItemInformation ItemInformation;

};