Why my GetTableRowNames failed?

Can anyone correct me What I am doing wrong here in this logic?

if (MyRowNames_Acc) //This fails, and why?

case EItemType::E_Accessories:
{
	if (const UDataTable* OutRowNames_Acc = LoadObject<UDataTable>(NULL, TEXT("/Game/DataTables/DT_WeaponAcc")))
	{
        MyRowNames_Acc = OutRowNames_Acc->GetRowNames();
		if (MyRowNames_Acc) //This fails, and why?
		{
			for (auto& ArrayElements_Acc : MyRowNames_Acc)
			{
				ItemTypeAndID_Acc.Type = EItemType::E_Accessories;
				ItemTypeAndID_Acc.ID = FName(ArrayElements_Acc);
				Arr.Add(ItemTypeAndID_Acc);//Adding elements
			}
		}
		else {print100s("Cast of Weapon Item Datas Failed: Line 339 :: Class MyGameMode"); return; }
	}
}break;

Your MyRowNames_Acc should be of type TArray<FName>, so did you set this one correctly?

If so, this is already an array, you do not really need to check for ‘if (rownames)’. A check for the array size could be done, I added this below: ‘rownames.Num()’. But it is not needed, as the iteration in the for loop will handle this.

Here’s some working example, just printing the row names…

LoadingScreenDatatable = LoadObject<UDataTable>(NULL, TEXT("DataTable'/Game/Henry/DataTables/DT_Test2.DT_Test2'"));
if (LoadingScreenDatatable)
{
	TArray<FName> rownames = LoadingScreenDatatable->GetRowNames();
	if (rownames.Num() > 0)   // not really needed to check!
	{
		for (auto& ArrayElement : rownames)
		{
			UE_LOG(LogHerb64, Display, TEXT("Rowname: %s"), *ArrayElement.ToString());
		}
	}
}
1 Like

yes I have it as TArray<FName> rownames

if i don’t check it for nullptr, the editor just crashed with error Array has changed during ranged-for iteration! …

got idea from line in your post

and i changed mine too
("/Game/DataTables/DT_WeaponAcc")))

should be

("DataTable'/Game/DataTables/DT_WeaponAcc'")))

Thank you very much for solving the issue :slight_smile:

Yeah, it is a good idea to get the reference string using the Right Mouse Button menu from content browser and paste this into the code:

this resolves to

DataTable’/Game/Henry/DataTables/DT_Test2.DT_Test2’

This “Array has changed…” error happens, if you change the array content while iterating over it. I looked at your code - indeed, you add items within the for loop.
You could try to iterate with a for (i =…) loop to avoid these issues.
See this forum thread.

1 Like