How do I handle manipulate const variable

I’m sure this is a programming question more to my lack of programming know-how and less a UE4 thing, but seeing as how many of the methods I wish to manipulate for the purposes of my game have this, I’m going to shamelessly ask anyways.

I’ve created a worldToComponent function that I I’m using throughout the CharacterMovementComponent during the walkable surface evaluations. Most of them use a const FHitResult& Hit, or Delta for use on the evaluations. An example of one is the IsWalkable Function. Something like: if (Hit.ImpactNormal.Z < KINDA_SMALL_NUMBER)

For my purposes I need to do something like if(WorldToComponent(Hit.ImpactNormal).Z < KINDA_SMALL_NUMBER) but I can’t because the input variables are const or at least I think that is what is going on. It appears that if I remove the const setting on the function declaration/ implementation then I can use my method, but many of these Methods are Virtual one’s so I have to dig through other files like the Movement Component. Being the noob that I am it’s a little scary going in there and pulling all of that out.

Is there another way I can alter a const variable during my evaluations? I thought maybe I could just create a FHitResult myHit = Hit; but this doesn’t seem to work. So this is where my understanding of the finer points of programming start to become less than enough. Anyhow, thanks in advance for any input on this.

The point of having a const variable is that it’s a contract, I’ll give you my data if you promise not to touch it, and you shouldn’t try to modify it. Your WorldToComponent function should not modify the incoming vector, it should take in a const FVector& and assign it to a FVector that you are free to modify and return.

If you need more assistance, post a bigger chunk of code, and I can try and help more :slight_smile:

Cheers,
Nick

Hmm I thought I tried that. This is the trimmed meat of what I’m trying to do:
//World To Component Function

WorldToComponent(FVector VectorToRotate)
{
FTransform compToWorldTrans = CharacterOwner->CapsuleComponent->GetComponentTransform();
FVector rotatedVector = compToWorldTrans.InverseTransformVectorNoScale(VectorToRotate);
return rotatedVector;
}

//Inside UCharacterMovementComponent::IsWalkable
IsWalkable(const FHitResult& Hit) const // Takes a const

//This logic is all over the CharacterMovementComponent, My game is a //circular world and my character capsule always points toward the center //(0,0,0), so I’m working around the “Z” is up logic by rotating the vectors to my //character capsules. Below is Original version

{ 
if (Hit.ImpactNormal.Z < KINDA_SMALL_NUMBER) {} 
}

//This is my version or what I’m trying to accomplish, but this won’t work because the Hit is a const. I’ll try what you recommended. Thanks!

if (WorldToComponent(Hit.ImpactNormal).Z < KINDA_SMALL_NUMBER) {}

WorldToComponent needs to be const

Change it to:
WorldToComponent(FVector VectorToRotate) const

The issue is const functions can only call const functions.