🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

State based function calls

Started by
3 comments, last by WitchLord 19 years, 2 months ago
Hi, I was wondering if it is possible to make state based function calls in Angel script like we can in UT script or some other scripting languages that I have seen. If you are not familiar with what a state based call is let me show you a small example lets say we have a script as follows float function1() { /// code code code // a state based call state_call function2() ; } float function2() { // code code code } void main() { function1() ; } basically what happens here is that when function1 calls function2, the stack used by function1 is discarded and only then is function2 called. This saves memory as is quite usefull in games. Can the stack of a function be discarded in anglescript either by calling another function or changing the calling convention in some way? Thanks, - Sid
Advertisement
Hi Sid,

There is no built-in functionality in AngelScript to do this yet.

Something similar could be done like this:

// AngelScript codevoid function1(){  ... do something  SetState("function2");}void function2(){  ... do something  SetState("function3");}void function3(){  ... do something  // No more states}


SetState(string) is an application registered function that lets the script tell the application what function it should call once the current one exits.

The application code would then look something like this:

// C++ code (parameters may not be correct)string stateFunc;void main(){  SetState("function1");  while( stateFunc != "" )  {    int func = engine->GetFunctionIDByName(stateFunc);    stateFunc = "";    ctx->Prepare(func);    ctx->Execute();  }}void SetState(string &nextFunc){  stateFunc = nextFunc;}


I think supporting a feature like this natively may be a bit complicated with the way AngelScript is implemented. Still, I will give it some more thought before discarding it completely.

Except as a memory saving technique, is it used for something else?

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

I noticed that your "function1" doesn't return anything, is "function2" meant to return its value in place of "function1"?

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 for your help WitchLord.

I'm not looking for native support, so the solution you have given should work fine (I still have not looked at it in detail).

A small drop in performance also would not be big deal in my case.
As far as I can remember the scripting language that I was using (GameMonkey) was doing something like what you have given. I don't think they had native support either. Also state based function calls don't return anything in game monkey. I can't seem to remember how it was in UT script.

I think you should seriously consider this feature for your engine. It looks like state based function calls seem to be an integral part of a scripting language for games (or is that just an impression I got from 2 scripting engines ??).

Thanks again for your help. I will try and implement your answer now.

- Sid
I will definitely investigate this further. But so far I can't really see the big advantage of this, except that it might save some memory on the stack.

The funny thing is that I know almost nothing about any other scripting languages (except &#106avascript and VBScript). I'm building AngelScript on my knowledge of normal programming languages, so what I add to the library most of the time has a founding in C++ or similar language.

I like the feedback from AngelScript users, since that lets me know what features are the most needed or wanted.

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