Hello there. I’m following a tutorial from the YouTube, a guy make it in blueprint and im trying to make a copy of it in c++. He created FunctionLibrary Blueprint with getting data row like this:
And later on, he uses it in a blueprint actor in construction like this:
So he calls to data table on construction right?
I tried to make it in c++ but it’s not working and i dont understand why. I followed a guide about making one file for data tables like this:
DataTables.h
class TURNBASERPG_API UDataTables : public UObject
{
GENERATED_BODY()
public:
UDataTables();
static UDataTables* GetInstance();
TArray<FGridShapeData*> GetGridShapes() const;
UDataTable* GetGridTable() const { return GridTable; }
void SetGridTable(UDataTable* Value) { GridTable = Value; }
private:
static UDataTables* Instance;
UPROPERTY()
UDataTable* GridTable;
};
DataTable.cpp
UDataTables* UDataTables::Instance;
UDataTables::UDataTables()
{
static ConstructorHelpers::FObjectFinder<UDataTable>
GridDataTable(TEXT("MyPathThatIWantedToHide, it works in other places"));
SetGridTable(GridDataTable.Object);
}
UDataTables* UDataTables::GetInstance()
{
if (Instance == nullptr)
{
Instance = NewObject<UDataTables>();
}
return Instance;
}
TArray<FGridShapeData*> UDataTables::GetGridShapes() const
{
TArray<FGridShapeData*> GridShapes;
GetGridTable()->GetAllRows<FGridShapeData>(TEXT("Test"), GridShapes);
return GridShapes;
}
Then i created FunctionLibrary.h and .cpp (i think it’s only important to paste a function from .cpp):
FGridShapeData* UFunctionLibrary::GetGridShapeData(const EGridShape GridShape)
{
const UDataTables* Instance = UDataTables::GetInstance();
TArray<FGridShapeData*> GridShapesData = Instance->GetGridShapes();
for (FGridShapeData* GridShapeData : GridShapesData)
{
if (GridShapeData->GridShape == GridShape)
{
return GridShapeData;
}
}
return GridShapesData[0];
}
And finally I made a GridModifier actor where i want to use this GetGridShapeData in OnConstruct (like blueprint guy did) .cpp:
const FGridShapeData* GridShapeData = UFunctionLibrary::GetGridShapeData(Shape);
StaticMeshComponent->SetStaticMesh(GridShapeData->Mesh);
But it crushes and gives me UE error “EXCEPTION_ACCESS_VIOLATION”, why is that? Is DataTable not ready yet? I used this function in blueprint in different place and it was working correctly.
Can i somehow skip first few seconds until everything is loaded so i can use it? I would like to change those variables in editor and the blueprint would change accordingly (like changing a color and or smt like that), when making pure blueprint it works, but in c++ it doesnt, what am i missing?