Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


- - - - -

Exposing arrays


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 robiwan   Members   -  Reputation: 107

Like
0Likes
Like

Posted 21 June 2012 - 03:19 AM

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

Sponsor:

#2 saejox   Members   -  Reputation: 714

Like
0Likes
Like

Posted 21 June 2012 - 09:09 AM

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

#3 Andreas Jonsson   Moderators   -  Reputation: 3610

Like
0Likes
Like

Posted 21 June 2012 - 09:47 AM

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

#4 robiwan   Members   -  Reputation: 107

Like
0Likes
Like

Posted 23 June 2012 - 05:13 AM

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]
:)

#5 robiwan   Members   -  Reputation: 107

Like
0Likes
Like

Posted 23 June 2012 - 05:29 AM

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.

#6 Andreas Jonsson   Moderators   -  Reputation: 3610

Like
0Likes
Like

Posted 23 June 2012 - 09:43 AM

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

#7 robiwan   Members   -  Reputation: 107

Like
0Likes
Like

Posted 23 June 2012 - 10:19 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS