Advertisement

Support for STL-Containers?

Started by November 14, 2021 07:59 PM
1 comment, last by WitchLord 3 years ago

Hello,

what is the current best practice to interact with STL-Containers inside a script? E.g. if I have a C++ function “std::vector<Entity> GetObjectsInArea()”, how can it be used in AS?

I found nothing about this in the documentation. There is an addon for std::strings, which works very well, but nothing for other containers. There is however the CScriptArray, but this means, I have to copy my vector into a new CScriptArray each time I want to pass it to a script, plus this can be inefficient, since apparently, the Array stores pointers to each value, so a vector<int> can't be copied in one step using something like memcpy.

Additionally, there are few examples of how to use this class and especially the documentation about memory management is not quite clear to me. If I create a new Array and pass it to a script-function, at which point will it be deleted?

Since working with containers is such an omnipresent task, I think the documentation should at least contain an extensive ‘best practise’ guide to it, better yet, direct support by AngelScript would be awesome!

Hi Gracula,

unfortunately the memory model of the STL-containers in C++ don't make them very efficient when working from the scripts and automatic garbage collection. You can register them with the engine as value types, but that means they would be copied every time the container is passed to or from a function. You can also register them as ref types without garbage collection (asOBJ_NOCOUNT), but that would be subject to memory leaks as the GC would not be able to identify when to delete the instances.

Gracula said:
the Array stores pointers to each value, so a vector can't be copied in one step using something like memcpy.

CScriptArray stores primitives inline, so vector<int> can be copied with memcpy.

Gracula said:
If I create a new Array and pass it to a script-function, at which point will it be deleted?

The array will be deleted when there are no more references to it.

Gracula said:
I think the documentation should at least contain an extensive ‘best practise’ guide to it,

That's a good suggestion. I'll add that to my to-do list.

Gracula said:
direct support by AngelScript would be awesome!

Direct support is not likely going to happen. But you may want to take a look at Sami Vuorela's angelscript add-on template containers. They come pretty close to the standard STL-containers.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement