# C++ STL-Style Array That's Compatible with AngelScript

## Recommended Posts

Squared'D    2427

I've written a wrapper for the CScriptArray add-on that makes it so you can use it almost exactly like std::vector. I wrote it so I could more easily share arrays with the C++ program and AngelScript. I've written a blog post for it. You can find the CScriptArraySTL class here:

http://squaredprogramming.blogspot.kr/2014/01/c-stl-style-array-thats-compatible-with.html

or

http://www.gamedev.net/blog/1670/entry-2259127-c-stl-style-array-thats-compatible-with-angelscript/

Let me know if you have any problems or if it's lacking any important functionality. I hope it's useful to others.

##### Share on other sites
WitchLord    4677

Interesting. I'll take a closer look at this later on. Perhaps I can incorporate part of into the CScriptArray add-on directly (with your permission of course).

##### Share on other sites
iraxef    451

You're allowing C++ and script to easily share data through a 'global' (actually a global C++ variable, or at least lives long enough to cover the execution of a given script coroutine) instance of e.g. a string array?

Am I correct in guessing that this wouldn't work if you were running simultaneous coroutines via threads?

##### Share on other sites
Squared'D    2427

Interesting. I'll take a closer look at this later on. Perhaps I can incorporate part of into the CScriptArray add-on directly (with your permission of course).

I hope it's useful. The code is completely open so if you'd like to incorporate it that'll be ok. I'm glad that I'm able to contribute.

##### Share on other sites
Squared'D    2427

You're allowing C++ and script to easily share data through a 'global' (actually a global C++ variable, or at least lives long enough to cover the execution of a given script coroutine) instance of e.g. a string array?

Am I correct in guessing that this wouldn't work if you were running simultaneous coroutines via threads?

The main purpose for writing the CScriptArraySTL class was so I could manipulate a global array in C++ and AngelScript directly without copying data or manually casting void pointers. Underneath, this uses the CScriptArray class that's in the AngelScript add_on folder so it can do everything that CScriptArray can do. CScriptArraySTL creates a CScriptArray object and that object is what can be registered. If you register it as a global property, the object should exist as long as the script is running, but becuase CScriptArray is a reference type object, you could also return a handle to it from a function. If you do this, the reference counted CScriptArray would persist until the reference count reaches zero.

Right now it can't using an existing array, but that's something I'm thinking about adding. Then you'll be able to use it to manipulate arrays created inside a script.

This example can be found here: http://www.angelcode.com/angelscript/sdk/docs/manual/doc_addon_array.html

// Registered with AngelScript as 'array<string> @CreateArrayOfString()'
CScriptArray *CreateArrayOfStrings()
{
// If called from the script, there will always be an active
// context, which can be used to obtain a pointer to the engine.
asIScriptContext *ctx = asGetActiveContext();
if( ctx )
{
asIScriptEngine* engine = ctx->GetEngine();

// The script array needs to know its type to properly handle the elements.
// Note that the object type should be cached to avoid performance issues
// if the function is called frequently.
asIObjectType* t = engine->GetObjectTypeById(engine->GetTypeIdByDecl("array<string>"));

// Create an array with the initial size of 3 elements
CScriptArray* arr = new CScriptArray(3, t);
for( asUINT i = 0; i < arr->GetSize(); i++ )
{
// Set the value of each element
string val("test");
arr->SetValue(i, &val);
}

// The ref count for the returned handle was already set in the array's constructor
return arr;
}
return 0;
}


I can re-write it using the CScriptArraySTL class so I can write it like std::vector

// Registered with AngelScript as 'array<string> @CreateArrayOfString()'
CScriptArray *CreateArrayOfStrings()
{
// If called from the script, there will always be an active
// context, which can be used to obtain a pointer to the engine.
asIScriptContext *ctx = asGetActiveContext();
if( ctx )
{
asIScriptEngine* engine = ctx->GetEngine();

CScriptArraySTL <string> arr;

// The script array needs to know its type to properly handle the elements.
// Note that the object type should be cached to avoid performance issues
// if the function is called frequently.
// create the array with an initial size of 3
arr.InitArray(engine, "array<string>", 3);

// use iterators to fill it
for(CScriptArraySTL <string>::iterator it = arr.begin(); it < arr.end(); ++it)
{
*it = "test";
}

// The ref count for the returned handle was already set in the array's constructor
// added parameters to GetRef() 2014-01-06
return arr.GetRef(false /*don't increment the reference count*/, true /*erase data from this object when done*/);
}
return 0;
}


Note: Thanks for this question. Because of this, I changed GetRef() to take two optional parameters.

##### Share on other sites
iraxef    451

Thanks for this - I look forward to studying the code more closely as I get started with AngelScript myself.

While we're on this topic.. does the primary difficulty with std::vector and script interoperability lie with the templated nature of the C++ container, which is resolved at C++-compile-time... so existing attempts to bind std::vector are themselves templated and require upfront binding for every type you want to expose... which would mean that you could expose (to script) std::vectors of all the primitive types and your registered application types... but when dealing with a vector in script, it would never be able to hold script-created types (such as script-class instances)?

##### Share on other sites
WitchLord    4677

@iraxef

This is exactly the reason why registering the std::vector can't be used for the dynamic script array. Since the C++ template is resolved at compile time there is no way for the script engine to create instances for each subtype used by the script dynamically. The application would have to register all possible types at start-up, which isn't viable, since the number of combinations can be infinite.

It is possible to use and register the std::vector for specific types, e.g. if you have situations where the script needs to manipulate a std::vector owned by the application, then you could register the std::vector of that particular type with AngelScript at start-up and then allow the script to interact with it directly. Remember that the std::vector won't treat incorrect usage in a friendly manner (i.e. out-of-bounds checks, etc), so if you want to get a friendly script exception if the script tries to access elements that don't exist you'll need to wrap these methods of the std::vector and add this check on your own.

##### Share on other sites
iraxef    451

If you're binding the add-on which creates the 'ref' script type.. could you bind an std::vector which contains CScriptHandle (or CScriptHandle*) and then you'd have a script-side std::vector type that can store any object handle?

##### Share on other sites
WitchLord    4677

Yes. That is perfectly possible.

## Create an account

Register a new account