calling functions with arrays arguments

Started by
6 comments, last by WitchLord 19 years, 5 months ago
Hi. I need some help with calling AS functions that take arrays as inputs.

// AS Code
// This wont work
int adder ( int[] x )
{
return x[0]+x[1];
}

// I got this working with no problem
int adder_simple ( int x, int y )
{
return x+y;
}
and

// C/C++ Code
int *ptr;
ptr = (int*)malloc(sizeof(int)*2 );
ptr[0] = 5;
ptr[1] = 9;
ctx->SetArguments ( 0, &ptr, 1 );
Can anyone show me how to send input to adder() function? Thanks
Advertisement
if you have an adder function like

int adder(int x[]) // notice the brackets are _after_ the var
{
return x[0] + x[1];
}

you can call it like this:

int *ptr;
ptr = (int*)malloc(sizeof(int)*2 );
ptr[0] = 5;
ptr[1] = 9;

int sum = adder(ptr);

cout << sum << endl;

That's because ptr is a pointer as defined in int* ptr, but if you have an array e.g. int a[2], you can say just a and C will take that as a pointer pointing to the first element.
Example:

int* ptr1 = new int[2];
int ptr2[2];

Saying ptr1 and ptr2 are (almost) equivalent as both point the start of their memory.
One diference is that as for ptr2 the compiler knows at compile time the size of the array because you are saying it, this is not the case for ptr1.

sizeof(ptr1) will probably say 4 (most common size of a pointer)
sizeof(ptr2) will probably say 8 (2 elements time the common size of an int 4).

The other diference is that you can allocate any memory size for ptr1 being a pointer. The size of ptr2 cannot change.

Hope this help, if I'm wrong please somebody say so.
Ignore crypticmind. He didn't realize that we are talking about AngelScript and not C/C++. [wink]

Don't take me wrong, crypticmind. I appreciate that you're trying to help.

---

It's not quite that simple. You see, arrays in AngelScript are actually objects, not just pointers to a memory buffer.

If you want the application to interact with the arrays in AngelScript, then you first need to register the interface the application will use to interact with the array. I suggest you use the RegisterVector() function that Deyja wrote for this:

RegisterVector<int>("int[]", "int", engine);


Then after compiling the script you will be able to do the following:

// Initialize the vectorvector<int> vec(2);vec[0] = 5;vec[1] = 9;// Pass the vector object by reference to the functionctx->SetArguments(0, (asDWORD*)&vec, 1);


Don't forget to change the script function to take the array by reference, instead of by value.

// AS codeint adder(int[]& x){  return x[0] + x[1];}


If you don't do this you will have a hard time trying to pass the vector object by value to the script function. It can be done, but the code will become quite bloated and ugly since I haven't prepared the interface for passing objects by value to script functions. It would involve allocating memory, then copy constructing the object to the memory, then calling SetArguments() to copy the memory to the context stack, and finally deallocating the memory without destroying the object.

I'm going to write a detailed article on how exactly to call script functions with various types of parameters and return types. In the future the interface will also be very much improved.

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
It's not quite that simple. You see, arrays in AngelScript are actually objects, not just pointers to a memory buffer...



I never cared to notice this is AngelScript and not plain c/c++
:)
While we're talking about passing arguments to functions I've come up with...
BOOL Script_SetArguments( char *args, ... ){     asIScriptContext* ctx;     va_list marker;	     U32 count;     U32 dwordCount;     U32 stackPos = 0;	     va_start( marker, args );						     for (variableCount = 0 ; variableCount < strlen(args) ; variableCount++)     {          U32 i;          FP f;          VOID *p;          double d;          asDWORD *data;						          switch (args[variableCount])          {          case 'i':      i = va_arg( marker, U32 );                         dwordCount = sizeof( i ) / 4;	                         data = (asDWORD*) &i                         break;							          case 'f':      f = va_arg( marker, FP );                         dwordCount = sizeof( f ) / 4;                         data = (asDWORD*) &f                         break;				          case 'd':      d = va_arg( marker, double );                         dwordCount = sizeof( d ) / 4;                         data = (asDWORD*) &d                         break;				          case 'p':      p = va_arg( marker, VOID * );                         dwordCount = sizeof( p ) / 4;                         data = (asDWORD*) &p                         break;          default:       // should handle unknown argument type here                         break;          }						           ctx->SetArguments( stackPos, data, dwordCount );	           stackPos+=dwordCount;     }					     va_end( marker );}

You call it with something like
Script_SetArguments( "ifp", myU32, myFloat, myPointer );


It's (supposed) to do something similar to printf, in that the string supplied just identifies what variables to set. p is just a generic pointer.

Does this look like it will work? I was a bit unclear on the dwordCount and stackPos situations. So far I've had no problems with it but I'm a little scared I'm not cacking the stack up in some way.

Thanks

-- edit: that took ages to tab with space and the forum has just got rid of it all. ack! --

-- edit: thanks desertcube. Fixed the formatting! --

Scott

[Edited by - waheyluggage on November 25, 2004 2:21:50 PM]
Quote:Original post by waheyluggage
It's (supposed) to do something similar to printf, in that the string supplied just identifies what variables to set. p is just a generic pointer.

Does this look like it will work? I was a bit unclear on the dwordCount and stackPos situations. So far I've had no problems with it but I'm a little scared I'm not cacking the stack up in some way.

Thanks


If all you wan to do in the script is pass in different variables and turn them into a string, you may want to check out the iostream class, which allows you to do this:

cout << "Float:\t" << myFloat << "\tInt:\t" << myInt << std.endl;


Now the way the class is wrote, you could use a stringstream if all you want to do is convert the variables to a string and then use stringstream.str() to get that string.

Hope that helps.

Quote:
-- edit: that took ages to tab with space and the forum has just got rid of it all. ack! --

Scott


Use (code)(/code) tags (but change the () to [] or even the (source) tags (see the FAQ)
Hi DesertCube

I'm not worried about building the strings up, I would just use sprintf for it. My method for passing parameters to AngelScript is what I was on about. I use something similar to printf as in you give it a string to declare what type of variables are coming on the va_start parameters. I just wanted to know if what I'm doing is 'correct'. ie. I'm not misunderstanding about the way it works.

Scott
waheyluggage,

although what you wrote would seem to work, it may in fact cause trouble with float parameters. I've noticed that when C++ passes a float to functions that take variable number of arguments, it is in fact passing a double. That is why printf("%f", float(f)), works just as printf("%f", double(f)) does.

You also don't need to declared the variables i, f, etc. You could just do sizeof(U32), sizeof(float), etc, instead.

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