Simple Game Using Grail

This page presents a minimal example of a game using Grail. The aim of this code sample is to give an overview of how Grail interacts with a game loop.

  • C++

  • C#

InitializeGame(); //load scene, call initialization methods on scene objects

//create an AI manager with 4 worker threads
grail::AIManager manager{ 4 };

//create an AI Entity and assign a Reasoner
auto entity = std::make_shared<grail::AIEntity>();
entity->SetReasoner(std::make_unique<MyReasoner>();

//register the entity
manager.RegisterAIEntity(entity, 0);

//this section represents a simple game loop
float deltaTime = 0.05f;
const std::size_t FRAME_COUNT = 1000;
float reasonerUpdateInterval = 0.5f;
float reasonerUpdateTimer = 0.0f;
for(std::size_t frame = 0; frame < FRAME_COUNT; ++frame)
{
    GameplayLogic();

    //each frame, update the AI manager
    //in our Unity and UE4 plugins, this is done by special components
    //but if you're using your own engine, you'll have to add this update code yourself

    //you can choose to update reasoners less frequently
    reasonerUpdateTimer += deltaTime;
    if(reasonerUpdateTimer > reasonerUpdateInterval)
    {
        reasonerUpdateTimer = 0.0f;
        manager.UpdateReasoners();
    }
    manager.UpdateEntities(deltaTime);
}

FinishGame();
InitializeGame(); //load scene, call initialization methods on scene objects

//create an AI manager with 4 worker threads
var manager = new Grail.AIManager(4);

//create an AI Entity and assign a Reasoner
var entity = new Grail.AIEntity();
entity.Reasoner = new MyReasoner();

//register the entity
manager.RegisterAIEntity(entity, 0);

//this section represents a simple game loop
float deltaTime = 0.05f;
const int FRAME_COUNT = 1000;
float reasonerUpdateInterval = 0.5f;
float reasonerUpdateTimer = 0.0f;
for(int frame = 0; frame < FRAME_COUNT; ++frame)
{
    GameplayLogic();

    //each frame, update the AI manager
    //in our Unity and UE4 plugins, this is done by special components
    //but if you're using your own engine, you'll have to add this update code yourself

    //you can choose to update reasoners less frequently
    reasonerUpdateTimer += deltaTime;
    if(reasonerUpdateTimer > reasonerUpdateInterval)
    {
        reasonerUpdateTimer = 0.0f;
        manager.UpdateReasoners();
    }
    manager.UpdateEntities(deltaTime);
}

FinishGame();