🎉 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!

AngelScript 1.8.0 WIP #2 (2004/06/26)

Started by
46 comments, last by WitchLord 19 years, 12 months ago
I get what you are saying but I don't get how to implement it.

the soldier will have a function ID to tell what happens when he dies. Since it is a soldier, the module to be used is assumed to be "PEOPLE" or something. So the function ID could be obtained like so:

acCString str; str.Allocate(256, false);
soldier->ID = engine->GetFunctionDeclaration(engine->GetFunctionIDByName(mod, "main"), str.AddressOf(), 256);

To load and get the script file ready to be accessed by the scripting engie I would call LoadScript? Okay, here is my loadscript code:

int LoadandCompile ( asIScriptEngine *engine, const char *filename, char *mod )
{
FILE *f = fopen(filename, "rb");
int len = _filelength(_fileno(f));
acCString code; code.Allocate(len, false);
int c = fread(code.AddressOf(), len, 1, f);
fclose(f);
int r = engine->AddScriptSection(mod, code, len);
COutStream out; r = engine->Build(mod, &out);
}

but AddScriptSection does not take 3 parameters.

Then to call the soldiers die function you call
int ExecuteFunction ( asIScriptEngine *engine, const char *mode, int function )
{
asIScriptContext *ctx;
int r = engine->CreateContext(&ctx);
r = ctx->Prepare(function);
r = ctx->Execute();
return 0;
}

but, Prepare does not take 1 parameter.

I guess that would be the way to do it, right? What is the most efficient way? Obviously writing all that code to load a script and set functions and whatnot is a lot of writing. That is why I think making functions like the ones above would help, but I don't think they are right. I admit that I don't understand how to use as, but I am kinda picking up on it.



Advertisement
Ok, I can't tell you how exactly you should implement things. Everyone have their own preferred solution. Not even I know what is best since I don't use AngelScript. I don't have the time to write any projects using AngelScript.

The problem about the wrong number of arguments is probably because you haven't downloaded the latest 1.8.0 WIP #3. Or you haven't updated the angelscript.h file in your project.

Your function for loading and compiling the script is fine. If you want to compile more than one script section into the same module you should only call Build() after you have added all the sections.

GetFunctionDeclaration() is only there so that you can get the function interface declared by the script. You don't need to store this string. To get the function ID you ought to use GetFunctionIDByDecl() since it guarantees that the function you're calling is using the interface that you expect.

int funcID = engine->GetFunctionIDByDecl("mymodule", "void DoSomething(float)");

This call is relatively slow so it is recommended that you store the funcID for later use.

To call a script function you need to prepare a context. Then call Execute() on it.

asIScriptContext *ctx;
engine->CreateContext(&ctx);
ctx->Prepare(funcID, 100); // Can't remember right now if there are more parameters, check angelscript.h or the app writer's manual
float arg = 1.0;
ctx->SetArguments(0, &arg, 1);
ctx->Execute();
ctx->Release();

You could optimize it some more by reusing the context for another execution. Just call Prepare() again, and then Execute().

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

I can't thank you enough for being one of the most patient guys I've ever communicated with. This whole thread is pretty informative for anyone trying to learn angelscript.
Problem when calling this function. The thing it craps out on is the r = engine->AddScriptSection(); What is the difference between mod and name, and what should I put for len? I downloaded the sources just now, and it didn't change anything for the parameters.

int LoadandCompile ( asIScriptEngine *engine, asIScriptContext *ctx, const char *filename, char *mod )
{
FILE *f = fopen(filename, "rb");
int len = _filelength(_fileno(f));
acCString code; code.Allocate(len, false);
int c = fread(code.AddressOf(), len, 1, f);
fclose(f);
hge->System_Log("%d size adding script section..", len);
int r = engine->AddScriptSection(mod, "flatch", code, len);
//COutStream out;
hge->System_Log("building engine");
r = engine->Build(mod, NULL);
hge->System_Log("creating context");
r = engine->CreateContext(&ctx);
if( r System_Log("Failed to create a context"); return -1; }
return 0;
}
You're most welcome :)

The module parameter is the name of the module, which basically is the namespace for the script section.

The name parameter is the name of the script section. This name will only be used during the build for writing error messages. A good choice for this parameter is the filename of the script.

I can't see any obvious errors in your code. Does AddScriptSection() return a value, or does the application crash? Is the crash an assert failure, or something else?

If you receive a return value your parameters are probably wrong somehow.

If you receive an assert failure, it could mean a bug in AngelScript.

If you application crashes for other reasons my guess is that somehow you are linking to the wrong version of AS. Remember that differences between WIP versions are not detected by asCreateEngine() as the version number is the same.

- 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

I have downloaded and completely reinstalled the newest WIP of angelscript. The file I downloaded was angelscript_sdk_180WIP3.zip
I also completely removed any other version of the lib, but still the parameters from what you say it should be and the parameters I have for some functions are different. I rebuilt the lib and everything (msvc) so I don't know what's wrong. Maybe you didn't update the msvc workspace or something? Maybe I need to do something to my project?
just using the version I have, here is what I am having trouble with. When I try the GetFunctionIDbyDecl() I get different error codes. The last I tried I got -1 which is just error.
When the program gets to the line ctx->Prepare(f, 1000); it crashes (in debug mode) and points to that line of code. You said the crashing may be a result of version conflicts, but I have downloaded the most recent WIP.
Here is my code and how it basically is in my program:

/* script file */
void main()
{
SND_PWRUP = 5;
return 0;
}

/* Loading */

engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
ConfigureEngine(engine);
ctx = NULL;
LoadandCompile( engine, ctx, "flatch.as", "" );

/* Load and compile */

int LoadandCompile ( asIScriptEngine *engine, asIScriptContext *ctx, const char *filename, char *mod )
{
FILE *f = fopen(filename, "rb");
int len = _filelength(_fileno(f));
acCString code; code.Allocate(len, false);
int c = fread(code.AddressOf(), len, 1, f);
fclose(f);
int r = engine->AddScriptSection(mod, filename, code, len);
//COutStream out;
r = engine->Build(mod, NULL);
r = engine->CreateContext(&ctx);
context"); return -1; }
return 0;
}

/* execute the script */

int f = engine->GetFunctionIDByDecl("", "void main()");
int r = ctx->Prepare(f, 1000); assert ( r > 0);
ctx->Execute();
I wont be using WIP#3 for now(will wait for the full release), but i wonder if adding different script sections would help debugging, i.e, if there is an error on line 3 col 45 of the script file, would the error be poitted out relative to the file or to the section.

Wonder how many changes i would have to make for 1.8.0 from 1.7.1 :)
Jayanth.KRaptor Entertainment Pvt. Ltd.http://www.raptorentertainment.com---------------------------------------------------------Why Mr. Anderson? Why? ...Why keep fighting? Do you think you're fighting for something - for more than your survival? Can you tell me what it is? Do you even know? Is it freedom, or truth, perhaps peace, could it be for love? Illusions Mr. Anderson, vagaries of perception. Temporary constructs of a feeble human intellect trying desperately to justify an existence without meaning or purpose.
EddHead:

Since AngelScript doesn't know anything about files it reports errors relative to the start of script sections.

I will do my best to make the upgrade to 1.8.0 as smooth as possible.

Anonymous Poster:

I can't see any obvious errors in your code. Could you tell me what the Build() method returned? And what GetFunctionIDByDecl() returned?

I'm not ruling out a bug in AngelScript so I'll be making some tests here, but you can help me by answering the above questions.

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 got a login now.

build returned 0. Create context returned 0. GetFunctionIDbyDecl returned -1.

If you want I can send you the full source and workspace. I probably am doing something wrong.

Here is where I try to call a function in the script ctx:
if (ctx) {				int function = engine->GetFunctionIDByDecl("", "void main(void)");		hge->System_Log("function ID for main: %d.. Ctx: %d", function, ctx);		if ( function > -1 ) 		{			int r = ctx->Prepare(function, 1000); 			hge->System_Log("prepare %d = %d", function, r);			hge->System_Log("SND_PWRUP: %d", SND_PWRUP);			ctx->Execute();			hge->System_Log("SND_PWRUP: %d", SND_PWRUP);		}	}

This topic is closed to new replies.

Advertisement