Hi, as a part of my learning process in this good engine i’m trying to convert a Blueprint project to a C++ project.
No, i’m trying to make the player’s character rotate and for it i have the next workflow:
So, i make the next code:
GENERATED_BODY()
FTimeline OnDeathRotation;
FTimeline OnPlayRotation;
UPROPERTY(EditAnywhere)
class UCurveFloat *DeathRotation;
UPROPERTY(EditAnywhere)
class UCurveFloat *PlayRotation;
void BeginPlay() override;
virtual void Tick(float DeltaSeconds) override;
protected:
UFUNCTION()
void RotatePlayer();
For the player’s character header file.
PlayRotation = NewObject<UCurveFloat>();
PlayRotation->FloatCurve.AddKey(0.0f, 0.0f);
PlayRotation->FloatCurve.AddKey(1.0f, -360.0f);
DeathRotation = NewObject<UCurveFloat>();
DeathRotation->FloatCurve.AddKey(0.f, 0.f);
DeathRotation->FloatCurve.AddKey(1.f, -512.f);
FOnTimelineFloat TimelineCallback;
FOnTimelineEventStatic TimelineFinishedCallback;
TimelineCallback.BindUFunction(this, "RotatePlayer");
// TimelineFinishedCallback.BindUFunction(this, FName(TEXT("SetState")));
if(PlayRotation){
OnPlayRotation.AddInterpFloat(PlayRotation, TimelineCallback, TEXT("Player Rotation"));
OnPlayRotation.SetLooping(true);
OnPlayRotation.SetTimelineLength(1.f);
OnPlayRotation.SetTimelineLengthMode(ETimelineLengthMode::TL_TimelineLength);
} else{
UE_LOG(LogTemp, Warning, TEXT("There is no Curvefloat Object here!"));
}
For the player’s character constructor in the cpp
file; in creating the curve, instead to import it, based on [this][2] forum post; also, i’m creating the FTimeline
variable in the constructor based on the same post because i will play the timeline in the BeginPlay
function.
void ASideRunnerPaperCharacter::Tick(float DeltaSeconds){
Super::Tick(DeltaSeconds);
if(OnPlayRotation.IsPlaying()) OnPlayRotation.TickTimeline(DeltaSeconds);
}
void ASideRunnerPaperCharacter::BeginPlay(){
Super::BeginPlay();
Ref_GameMode = Cast<ASideRunnerGameModeBase>(GetWorld()->GetAuthGameMode());
OnPlayRotation.Play();
}
This both also in the cpp
file, when i play the timeline in the BeginPlay
function, and, as far as i understood, i need to link the timeline to the tick function in the Tick
function; until here, no problems, we can say.
`void ASideRunnerPaperCharacter::RotatePlayer()
{
float TimelineValue = OnPlayRotation.GetPlaybackPosition();
// UE_LOG(LogTemp, Warning, TEXT("Curves: %S"), *OnPlayRotation.GetAllCurves());
if(PlayRotation){
UE_LOG(LogTemp, Warning, TEXT("Timeline Value: %f"), TimelineValue);
} else {
UE_LOG(LogTemp, Warning, TEXT("There is no such CurveFloat"));
}
// float PlayRotationFloatValue = PlayRotation->GetFloatValue(TimelineValue);
// OnPlayRotation->Play();
// TODO: implements Rotation Timeline here
// GetSprite()->SetRelativeRotation(FRotator(0.f, PlayRotationFloatValue, 0.f)); // Rotation here
}
This is the function i bind with the timeline and which i use to rotate the player, but, i have a problem here: as you can see there, i’m reviewing is the PlayRotation
curve does exist because i’m getting an editor’s crash each time i get the PlayRotationFloatValue
variable and i don’t understand why.
Trying to understand what is happening i got that PlayRotation
curve in that function does not exist of maybe it is null, for it, i made some experiments:
Here, i reviewed it the curve does exist, and it is, at least in the constructor.
I wanted to know if there is a problem with the binded function and we can see that the timeline value does exist.
Here is where i think there is a problem with the curve, because inside of the binded function i cannot get it.
I tried to make the timeline in the BeginPlay
function, as other tutorials like [this][6] or [this][7] do it, but it also gives me problems:
So, what’s the problem here and how can i solve it?
Thank you very much.