Advertisement

Re: Long running scripts & concurrent contexts

Started by March 13, 2017 05:56 PM
0 comments, last by WitchLord 7 years, 8 months ago

In the SDK/Samples folder there is a concurrent context example. In the same two scripts are executed, script1/script2, with each script having a for loop that sleeps for 1 second. Within the main.cpp, the following function then causes the scripts to execute:

for(;;)
{
// Check if any key was pressed
if( kbhit() )
{
contextManager.AbortAll();
break;
}
// Allow the contextManager to determine which script to execute next
contextManager.ExecuteScripts();
}

I understand the contextManager.ExecuteScripts() causes the "main" in each of the scripts to be executed, and more particularly, given time to perform their loop before yielding back and sleeping.
However, what I do not understand (nor can find in the documentation) is the answer to the following questions:
  1. Does the ExecuteScripts() method only call the main function in each thread? I assume this is true, but I can't find the answer within the contextmanager addon sourcecode. It would be good to know why ExecuteScripts is calling 'main' and if its limited to only operating on that interface method. It would be nice to be able to write a class with a method that is intended to be long-running and called by the ContextManager. I know I can get there by having an object instantiated in main and calling its methods.. but I'd like to avoid that if possible.
  2. This leads to my second question, what if I want to call a script function from the C++ side. For instance, what if I want to call Obj::SomeLongRunningMethod() that sleeps every 500 ms and performs some type of long-running state control.

Thanks very much

The context manager doesn't by itself what is being executed. You just add the already prepared contexts to the manager, and then the manager will cycle through them to let each execute for a portion of time before the next. If you add the context as a co-routine to another, then the switch will only happen when one of the contexts call yield(), but if you add the context as a concurrent routine the switch will happen on a timing interval.

You can call class methods from the application too. Just prepare the context with the method, and set up the object to invoke the method on. Once the context is prepared you can give it to the context manager to manage the execution.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement