references giving me pain!

Started by
6 comments, last by WitchLord 19 years, 1 month ago
I'm trying to use references so that scripts can modify items i pass in. Once I get it working I'll be useing it for scripting some entity actions (like the "on use" event for triggers etc). As I can't register every trigger as a global, i figure I'll pass the trigger in as a reference and let the script do its thing. However I keep getting crashes. For testing i've got a script that looks like this:

// script function
void randomPoint(int &inout x, int &inout y)
{
   x = rand() % 10;
   y = rand() % 10;

   logInt(x);
   logInt(y);
}

// in my app
asIScriptContext *getPoint;

...

// every tick I call this to generate a new point
{
   getPoint->Prepare(asPREPARE_PREVIOUS);
   getPoint->SetArgDWord(0, x);
   getPoint->SetArgDWord(1, y);
   getPoint->Execute();
}


the logInt() function simple prints the value to my console. If the function doesn't use references it works perfectly, and if it does it always crashes. I've tried &in, &inout and &out and none of them seem to fix it. It always crashes with an access violation on line 1024 of as_context.cpp. Am I using references wrong? is there a trick? Are they working? hope someone can help [grin]
Advertisement
Actually, when calling a script function from the application the in, out, or inout doesn't make a difference. These flags are only used by the script compiler when it compiles a function call. Still, it's a good idea to use inout in this case, since it will make it clear to the script writer that the argument will be used both for passing values to the function and for returning values.

Your problem is that you're passing the variables by value, whereas you need to pass the address of the variables. You should be doing:

{   getPoint->Prepare(asPREPARE_PREVIOUS);   getPoint->SetArgDWord(0, (asDWORD)&x);   getPoint->SetArgDWord(1, (asDWORD)&y);   getPoint->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

yay - it works!

Thanks heaps!!!!!!
Its pretty crappy but thanks to your help I've uploaded the first working test I've created with AngelScript.

Top item in the downloads list.
cool :) I'll check it out as soon as I can.

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

Downloaded your demo, and while simple I found it quite interesting :)

I'd like to comment on your use of 'int &in' for the script function parameters. I suggest you use 'int &inout' instead since that is how you use them. In this case however it doesn't make a difference programmatically since the function is only called from the application, but if you were to call it from another script function then the value stored in the parameters for output wouldn't be copied back to the original variables.

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

Quote:Original post by WitchLord
Downloaded your demo, and while simple I found it quite interesting :)


Yeah its very simple. But it served its purpose which was to teach me how to use AS. I've got a new version (not uploaded, just for testing) where the script takes a reference to my vector class, and accesses a few global variables. I pretty much tried to put everything I'd ever want to use in the one demo just to test it all out and it all seems to be working - and so far this reference problem is the only thing I haven't been able to figure out pretty quick. The one thing I haven't tested yet is array's but I'll get round to that - I'm sure it'll be just as easy as the rest of it was!

&inout: Must remember to fix that! Its what I had originally but while trying to work out how to pass a reference I changed everything round - thanks for the heads up.

[Edited by - kaysik on March 28, 2005 7:15:14 PM]
&inout has one drawback that I'm not all that satisfied with. When used in functions called by scripts this modifier makes the function argument expression be evaluated twice, once to compute the value to pass in to the function, and once to copy the value returned from the function back to the original variable. The script compiler makes a warning if the argument expression is complex, i.e. calls any functions or alters any values, but still it might be confusing to the script writer.

My advice is to avoid &inout as much as possible, at least where the functions are called by scripts.

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