Override the Default Game State
The SDK provides a volatile game_state function with basic interaction and scene information. Register a non-volatile function with the same public name to provide your game’s state.
Include fields that help the agent understand whether it can act and what is happening in the current level. The default field names are _can_interact, _hint_key, and current_scene.
#pragma once
#include "CoreMinimal.h"
#include "Flayer/FlayerFunctionLibrary.h"
#include "Utils/FlayerFunctionUtils.h"
#include "MyGameStateFunctions.generated.h"
USTRUCT()
struct FMyGameState
{
GENERATED_BODY()
UPROPERTY()
bool _can_interact = true;
UPROPERTY()
FString _hint_key;
UPROPERTY()
FString current_scene;
UPROPERTY()
FString player_location;
};
UCLASS()
class UMyGameStateFunctions : public UFlayerFunctionLibrary
{
GENERATED_BODY()
public:
UMyGameStateFunctions()
{
REGISTER_FLAYER_FUNCTION_SHORT(
GetGameState,
"game_state",
"Returns the current game state as JSON"
);
}
UFUNCTION()
FString GetGameState() const
{
FMyGameState State;
if (const UWorld* World = GetWorld())
{
State.current_scene = World->GetMapName();
State._hint_key = State.current_scene;
if (const APawn* Pawn = UFlayerFunctionUtils::GetPlayerPawn(this))
{
State.player_location = Pawn->GetActorLocation().ToString();
}
else
{
State._can_interact = false;
}
}
else
{
State._can_interact = false;
}
FString Json;
UFlayerFunctionUtils::StructToString(State, Json);
return Json;
}
};Keep the interaction flag accurate during menus, cutscenes, loading, and other periods when the agent should wait. Add game-specific values that help the agent identify targets or verify an action.
Last updated on