Download

Derived cpp projectilecomponent's const modifier makes it unable to pass value to UPROPERTY

I am working on a cpp project in Unreal. I know people like to use const and & everywhere to improve the efficiency, but they make it difficult to pass values in the functions.

I create a derived class from UE4’s projectile calculation component class The parent class will call the compute acceleration function and pass a reference of the projectile’s current velocity I defined a separate Velocity0 FVector in the derived class(UNewActorComponent), I want to expose it to blueprint to report projectile’s current velocity, because the original velocity variable seems to be broken after I modify the acceleration function, it is printed as inf.
I want to assign its value from the argument InVelocity in

“ComputeAcceleration(const FVector & InVelocity, float DeltaTime)”

Here is the cpp code of my modified projectile component



FVector UNewActorComponent::ComputeAcceleration(const FVector & InVelocity, float DeltaTime) const  
{
 FVector Acceleration(FVector::ZeroVector);
 FVector DragAcceleration(FVector::ZeroVector);//caused by aero dynamic

 float CurrentGAccAcumulate = 0.0f;

Velocity0=InVelocity;// I try to pass the const reference argument (at a compute acceleration call) to a UPROPERTY
//compute acceleration code//

 return Acceleration;
 //return FVector();
}


FVector UNewActorComponent::ComputeDragAcc(const FVector & V0) const
{
//ompute drag function's code//
}

float UNewActorComponent::ComputeLocalAirDensity() const
{
//compute air density code//

}


This is what the header file is like



UCLASS(ClassGroup = Movement, meta = (BlueprintSpawnableComponent), ShowCategories = (Velocity))
class SHIPTEST_API UNewActorComponent : public UProjectileMovementComponent
{
 GENERATED_BODY()

public:
 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Projectile)
  float dragindex;

public:
 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Projectile)
  float crosssection;


public:
 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Projectile)
 float shellmass;

 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Projectile)
 FVector Velocity0;//


protected:
 FVector ComputeAcceleration(const FVector& InVelocity, float DeltaTime) const override;

 FVector ComputeDragAcc(const FVector & V0) const;

 float ComputeLocalAirDensity() const;
};



It is not gonna work, coz there even isn’t a “=” method for FVector


(PS the line 13 -590 is commented out)

But even If I break it into each element, it still does not work

So this time, the problem is that I cannot pass const reference type argument to a non const variable

But If I delete all the const modifier in my .h and .cpp file, it even does not allow me to override, jt just prompt “cannot override function UNewActorComponent::ComputeAcceleration…”

So how can I pass the velocity value to the blueprint?

Well…I even cannot change a float uproperty in the component functions…

ComputeAcceleration is marked as const, meaning it cannot modify the members of UNewActorComponent. Instead, it’s meant to calculate a velocity and then return that velocity which gets stored by the component.

If you want to store the InVelocity, then do so before you call ComputeAcceleration.

Thanks for the answer.
So it looks like the default projectilemovementcomponent marked all transform-related-functions with const…
This means I probably cannot store the InVelocity in my derived projectile class.
Instead, I should abandon it and modify directly on the projectilemovementcomponent.cpp