Advertisement

"special" function calls (catch?)

Started by September 04, 2005 08:24 PM
6 comments, last by WitchLord 19 years, 2 months ago
Hi there i've been trying AngelScript for a few days and find it so useful (and lack of documentation :P). Now, into the busyness: i need to make a different type of function call, which would create a new instance of that function, instead of the normal run. I explain: The main idea of this is create a pseudo multy-process management class, like the example "concurrent" that comes with AngelScript, with just a few differeces: -instead of stoping every Script (in my case: process) after a certain amount of time, every process can call a function Frame () to tell the manager it has done everything it needs to do in that frame, and thus, be sent to sleep In fact, this "frame" function is a refference to some function in the App which simply Suspends the context -Every Process is in fact, a Context. The Manager stores the Contexts into a vector, so we can do something like:

... 
for ("every Context")
   Manager.ContextVector->Execute ();
End For
...
Remember: Scripts will pause themselves with a Call to Frame () My Problem: Well, i've already managed to implement the Frame () related stuff thanks to the functionality of angelscript: Register a Global Function: Frame, which simply calls ActiveContexts->Suspend (). What i CANT do and cant figure out how to do is: Catch Function Calls within the scripts. Everytime a script calls a function, this function call shoudld be taken by the App and passed to the Manager, which will then allocate the contexts and do the necessary stuff to create a new process. any ideas? EDIT: actually, what i need is a way to thell the App "hey, create a new instance of *this* Process using *this* parameters". Something like
Create_Process (MyHappyProcess, Param1, Param2, ... , ParamN)
where the N Parameters will be passed to MyHappyProcess when it is created [Edited by - ALRAZ on September 4, 2005 8:46:53 PM]
format c: /q
Hi Alraz,

I'm happy you decided to stay with AngelScript, despite your feelings about the poor documentation (which I agree with).

It seems that what you want is co-routines, or at least something similar to them. Did you take a look at the co-routines example already? That sample shows how to register functions as CreateCoRoutine() and Yield() so that scripts can do the following:

struct ThreadArg                           {                                            int count;                                 string str;                              };                                         void main()                                {                                            for(;;)                                    {                                            int count = 10;                            ThreadArg a;                               a.count = 3;                               a.str = \" B\";                            CreateCoRoutine(\"thread2\", any(@a));     while( count-- > 0 )                       {                                            Print(\"A :\" + count + \"\\n\");          Yield();                                 }                                        }                                        }                                          void thread2(any &in arg)                  {                                            ThreadArg @a;                              arg.retrieve(@a);                          int count = a.count;                       string str = a.str;                        while( count-- > 0 )                       {                                            Print(str + \":\" + count + \"\\n\");     Yield();                                 }                                        }                                          


The CreateCoRoutine() function is implemented like this:

void ScriptCreateCoRoutine(string &func, asIScriptAny *arg){	asIScriptContext *ctx = asGetActiveContext();	if( ctx )	{		asIScriptEngine *engine = ctx->GetEngine();		string mod = engine->GetModuleNameFromIndex(ctx->GetCurrentFunction()>>16);		// We need to find the function that will be created as the co-routine		string decl = "void " + func + "(any &in)"; 		int funcId = engine->GetFunctionIDByDecl(mod.c_str(), decl.c_str());		if( funcId < 0 )		{			// No function could be found, raise an exception			ctx->SetException(("Function '" + decl + "' doesn't exist").c_str());			return;		}		// Create a new context for the co-routine		asIScriptContext *coctx = engine->CreateContext();		if( ctx == 0 )		{			// No context created, raise an exception			ctx->SetException("Failed to create context for co-routine");			return;		}		// Prepare the context		int r = coctx->Prepare(funcId);		if( r < 0 )		{			// Couldn't prepare the context			ctx->SetException("Failed to prepare the context for co-routine");			coctx->Release();			return;		}		// Pass the argument to the context		coctx->SetArgObject(0, arg);		// Join the current context with the new one, so that they can work together		contextManager.AddCoRoutine(coctx);	}}


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

Advertisement
i just dont understand how you do to separate each parameter

format c: /q
Well, I don't.

AngelScript doesn't support variable parameters. So if you want to be able to send different types of parameters to different functions, you'll have to register several versions of the CreateCoRoutine() function (or whatever name you choose).

In my case, I choose to register only one version that takes a parameter of the any type. This allows the scripts to declare a structure with all the parameters they wish to pass to the other function, and the other function can safely obtain these parameters by retrieving the correct structure type.

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
Got a better understanding now

also noticed that there is no One-line assingment to struct
(dont know the name...)

i mean

struct Text{  int X;  int Y;  string Name;};void InitialFunction (){   Text Msg;      Msg = {ScreenX / 2, 50, "Hello World"};  // THIS!!!!   }


it could save many lines of code for the script writter
format c: /q
Yeah, I know. I'm not sure if it is the correct term but I call it initialization lists, and I may implement these for structs as well. But only after I have them working for arrays (which should hopefully be soon enough).

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

Advertisement
going back into the "special function calls"

am still looking for a simplier way to implement an easy-to-use script function or reserved word to make something similar to fork (or create coroutine)

example:

void Enemy (int X, int Y, int ID){   while (1)   {      //do something ...      Frame ();   //has done all it needs to do on a single frame   }}void Spawn_Enemies (){   spawn Enemy (10, 10, 200);   spawn Enemy (20, 75, 28);   spawn Enemy (30, 100, 40);}


the "spawn" reserved word would call an APP function which would then create a context to the function (create a coroutine) with the apropiate params.

should i look at AngelScript's source? XD

[Edited by - ALRAZ on October 1, 2005 8:56:44 PM]
format c: /q
If you want the syntax like that, then you have two choices:

1. Modify the AngelScript code to accept that kind of syntax and do what you need.

2. Use a preprocessor to convert that syntax into something that AngelScript can understand.

In a distant future version I might implement built-in co-routines like what you want. I say distant future, because there are lots of other more important features that needs to be implemented first, especially since co-routines can be added like it is done in the sample.

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