Exposing arrays

Started by
5 comments, last by robiwan 11 years, 9 months ago
Hi, just starting to delve into AngelScript and I have a question: I need to expose several arrays in my C++ code to the scripting engine (with type and length), which is the best way to do that ?

TIA
/R
Btw. Is there a good search feature here on gamedev (that does not use google) ?
Advertisement
if your array is std::vector you can register it and expose you vector as a global property i think.

check this thread
http://www.gamedev.net/topic/583414-stdvector-registration/page__hl__%2Bstd+%2Bvector+%2Bregister

about search:
right top of this page, click on gray google button inside search field and select "This Forum".
it is very good
Are you talking about static C++ arrays, e.g. int buffer[256]? Or std::vector type of arrays?

The std::vector type can be registered like mentioned in the post linked to by saejox.

The static C++ array can also be registered, but as it doesn't contain the length in a readily available form you'll probably want to implement a light wrapper to add the proper bounds checking.

If the array is a member of a class then it is probably easiest to register a couple of property accessor methods, rather than registering the type of the array itself.

How would you like the script to be able to access the array and its elements? Perhaps if you describe that it will be a bit easier for us to give more direct advice.

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

Well actually I might have several C++ arrays (const double* that is) that I'd like to expose as properties to the script as:

[source lang="cpp"]double[] in0 = getInput(0);
uint in0Length = in0.length();

double[] in1 = getInput(1);
uint in1Length = in1.length();
[/source]
Then I'd also like to be able to create arrays as:

[source lang="cpp"]double[] out0 = createOutput(0);
out0.resize(512);

double[] out1 = createOutput(1);
out1.resize(512);

[/source]
:)

How would you like the script to be able to access the array and its elements? Perhaps if you describe that it will be a bit easier for us to give more direct advice.

See the reply to my original post for one example of how to. Maybe even cleaner would be to have the C++ arrays registered as:

double[][] input;

but the getInput(arg) would suffice well.
The const double * array can be exposed to AngelScript, but you need to figure out a way to get length() working. I mean, how can the method length() know the size the array? The index operator also needs to know the length so it can make sure the script won't do bad things.

I suggest a light wrapper, e.g.


class Array
{
const double *buffer;
unsigned int length;
double at(unsigned int index)
{
if( index >= length )
{
// Raise an exception to the script
asIScriptContext *ctx = asGetActiveContext();
if( ctx )
ctx->SetException("Out of bounds");
return 0;
}

// Return by value as the array shouldn't be modified by the script
return buffer[at];
}
}


You can then register this light wrapper with the script engine. I suggest you give it a meaningful name, for example DataBuffer or something like that depending on what you'll use it for.

Are you planning on using the script array add-on in parallel, or will this be the only type of arrays supported in your app?

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 suggest a light wrapper, e.g.


class Array
{
const double *buffer;
unsigned int length;
double at(unsigned int index)
{
if( index >= length )
{
// Raise an exception to the script
asIScriptContext *ctx = asGetActiveContext();
if( ctx )
ctx->SetException("Out of bounds");
return 0;
}

// Return by value as the array shouldn't be modified by the script
return buffer[at];
}
}


You can then register this light wrapper with the script engine. I suggest you give it a meaningful name, for example DataBuffer or something like that depending on what you'll use it for.

Are you planning on using the script array add-on in parallel, or will this be the only type of arrays supported in your app?


Ok, I was thinking along those lines aswell, thanx. It might be the only type of array supported, but I need to be able to create arrays as well, so I'll tinker around with the array add-on, see if I can combine some ideas with the one you gave above for a lightweight wrapper.

This topic is closed to new replies.

Advertisement