Why is the if statement failed? C++

Just a comment: Instead of iterating the array to find an item and adding it, if it is not present already, you can also use the AddUnique function of (T)Array.
This only adds the item, if it is not yet present and returns its index, and in case it is already there, it returns index_none.
I believe, this can make your life much easier, both, in bp as well as the c++ function.

And, as to the out of range - the root cause of this is most likely hidden somewhere in your RandomItemType function. Need to have a look at this, I found, that you did post this one and that the out of bound is gone…

What’s in the RandomItemNumber C++ function (Random Number Item in BP) ? Is it just a random int in range?

1 Like

the Random Number Item function looks like in CPP, and yes it is just generating a random number between 0 to 99

void AMyGameMode::RandomItemNumber(int32& Number)
{
	TArray<int32> Arr{};
	TArray<FName> OutRowNames{};
	(OutRowNames).Reset();

	if (const UDataTable* DT_ItemNumberProbability = LoadObject<UDataTable>(NULL, TEXT("/Game/DataTables/ItemsGeneration/DT_ItemNumbarProbability")))
	{
		OutRowNames = DT_ItemNumberProbability->GetRowNames();
		for (auto& Array : OutRowNames) 
		{
			if (const UDataTable* DT_ItemNumberProbability{ LoadObject<UDataTable>(GetWorld(), TEXT("/Game/DataTables/ItemsGeneration/DT_ItemNumbarProbability")) })
			{
				if (const FST_ItemNumberProbability * OutRow{ DT_ItemNumberProbability->FindRow<FST_ItemNumberProbability>(FName(Array), "") })
				{
					if (OutRow)
					{
						NumberProbabilityDatas = *OutRow;
						for ( int32 i = 1; i <= NumberProbabilityDatas.Percent; i++)
						{
							Arr.Add(NumberProbabilityDatas.Number);
						}
					}
				}
			}
		}
		//Randomize range between 0 to 99 , set return to Number
		FCustomThunkTemplates::Array_Shuffle(Arr);
		Number = Arr[FMath::RandRange(0, 99)]; 
	}
}

and in bp looks likes this

I’m a bit lost what is the end result you want.

1 Like

end result of the function GenerateItems()?

can you comment this line of code and re-test?

1 Like

sure and now it works and I have this result as I want


all the portions are created with random type of groups and random items in it by random numbers.

but now I don’t have the loop to check if the items are duplicated

1 Like

nice, your problem is you have infinite loop

this adds the same duplicate items always, investigate it.

hope it helps
cheers!

1 Like

thank you for identifying this, and how to investigate?

follow few steps:
1, how you are restricting the duplicates? by number, by location, by name etc… ?
if by number, check if you have duplicate numbers already in the array
if by location, check if you have duplicate locations stored in the array
if by name, check if you have duplicate names in the array

any of them you have twice, your loop will generate duplicate of it and it will be re-stared as infinite loop.

hope it helps
cheers!

1 Like

its mean I have to dig in my data tables from where all the data is derived, will update here if I found it


I am restricting the duplicates on locations stored in the data table of 6 rows, and 6 portions of random item groups are generated on this bases.
I don’t have any duplicated location in these rows, so why is the loop is infinite while it should not be?

While looking at the code, I think, the Shuffle in your BP/C++ is not needed, as you already use a random index to read a value, so the shuffle just randomizes an array, from which you plan to read at a random location, thus only creating overhead.

1 Like

Thank you for the tip, I will get rid of shuffle if this is not needed )

It could simply be a memory/ram issue, and unable to complete the number of iterations required… test with 2 rows, 2 item types . . . and check if it solved the infinite loop

hope it helps
cheers!

1 Like

now the engine is freezing, and I have to close it under visual studio by clicking stop debugging :tired_face:

Remember if the engine is freezing its mean the loop is still infinite.
try one more thing, clearing the array each time the group of items is generated, test and update us about the results

hope it helps
cheers!

1 Like
if (ItemsTypeArr.Find(LocItemType) == -1) //check for duplicates
	{
	 ItemsTypeArr.Add(LocItemType); // add all items to array if not duplicated
     ItemsTypeArr.Empty();
	}

this way?

nah, not that way… fix few errors in your code to make the array be able to clear properly.

1: your loop should have a body and you should call RandomItemType(LocItemType); in this loop, if you call outside of this loop, it makes loop useless.

2: this should be called under the for loop , in step one I explained.

when this loop finished call to make it clear,

ItemsTypeArr.Empty();

hope it helps
cheers!

1 Like


now the engine triggers a breakpoint :tired_face:

If you could share the .h and the .cpp, perhaps it would be easier to help.

On another note: when things get so tangled might be a good idea to start over… but thats just me. Lol

1 Like