Skip to content

Commit

Permalink
Replace constructstatemachine node by a custom k2 node, add delegates…
Browse files Browse the repository at this point in the history
… in state machines and other features. add StateMachine component
  • Loading branch information
Maxwell21 committed Nov 16, 2018
1 parent 104cb44 commit b1990c3
Show file tree
Hide file tree
Showing 10 changed files with 529 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,10 @@ UClass* UStateMachineBlueprint::GetBlueprintClass() const
{
return UStateMachineBlueprintGeneratedClass::StaticClass();
}

bool UStateMachineBlueprint::SupportsInputEvents() const
{
return true;
}

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/************************************************************************/
/* UMBRA TOOLS */
/* Maxwell - Axel Clerget */
/************************************************************************/

#include "StateMachineComponent.h"
#include "Kismet/GameplayStatics.h"

// Sets default values for this component's properties
UStateMachineComponent::UStateMachineComponent()
{
// 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 = false;
}


// Called when the game starts
void UStateMachineComponent::BeginPlay()
{
Super::BeginPlay();

if (AutoStart)
this->Start();
}

void UStateMachineComponent::BeginDestroy()
{
this->UnbindDelegates();
this->Stop();

Super::BeginDestroy();
}

FState UStateMachineComponent::GetCurrentState()
{
if (this->StateMachine)
return this->StateMachine->GetCurrentState();

return FState();
}

void UStateMachineComponent::Start()
{
if (StateMachine == nullptr)
{
this->StateMachine = UStateMachine::ConstructStateMachine(Template, this, UGameplayStatics::GetPlayerController(this, 0));

if (this->StateMachine)
{
this->BindDelegates();
this->StateMachine->Start();
}
}
else
this->StateMachine->Start();
}

void UStateMachineComponent::Stop()
{
if (this->StateMachine)
StateMachine->Stop();
}

FString UStateMachineComponent::GetCurrentStateName()
{
if (this->StateMachine)
return this->StateMachine->GetCurrentStateName();

return FString();
}

void UStateMachineComponent::Pause()
{
if (this->StateMachine)
this->StateMachine->Pause();
}

void UStateMachineComponent::UnPaused()
{
if (this->StateMachine)
this->StateMachine->UnPaused();
}

bool UStateMachineComponent::IsPaused() const
{
if (this->StateMachine)
this->StateMachine->IsPaused();

return false;
}

bool UStateMachineComponent::IsValid() const
{
return this->StateMachine->IsValidLowLevel();
}

void UStateMachineComponent::BindDelegates()
{
this->StateMachine->OnInit.AddDynamic(this, &UStateMachineComponent::OnInitTriggered);
this->StateMachine->OnStart.AddDynamic(this, &UStateMachineComponent::OnStartTriggered);
this->StateMachine->OnStop.AddDynamic(this, &UStateMachineComponent::OnStopTriggered);
this->StateMachine->OnPause.AddDynamic(this, &UStateMachineComponent::OnPauseTriggered);
this->StateMachine->OnUnPause.AddDynamic(this, &UStateMachineComponent::OnUnPauseTriggered);
}

void UStateMachineComponent::UnbindDelegates()
{
if (this->OnInit.IsBound())
this->OnInit.RemoveDynamic(this, &UStateMachineComponent::OnInitTriggered);

if (this->OnStart.IsBound())
this->OnStart.RemoveDynamic(this, &UStateMachineComponent::OnStartTriggered);

if (this->OnStop.IsBound())
this->OnStop.RemoveDynamic(this, &UStateMachineComponent::OnStopTriggered);

if (this->OnPause.IsBound())
this->OnPause.RemoveDynamic(this, &UStateMachineComponent::OnPauseTriggered);

if (this->OnUnPause.IsBound())
this->OnUnPause.RemoveDynamic(this, &UStateMachineComponent::OnUnPauseTriggered);
}

void UStateMachineComponent::OnInitTriggered()
{
this->OnInit.Broadcast();
}

void UStateMachineComponent::OnStartTriggered()
{
this->OnStart.Broadcast();
}

void UStateMachineComponent::OnStopTriggered()
{
this->StateMachine = nullptr;
this->UnbindDelegates();
this->OnStop.Broadcast();
}

void UStateMachineComponent::OnPauseTriggered()
{
this->OnPause.Broadcast();
}

void UStateMachineComponent::OnUnPauseTriggered()
{
this->OnUnPause.Broadcast();
}
33 changes: 31 additions & 2 deletions Source/UT_Framework/Private/Object/StateMachine/StateMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "TimerManager.h"
#include "Engine/Engine.h"
#include "Engine/World.h"
#include "GameFramework/PlayerController.h"

/************************************************************************/
/* FTransition */
Expand Down Expand Up @@ -49,15 +50,18 @@ bool FState::IsValid()
/* UStateMachine */
/************************************************************************/

UStateMachine* UStateMachine::ConstructStateMachine(TSubclassOf<UStateMachine> Template, UObject* Owner)
UStateMachine* UStateMachine::ConstructStateMachine(TSubclassOf<UStateMachine> Template, UObject* Owner, APlayerController* Controller)
{
if (!Template || !Owner)
return nullptr;

if (UStateMachine* StateMachine = NewObject<UStateMachine>(Owner->GetWorld(), Template, NAME_None, RF_Standalone))
{
StateMachine->Init(Owner);
if (Controller)
StateMachine->PlayerController = Controller;

StateMachine->Init(Owner);

return StateMachine;
}

Expand All @@ -70,6 +74,7 @@ void UStateMachine::Init(UObject* Owner)
return;

this->OwnerObject = Owner;
this->OnInit.Broadcast();
}

void UStateMachine::CheckTransitionForCurrentState()
Expand All @@ -88,6 +93,7 @@ void UStateMachine::CheckTransitionForCurrentState()
{
FTransition* Transition = this->Transitions.Find(TransitionName);
FState State = this->GetStateByName(Transition->ToState);

// we notify the current state to finish is state and start the new one
this->FinishState();
this->SetCurrentState(State);
Expand All @@ -109,10 +115,21 @@ void UStateMachine::Start()
this->OnStateMachineStart();
this->SetCurrentState(this->GetRootState());
this->BeginState();

this->OnStart.Broadcast();
}
}
}

void UStateMachine::Stop()
{
this->UseTick = false;
this->CurrentState = FState();
this->OnStop.Broadcast();
this->ConditionalBeginDestroy();
GEngine->ForceGarbageCollection(true);
}

void UStateMachine::ExecuteInnerFunction(FName FunctionName, void* Params/* = nullptr*/)
{
if (UFunction* Function = this->FindFunction(FunctionName))
Expand Down Expand Up @@ -148,6 +165,16 @@ void UStateMachine::SetCurrentState(FState State)
this->CurrentState = State;
}

FState UStateMachine::GetCurrentState()
{
return this->CurrentState;
}

FString UStateMachine::GetCurrentStateName()
{
return this->GetCurrentState().Name;
}

void UStateMachine::SetRootState(FString Name)
{
for (int32 Index = 0; Index < this->States.Num(); ++Index)
Expand Down Expand Up @@ -286,12 +313,14 @@ void UStateMachine::Pause()
{
this->Paused = true;
this->UseTick = false;
this->OnPause.Broadcast();
}

void UStateMachine::UnPaused()
{
this->Paused = false;
this->UseTick = true;
this->OnUnPause.Broadcast();
}

bool UStateMachine::IsPaused() const
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/************************************************************************/
/* UMBRA TOOLS */
/* Maxwell - Axel Clerget */
/************************************************************************/

#include "StateMachineCallbackProxy.h"
#include "StateMachine.h"
#include "GameFramework/PlayerController.h"

UStateMachineCallbackProxy::UStateMachineCallbackProxy(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
}

UStateMachineCallbackProxy* UStateMachineCallbackProxy::CreateProxyObjectForConstructStateMachine(TSubclassOf<UStateMachine> Template, UObject* Owner, APlayerController* Controller)
{
UStateMachineCallbackProxy* Proxy = NewObject<UStateMachineCallbackProxy>();
Proxy->SetFlags(RF_StrongRefOnFrame);

if (UStateMachine* StateMachine = UStateMachine::ConstructStateMachine(Template, Owner, Controller))
{
Proxy->OnCompleted.Broadcast(StateMachine);
}
else
Proxy->OnFailed.Broadcast(nullptr);

return Proxy;
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ class UT_FRAMEWORK_API UStateMachineBlueprint : public UBlueprint
UStateMachine* GetStateMachine();

#if WITH_EDITOR

virtual UClass* GetBlueprintClass() const override;

virtual bool SupportsInputEvents() const override;

#endif

};
Loading

0 comments on commit b1990c3

Please sign in to comment.