Would this be misuse of aslScriptContext::Suspend()?

Started by
1 comment, last by Servant of the Lord 8 years, 7 months ago

I'm currently thinking through how I want scripts to interact with my game logic.
Most of my logic is going to be implemented strait in C++, but some of it will be exposed to scripts.

Some of the exposed logic gets processed over time, with scripts firing off an action that may take several seconds before it completes.

Alot of this logic is higher-level, so I was thinking my scripts might look something like this:


//Sets this entity's movement goal, and the entity moves towards that location.
//This function just returns an ID for a 'trigger' that gets triggered when the entity reaches that location.
Trigger reachedNode = thisEntity.MoveTo("node placed on map");

//Other events can be operating simultaneously.
Camera.ShakeScreen(amount, duration);

//Some functions just operate immediately.
thisEntity.SetSomeValue(value);

//Here, the script gets suspended (likely using aslScriptContext::Suspend()).
//The entity continues doing whatever logic C++-side that was already triggered (i.e. shaking the screen for the desired length of time, and walking toward the node), until the entity reaches the node, at which time C++ will resume the script's execution. Other scripts for other entities would've still been running.
WaitForTrigger(reachedNode);

//Now that the entity reached the node, we show a dialog box.
Trigger doneReadingDialog = GUI.ShowDialog("human-readable name for dialog details (including facial portraits and rich text and etc...)");

//Wait for the player to read the dialog, before resuming.
WaitForTrigger(doneReadingDialog);

//...etc...

I'd probably have up to 50-ish of these scripts in execution and suspended, though usually it wouldn't be that high, and

My questions are:
A) Does this seem like reasonable use of aslScriptContext::Suspend(), or would this be abuse of that feature?
B) Am I using too many contexts?
C) What function should I use to execute small blocks of "throwaway" scripts (i.e. scripts I want to run once and then discard)? Would the ExecuteString() add-on be able to run multi-line chunks of code like the above example? Would it be able to suspend and resume?
D) If one script calls another script, do I have to use a new context, or can I use aslScriptContext::PushState()/PopState() to execute that new script before resuming the original script?


Thank you for your help - I'm new to AngelCode.

Advertisement

A) This is a perfectly valid use case. Using this script engine like this can be thought of a form of coroutine. Just remember that it is currently not possible to serialize a context's state, so if you plan on allowing the user to save the game in the middle of a playthrough with coroutines still running you'll face trouble.

B) You'll probably be fine with 50 something contexts. Expect each context to take up a few KB of memory. If you're targeting platforms with limited amount of memory you can tweak the amount of memory that is allocated for the callstack in the contexts by setting the engine property asEP_MAX_STACK_SIZE.

C) Take a look at the code for the ExecuteString helper function. It is a good place to start, but you'll likely want to make some adaptions to suite your own specific needs. For example, resuming the context after it has been suspended. By default the function doesn't support resuming, since it discards the script code before returning.

D) Yes, PushState/PopState can be used to call the different script. Though, obviously you cannot resume the original script before the called script finishes if you do.

Regards,

Andreas

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

Thanks a bunch. The library and documentation seems really straightforward and easy to understand.

I appreciate being able to pick up and use a language like this without having to code (a exceptionally poorer) one myself.

This topic is closed to new replies.

Advertisement