Pass by reference is creating a local copy

Hello everyone,
I’m trying to pass in my skills object as a reference for it to be modified in another function:

void UMySkills::UpdateSkills()

{
	//for (FSkill searchedSkill : MySkillsList)
	for(int index = 0; index < (sizeof(MySkillsList)/sizeof(MySkillsList[0]));index++)
	{
		CalculateSkillValue(AttributesRef, &MySkillsList[index]);
	}
}

void UMySkills::CalculateSkillValue(TArray<FAttributes>* AttributeRef, FSkill* skill)
{
	skill->SkillValue = 999999;
	
}

However the skill->SkillValue reverts back to its default after the function call.

Am I doing pass by reference incorrect here? My understanding is that &MySkillsList[index] passes the address to the function pointer FSkill* skill so that when skill->SkillValue is changed, the value at the address is changed.

Please use unreal instructions. Perhaps that’s the origin of the problem.

 for (int32 index = 0; index < MySkillsList.Num(); index++)

It lets me step through my loop with that but the value still doesn’t seem to save.

As far as the pass by reference is that correct and the issue is somewhere else?

TArray of UStructs or UStructs*

To pass by reference, I’d do something like this instead:

void UMySkills::UpdateSkills()
{
     for (FSkill& MySkill : MySkillsList)
     {
          CalculateSkillValue(AttributesRef, MySkill);
     }
}
     
void UMySkills::CalculateSkillValue(TArray<FAttributes>* AttributeRef, FSkill& MySkill)
{
     skill.SkillValue = 999999;
}

Unfortunately this doesn’t work either. I think in this case FSkill MySkill is a local copy.

Of what type is MySkillsList? Is it a TArray? If so, why (sizeof(MySkillsList)/sizeof(MySkillsList[0])) and not MySkillsList.Num()?

It is a TArray of UStructs. sizeof is just the general way to get the length of an array. Also i haven’t looked at .Num() :stuck_out_tongue:

UPROPERTY(BlueprintReadOnly, Category = “Skills”)
TArray MySkillsList;

So I think its working (some of the other systems are not working) with a combination of the two:

for (int index = 0; index <MySkillsList.Num(); index++)
	{
		CalculateSkillValue(AttributesRef, MySkillsList[index]);
	}

In the debugger it looks like the values are sticking.

Thank you for the help!

Did it compile? This makes no sense.

It compiled just pasted in an odd way. What is not making sense?

My bad! The for loop should be like this instead:
for (FSkill& MySkill : MySkillsList)
{
CalculateSkillValue(AttributesRef, MySkill);
}
Updated the answer!