Unreal Code Examples

Basic Integration

void PlayerController::BeginPlay()
{
    Super::BeginPlay();
    	
    World = NewObject<UARCWorld>(this);
    ARCActionSpace = NewObject<UARCActionSpace>(this);
    ARCStateSpace = NewObject<UARCStateSpace>(this);

    Model = UARCBPLibrary::LoadModel(ModelConfig, false);
}

void PlayerController::Tick(float Delta)
{
    Super::Tick(Delta);

    // Create the world representation which we will convert to the state space matrix
    World->InitializeWorld(
        // All world attributes/objects that are relevant for the AI
    );
    
    // Get the state space for the model
    TArray<double> State = ARCStateSpace->GetState(*World);
    
    if (human)
    {
        // Convert the inputs into something the ARC agent can understand
        TMap<FString, bool> Action = ARCActionSpace->ConvertInputToBools(*World);
        
        // Add the observed state and action to the dataset
        Model->Collect(State, Action);
    } else {
        // If ARC agent is controlling, then perform inference
        auto ModelAction = UARCBPLibrary::SelectAction(Model,State);
        
        // Execute the action in-game
        auto Action = ARCActionSpace->ConvertActions(ModelAction);    
    }
}

Then at the end of the game, you can send the data you collected to the trainer platform as follows:

OR

If you have the API access to train in-game, then you can directly train within the game loop as follows:

State Space

First we will create the header file

Then we will create the C++ file

Action space

First we will create the header file

Then we will create the C++ file

Data Collection

Model Initialization

Inference

Last updated