Sign in to follow this  

Exposing arrays

This topic is 2033 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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) ?

Share this post


Link to post
Share on other sites
if your array is std::vector you can register it and expose you vector as a global property i think.

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

about search:
right top of this page, click on gray google button inside search field and select "This Forum".
it is very good

Share this post


Link to post
Share on other sites
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 [url="http://www.angelcode.com/angelscript/sdk/docs/manual/doc_reg_objprop.html"]register a couple of property accessor methods[/url], 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

Share this post


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

Share this post


Link to post
Share on other sites
[quote name='Andreas Jonsson' timestamp='1340293658' post='4951389']
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.
[/quote]
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.

Share this post


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

[code]
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];
}
}
[/code]

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?

Share this post


Link to post
Share on other sites
[quote name='Andreas Jonsson' timestamp='1340466212' post='4952025']
I suggest a light wrapper, e.g.

[code]
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];
}
}
[/code]

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?
[/quote]

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.

Share this post


Link to post
Share on other sites
Sign in to follow this