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.