(info) This AI is still in early stages of development.

Process Instances

Process instances contain one or more tasks, and run those tasks over several game ticks to avoid game stutter/freezing.

 

What is a Process Instance?

You can think of process instances as very basic threads.

Each process instance has a unique name and some other settings that are defined when it's created, and can store a number of Tasks that it will carry out when it's run.

How do I create one?

Use Processes() to create new instances, or retrieve existing instances.

How do I add tasks to an instance?

You'll need to create some tasks – see Tasks for more information.

Then add your tasks to a process, in the order you want them to be executed, using the instance.addTask() method.

How do I run an instance?

There are several ways to run a process:

  • Execute the instance directly (it's a function) – see instance() and instance.run()
  • Activate the instance and it will run on a regular interval – see instance.activate()
  • You can also automatically activate instances when the game starts, see - Automatic activation and deactivation
  • Link the instance to a game event and it will run whenever that event fires – see Using events to trigger instances

What happens when the instance runs?

When the instance runs, it will process each of its tasks in turn.

If there isn't enough processing time on the current game tick, remaining tasks will be deferred to later game ticks when there's more processing time.

How do I know when a run starts and stops?

All instances have two events - instance.onRun() and instance.onEnd(). These events are called at the start and finish of every run, they are useful for initialising instance.data or triggering other processes, etc.

What happens if I try and run an instance that's already running?

If you try running an instance that's already running, the current run will finish first before the next run starts.

Each run has it's own data object, and an array of these can be found in instance.runData.

When a task is triggered the data for the current run is passed in as one of the parameters - see Tasks for more information.

How do I abort a run?

You can abort the current run by returning PROCESS_STATE_ABORT from a task - see Tasks for more information.

Alternatively, you can call instance.abort() to abort either the current run or all runs.

When a run is aborted, no further tasks will be processed for that run, and the process will either return to it's ready state (waiting for a future run) or, if there are other runs queued, the next run will start.

How do I delete an instance?

To delete process instances, use Processes.destroy(processName).

Documentation

Instances have a number of methods, properties and events. For more information see:

  • instance.activate()Activate the interval timer for a process instance, causing the instance to be run at regular intervals until deactivated.
  • instance.onRun()Fired immediately before a new process run starts.
  • instance.onEnd()Fired immediately before the current process run ends.
  • instance.onActivate()Fired when the interval timer for a process instance becomes active.
  • instance()Process instances are functions which you can run to, uhm..., run them...
  • instance.onDeactivate()Fired when the interval timer for a process instance has been deactivated.
  • instance.activeA boolean indicating whether the process instance has an active timer interval.
  • instance.stateIndicates whether the process is currently idle, running or aborting.
  • instance.intervalThe current interval time set for a process instance.
  • instance.run()Run a process instance to execute it's tasks.
  • instance.skipsA number used to determine if a process instance is stalling due to other processes being run.
  • instance.runsA number indicating how many times a process instance has been run
  • instance.changeInterval()Change the timer interval for a process instance.
  • instance.deactivate()Deactivate the interval timer so the process instance will no longer be run at a regular interval.
  • instance.autoActivateA boolean value stating whether the process instance should automatically activate or not.
  • instance.processNameA string containing the name of the instance process
  • instance.autoDeactivateA boolean value stating whether the process instance should automatically deactivate or not.
  • instance.addTask()Adds a new task to a process instance.
  • instance.abort()Abort the current run of a process instance, optionally cancelling any queued runs.
  • instance.removeTask()Remove any matching tasks, or all tasks, from a process instance.
  • instance.dataThe processData object for a process instance.