I have an component in my UE5 project (custom state machine):
UCLASS(ClassGroup = States, meta = (BlueprintSpawnableComponent), Blueprintable, BlueprintType)
class EP1_API UStateMachineComponent : public UActorComponent {
GENERATED_BODY()
public:
UPROPERTY(BlueprintReadOnly, VisibleAnywhere)
ECharacterState state; //my enum
void UStateMachineComponent::BeginPlay() {
Super::BeginPlay();
state = ECharacterState::Idle;
}
UFUNCTION(BlueprintCallable)
bool SwitchState(ECharacterState NewState); //only way to change state
UFUNCTION(BlueprintCallable)
FORCEINLINE ECharacterState GetCurrentState() const { return state; }
};
I have this component inside my Character class (then I have BP class that inherits from it, and on top of it another BP class inheriting from previous BP class):
UCLASS(Blueprintable)
class EP1_API ACharacterBase : public ACharacter, public ICharacterI {
GENERATED_BODY()
public:
UPROPERTY(BlueprintReadOnly, VisibleAnywhere)
TObjectPtr<UStateMachineComponent> StateMachine;
ACharacterBase() {
StateMachine = CreateDefaultSubobject<UStateMachineComponent>(TEXT("StateMachine"));
}
...
}
During gameplay, I have some object that upon interaction calls to SwitchState
with ECharacterState::Sitting
.
When I hit play for the first time, on details panel of my Character I can see StateMachine component with state Idle (grayed out, not editable, as it should be). When I reach a chair, it calls SwitchState
, it triggers the breakpoint in C++ and I see it calling my state change.
But when I look at details panel of Character it keeps showing the Idle
instead of Sitting
(regardless if I select another object and select Character again to refresh or anything):
At the same time, in C++, when I place breakpoints in different places, it shows me Sitting
:
Why there is such difference?
Even more strange. When I stop game and hit play again, details shows Idle
but Character is already in Sitting
state (in C++), from previous game session (despite I set it to Idle
at start in component’s BeginPlay()
- which I see being called).
From what I see in Visual Studio debugger, inside BeginPlay()
of my UStateMachineComponent
this
has different address than what I get later during game (almost like I have some mixed-up pointers there… or getting the Character or StateMachine from previous game in C++).