Hello everybody.
I started to learn UE with c++ and i have an issue.
I created BP Actor, for example, it is a gate. BP consist of two things: cube and c++ component script, which tells BP Actor how to move and when.
The problem is, if i try to change any of variables in the Editor (for example move speed of the gate), it fires BeginPlay() method of BP Actor.
I have a variable in BeginPlay method, which is store the original (start) location of the Actor. And everytime i change a variable in the editor, it is been rewritten.
How can i avoid calling of BeginPlay method in my c++ script and make a changes for testing my variables in the editor?
Inside the BeginPlay method, remove Super::BeginPlay, I think this should do.
just tested it and it doesnt seem to work ![]()
Yes, i tried this one too.
Also, if i attach my cpp script to any primitive object in the scene (i.e. not a blueprint actor), the script works fine with changing variables in Editor, it fires BeginPlay only ones, as i expect
Could you share the code?
Ofcourse.
But this thing appears with any over cpp components BeginPlay() function.
Mover.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "Mover.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class CRYPTRAIDER_API UMover : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UMover();
UFUNCTION(BlueprintCallable)
void Move(const float& DT);
UFUNCTION(BlueprintCallable)
void Rotate(const float& DT);
protected:
// Called when the game starts
virtual void BeginPlay() override;
public:
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
private:
UPROPERTY(EditAnywhere)
FVector MoveOffset;
UPROPERTY(EditAnywhere)
FRotator RotationAngle;
UPROPERTY(EditAnywhere)
float TimeForMove;
UPROPERTY(EditAnywhere)
float AngleSpeed;
UPROPERTY(EditAnywhere)
bool ShouldMove = false;
UPROPERTY(EditAnywhere)
bool ShouldRotate = false;
FVector OriginalLocation;
FRotator OriginalRotation;
};
Mover.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "Mover.h"
#include "Math/UnrealMathUtility.h"
// Sets default values for this component's properties
UMover::UMover()
{
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don't need them.
PrimaryComponentTick.bCanEverTick = true;
// ...
}
// Called when the game starts
void UMover::BeginPlay()
{
Super::BeginPlay();
OriginalLocation = GetOwner()->GetActorLocation();
OriginalRotation = GetOwner()->GetActorRotation();
// ...
}
// Called every frame
void UMover::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
Move(DeltaTime);
Rotate(DeltaTime);
// ...
}
void UMover::Move(const float& DeltaTime)
{
FVector TargetLocation = ShouldMove ? OriginalLocation + MoveOffset : OriginalLocation ;
float MoveSpeed = MoveOffset.Length() / TimeForMove;
FVector NewLocationToMove = FMath::VInterpConstantTo(
GetOwner()->GetActorLocation(),
TargetLocation,
DeltaTime,
MoveSpeed
);
GetOwner()->SetActorLocation(NewLocationToMove);
}
void UMover::Rotate(const float& DeltaTime)
{
FRotator TargetRotation = ShouldRotate ? OriginalRotation + RotationAngle : OriginalRotation;
FRotator NewRotation = FMath::RInterpConstantTo(
GetOwner()->GetActorRotation(),
TargetRotation,
DeltaTime,
AngleSpeed
);
GetOwner()->SetActorRotation(NewRotation);
}