Skip to content

Processes

Adrian edited this page Feb 5, 2021 · 16 revisions

Overview

Zenith attempts to stay true to an entity-component architecture, so it would be remiss not to have an interface that can be inherited by all updatable and renderable objects and components. The ZProcess class is an abstract interface that includes such methods, alongside other useful methods, such as those for pausing and aborting a process.

Upon creating a ZProcess pointer, we can attach it to the engine process runner:

    ZServices::ProcessRunner()->AttachProcess(process);

Take a look at the Process Runner section for more information on the process runner implementation.

We can query the state of a process using some of the provided helper methods:

    process->IsAlive();   // running or paused
    process->IsDead();    // finished, failed or aborted
    process->IsPaused();  // self explanatory

We can also query the raw state of the process:

    process->State();

Or set the state:

    process->SetState(ZProcessState::Aborted);

An interesting property of processes is that we can chain them. Any child we attach to a process will run after the parent process has finished.

    process->AttachChild(childProcess);

All Zenith processes, whether objects, components or other classes, contain a protected id_ member, which should be set when the object is initialized. For this purpose, a ZIDSequence class which can be instantiated at any level of granularity in order to provide unique object ids. It uses the STL mersenne twister implementation to generate pseudo-random number sequences.

    // Setting a unique id for every subclass instance of ZProcess is recommended.
    // Append or prepend to this as you see fit and feel free to use your own RNG implementation.
    static ZIDSequence idGenerator_;
    ... Some time later ...
    id_ = idGenerator_->Next();

Process Runner

ZProcessRunner is a subsystem that is initialized by the engine. It is responsible for updating and handling ZProcess objects.

To attach a process to the runner we simply call it’s AttachProcess method and pass in the process pointer like so:

    ZServices::ProcessRunner()->AttachProcess(process);

We can kill all the currently running processes by calling AbortAllProcesses:

    ZServices::ProcessRunner()->AbortAllProcesses(true);

By passing in a boolean, we instruct the process runner as to whether or not it should immediately abort a process, or leave it in its current state. Regardless, all processes will be removed from the runner's internal list.

Ticking the process runner is as simple as calling its UpdateProcesses method:

    ZServices::ProcessRunner()->UpdateProcesses();

If a process is attached in an uninitialized state, this method will properly initialize the process. Thenceforth, a process’s Update method will be called until it is aborted, or finished, at which point the process runner will queue any child process if it exists, and then proceed to remove the finished parent process from the internal list.

Calling UpdateProcesses() is not strictly necessary, as it is done internally by every game loop.

Clone this wiki locally