Microthreading

Every executed scripts may be suspended and resumed. A scrips has a built pointer variable _scScript of type ISCVMScript*. When you call Sleep method of this object, the script state is saved, and execution finished. When Execute method is called next time the script is not started from the begin, but the state is restored and next execution after Sleep is executed. This mechanism allows to use convenient and consistent algorithm structures (e.g. AI), it is not necessary to take into account that other objects must also be executed simultaneously.

But you must remember that the state is restored after the next execution and the state is not initial. If you call some SC function from C++ code, and the script sleeps, the call fails if the entry function (or main code) is different, that the entry piece of code on the sleep time. The simpliest way to avoid this situation is not to use multiple entry points in a script that uses sleep/resume mechanism.

A sample (not real case but shows the concept) below shows two scripts, that can be called one after another. Assumed that the used data is defined somewhere and is correct.

Let it be code for enrityScript1

while(alive)
{
  Move();
  _scScript->Sleep();
}

Let it be code for enrityScript2

while(!playerSeen)
{
  Patrol();
  _scScript->Sleep();
}

The c++ code called for example every frame may look like

while(runGame)
{
  ...
  entityScript1->Execute();
  entityScript2->Execute();
  ...
}

In this case the execution of the scripts are suspended every time _scScript->Sleep() instruction met, and resumed from the next instruction when Execute method is called again.