How to add int+int while an int is array?

Hello, I want to do something like this BP but in C++

what I am trying is:

if (OutRow)
		{
			for ( int32 i = 1; i <= NumberProbabilityDatas.Percent; i++)
			{
				Arr.Add(NumberProbabilityDatas.Number);
			}
		}

if OutRow is the struct, you need OutRow.Percent or OutRow->Percent if it’s a pointer.
Anything else specific?

1 Like

yes OutRow is a struct and in blueprint I am getting add copy from the Arr variable and adding OutRow.Number in it, but the problem is I don’t know why I am incrementing i++ in for loop while I don’t see any incrementation in the blueprint script.

The First Index > Last Index inputs of the blueprint for loop are equal to how int i is incremented in a c++ loop.

In the blueprint nodes it looks like you are looping from 1 to X, but the original struct ST_ItemNumberProbability remains the same. This means Percent will always be the same and Number will always be the same. You are not using the Index of the for loop for anything. The result is that the array Arr will contain a bunch of the same int32 values (all copies of Number). This does not make sense.

1 Like

this is goal I want to achieve.
after SHUFFLE the for loop is for testing purpose to let me know everything is right.
calling the function TestingRandomItemNumber(); in beginplay is printing 30 -- 40 -- 30 from the data table which is what I want to do in C++. The blueprint script is working fine, I just want to translate it to C++.


void ATestActor::RandomItemNumber()
{
	int32 var3{};	//Testing purpose to store and print the stored values
	int32 var4{};	//Testing purpose to store and print the stored values
	int32 var5{};	//Testing purpose to store and print the stored values
	TArray<int32> Arr{};
	TArray<FName> OutRowNames{};
	(OutRowNames).Reset();

	if (UDataTable *DT_ItemNumberProbability = LoadObject<UDataTable>(NULL, TEXT("/Game/DataTables/ItemsGeneration/DT_ItemNumberProbability")))
	{
		OutRowNames = DT_ItemNumberProbability->GetRowNames();

		for (auto& Array : OutRowNames)
		{
			if (const UDataTable* DT_ItemNumberProbability{ LoadObject<UDataTable>(GetWorld(), TEXT("/Game/DataTables/ItemsGeneration/DT_ItemNumberProbability")) })
			{
				if (const FST_ItemNumberProbability * OutRow{ DT_ItemNumberProbability->FindRow<FST_ItemNumberProbability>(FName(Array), "") })
				{
					if (OutRow)
					{
						for ( int32 i = 1; i <= NumberProbabilityDatas.Percent; i++)
						{
							Arr.Add(NumberProbabilityDatas.Number);
						}
					}
				}
			}
		}

	}
}

this solution is still not complete and I am trying to make it complete

the shuffle part, you are missing these local variables

int32 ArrayIndex{};
int32 ArrayElement{};
bool bReturnInt3{};
bool bReturnInt4{};
bool bReturnInt5{};
        FCustomThunkTemplates::Array_Shuffle(Arr);
		FCustomThunkTemplates::Array_Get(Arr, ArrayIndex, ArrayElement);
		bReturnInt3 = UKismetMathLibrary::EqualEqual_IntInt(ArrayElement, 3);
		if (bReturnInt3)
		{
			++var3;
		}
		FCustomThunkTemplates::Array_Shuffle(Arr);
		FCustomThunkTemplates::Array_Get(Arr, ArrayIndex, ArrayElement);
		bReturnInt3 = UKismetMathLibrary::EqualEqual_IntInt(ArrayElement, 4);
		if (bReturnInt4)
		{
			++var4;
		}
		FCustomThunkTemplates::Array_Shuffle(Arr);
		FCustomThunkTemplates::Array_Get(Arr, ArrayIndex, ArrayElement);
		bReturnInt3 = UKismetMathLibrary::EqualEqual_IntInt(ArrayElement, 5);
		if (bReturnInt5)
		{
			++var5;
		}

now just don’t forget to print each variable to test if it works for you.

hope it helps, cheers!

1 Like

the code compiles fine but nothing prints on the screen, I printed the variables but nothing comes out on the screen.
I have this macro for printing variables data

#define printf(Format, ...) if(GEngine){GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Yellow, FString::Printf(TEXT(Format), ##__VA_ARGS__), false);}

to use this macro i just call this

printf(" %d", var3);
printf(" %d", var4);
printf(" %d", var5);

on the screen nothing printed, I don’t know what I am doing wrong

still in trouble translating this code in C++ :tired_face:

The Blueprint and the Code are not making any sense at all to me.

If you start with an empty Arr [], for each row in the Data Table, you are adding DataTable.Number to it DataTable.Percent times.

So, given the data table in your example, your array would look like

[3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]

is that what you’re trying to achieve? I can’t fathom what you would use that for.

and am I correct in seeing that you have variables named with numbers? or are those constant ints? If they are variables named with numbers, then you’ve completely lost me as to what the rest of it is doing… and if they are constant ints, then all that branching is doing nothing.

2 Likes


this is what I am trying to achieve and I am so closed to it.

You can do percentage selection WAY easier than building an array like that

int GetNumberOfItems() {
    const float PercentRoll = FMath::FRand();
    int NumberOfItems = 5;
    if (PercentRoll <= 0.30f) { NumberOfItems = 3; }
    else if (PercentRoll <= 0.70) { NumberOfItems = 4; }
    return NumberOfItems;
}

of course, it’ll take a few more lines to make it work with your datatable, but that’s the basics to doing a percent roll for selection.

3 Likes

I like your solution, its very understandable… I would like you to translate the whole bp logic in C++ on your own way if you are willing of course, but the functionality must be 100% the same… so it will fit my algorithm Thank you Sir.

this is the best solution what @eblade has shown you PercentRoll base Spawn.

and you said you don’t have anything printed on the screen is because your loop is missing dynamic instance pointer and it failed when tried to read/assign the datas from the struct variable NumberProbabilityDatas

Location where your loop is failed:

if (OutRow)
	{
		for ( int32 i = 1; i <= NumberProbabilityDatas.Percent; i++)
		{
			Arr.Add(NumberProbabilityDatas.Number);
		}
	}

Solution:

if (OutRow)
	{
        NumberProbabilityDatas = *OutRow;//you are missing this dynamic forward cast
		for ( int32 i = 1; i <= NumberProbabilityDatas.Percent; i++)
		{
			Arr.Add(NumberProbabilityDatas.Number);
		}
	}

hope it helps, cheers!

1 Like

wow, this works fine and how to print all the data in one line I mean print string append as I have done it in blueprint, if this is possible in C++ or any other alternative?
thank you so much for the detailed reply

you already have the macro defined, you can use it to print all the data in one line.

you don’t need to do this way

printf(" %d", var3);
printf(" %d", var4);
printf(" %d", var5);

do it this way: you can print unlimited integers and other data types this way, just change the format according to your macro
printf("%d __ %d __ %d", var3, var4, var5);

it will log var3 __ var4 __ var5

hope it helps, cheers!

1 Like

Also check out the logging macros. You can define log categories and set verbosity levels so you can filter out what you want to log.

UE_LOG(LogBlueprint, Warning, TEXT("Percent is: %d on object %s"), Percent, *GetName());
1 Like

very useful information , very much appreciated )