Sorry, I can’t speak English well…
I want to enable / disable Tick Event dynamically.
Here are my code and blueprint node
First Code ( GameMode ) - when actor is clicked, call blueprint(UMG) function for set actor reference. It was checked by log ( print actor’s name)
void ATrackGameMode::ChangeMenuWidget(TSubclassOf<UUserWidget> NewWidgetClass,ASplineActor_Modify* actor) {
if (CurrentWidget != nullptr) {
CurrentWidget->RemoveFromViewport();
CurrentWidget = nullptr;
}
if (NewWidgetClass != nullptr) {
CurrentWidget = CreateWidget<UUserWidget>(GetWorld(), NewWidgetClass);
if (CurrentWidget != nullptr) {
CurrentWidget->AddToViewport();
FOutputDeviceNull ar;
if (actor != NULL) {
UE_LOG(LogTemp,Warning, TEXT("%s"), *actor->GetName());
const FString command = FString::Printf(TEXT("SetActor /Game/StarterContent/Maps/Minimal_Default.Minimal_Default:PersistentLevel.%s"), *actor->GetName());
CurrentWidget->CallFunctionByNameWithArguments(*command, ar, NULL, true);
}
}
}
}
Second, when UMG’s button is clicked, call actor’s function named Simulate / SimulateOff calling SetActorTickEnabled in cpp. It was checked by log ( print IsActorTickEnabled())
Constructor
ASplineActor_Modify::ASplineActor_Modify()
{
PrimaryActorTick.bCanEverTick = true;
PrimaryActorTick.bStartWithTickEnabled = false;
bIsSimulate = false;
}
Simulate / SimulateOff / SetTickEnabled
void ASplineActor_Modify::Simulate(APawn* pawn) {
bIsSimulate = true;
MySetTickEnabled(true);
}
void ASplineActor_Modify::SimulateOff() {
bIsSimulate = false;
MySetTickEnabled(false);
}
void ASplineActor_Modify::MySetTickEnabled(bool b) {
SetActorTickEnabled(b);
if (IsActorTickEnabled()) {
UE_LOG(LogTemp, Warning, TEXT("Tick Enabled"));
}
else {
UE_LOG(LogTemp, Warning, TEXT("Tick Disabled"));
}
}
Next, Tick Code. - Trace the splineComponent’s road. If Tick had been called, printed Log. But, actually didn’t print log.
void ASplineActor_Modify::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (bIsSimulate) {
UE_LOG(LogTemp, Warning, TEXT("true"));
}
else {
UE_LOG(LogTemp, Warning, TEXT("false"));
}
//UE_LOG(LogTemp, Warning, TEXT("Tick"));
if (bIsSimulate) {
UE_LOG(LogTemp, Warning, TEXT("IsSimulate"));
if (bIsSimulateStart) {
StartDeltaTime = DeltaTime;
bIsSimulateStart = false;
}
CurrDistance += FMath::FInterpTo(0.0f, SplineComponentExample->GetSplineLength(), DeltaTime-StartDeltaTime, Speed);
/*GetLocationDistanceAlongSpline*/
FVector vector = SplineComponentExample->GetLocationAtDistanceAlongSpline(CurrDistance, ESplineCoordinateSpace::Type::World);
FRotator rotator = SplineComponentExample->GetRotationAtDistanceAlongSpline(CurrDistance, ESplineCoordinateSpace::Type::World);
UGameplayStatics::GetPlayerPawn(GetWorld(), 0)->SetActorLocationAndRotation(vector, rotator);
if (SegmentLocations[CurrSplineNdx + 1] - CurrDistance < DistanceTillNextShift) {
int input = CurrSplineNdx + 1;
int length = SegmentLocations.Num();
if (input >= length - 1) {
CurrSplineNdx = 0;
CurrDistance = 0.0f;
}
else {
CurrSplineNdx += 1;
}
}
}
}
Last, my log - TickEnabled is set well
So, I think it is working well. but, Tick doesn’t work…
Please advise me to know how to implement.
Thank you for reading long answer.