• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
leblebi

calling functions with arrays arguments

7 posts in this topic

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
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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 vector
vector<int> vec(2);
vec[0] = 5;
vec[1] = 9;

// Pass the vector object by reference to the function
ctx->SetArguments(0, (asDWORD*)&vec, 1);


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


// AS code
int 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
0

Share this post


Link to post
Share on other sites
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++
:)
0

Share this post


Link to post
Share on other sites
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]
0

Share this post


Link to post
Share on other sites
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)
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0