Skip to content

Using with C++

C++ can be used to manage instances. Assembling state machines should only be done using the state machine blueprint editor.

Naming

All types generally start with SM (USM, FSM, etc) and follow Unreal Engine naming conventions.

SM is for State Machine

Most of the core plugin was written before settling on "Logic Driver". "FSM" was a nice coincidence for structs!

Build.cs

In your [PROJECT].Build.cs file add SMSystem to PublicDependencyModuleNames.

PublicDependencyModuleNames.AddRange(new string[]
{
    "Core", "CoreUObject", "Engine", "InputCore", "SMSystem"
});

Mac OS and Logic Driver Pro

For Mac OS and Logic Driver Pro add the following to your [PROJECT].Build.cs:

if (Target.Type == TargetType.Editor)
{
    PrivateDependencyModuleNames.AddRange(
        new string[]
        {
            "SlateCore"
        }
    );
}

This prevents a compile error for FTextBlockStyle. TextBlockStyle is used to edit the widget of exposed properties and only required for the editor.

File Usage

Include the utility header for instantiating instances:

#include "SMUtils.h"

Instantiating and Running a State Machine Instance (Non-Component)

.h file
UPROPERTY(BlueprintReadWrite)
USMInstance* StateMachine
.cpp file
StateMachine = USMBlueprintUtils::CreateStateMachineInstance(StateMachineClass, YourContext);
StateMachine->Start()

The StateMachineClass is a subclass of USMInstance, and YourContext is the object the state machine should run for. Just call Start() when you are ready.

Memory Management

Make sure you assign your instance to a UPROPERTY! The instance is a UObject and may be garbage collected by Unreal Engine otherwise. When you do want it garbage collected, null out the reference and make sure there are no other strong references to it.

Creating Nodes with C++

Logic Driver Pro supports designing custom node classes through C++. See the Pro Quickstart Guide. You will still need to use the state machine blueprint editor to design the state machine itself.

C++ Example

Below we extend USMStateInstance which is a state class that can be placed in a state machine graph. Notice that OnStateBegin_Implementation, OnStateUpdate_Implementation, and OnStateEnd_Implementation are overidden. This allows you to execute state logic in C++ rather than blueprints. See the USMStateInstance class for all virtual _implementation methods you can override.

#pragma once

#include "CoreMinimal.h"
#include "SMStateInstance.h"
#include "SMNativeStateNode.generated.h"  // Your filename.generated.h

UCLASS(Blueprintable, BlueprintType)
class YOURGAME_API USMNativeStateNode : public USMStateInstance
{
public:
    GENERATED_BODY()

protected:
    virtual void OnStateBegin_Implementation() override;
    virtual void OnStateUpdate_Implementation(float DeltaSeconds) override;
    virtual void OnStateEnd_Implementation() override;

};

Below is an example implementation of OnStateBegin and how to use the context passed into the state machine.

1
2
3
4
5
6
7
8
void USMNativeStateNode::OnStateBegin_Implementation()
{
    // If your context is your character we can use it just like in blueprints.
    if(ACharacter* CharacterContext = Cast<ACharacter>(GetContext()))
    {
        CharacterContext->DoStuff();
    }
}

You can also create transitions, conduits, and state machine node types in C++.

Example Project

See the Dialogue Plugin for an example C++ implementation of node classes.