🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

native array support

Started by
27 comments, last by Deyja 19 years, 8 months ago
OK, just a suggestion on the syntax. Could you have something like this:

string [] a = string[5];

Not sure how easy that is to implement, but I think it doesn't look too bad. While I’m posting, I may as well ask a few questions about the actual array's.

1. Will you be able to add stuff to the end (i.e. std::vector push_back)
2. Are they bound checked? If so, is there an option to disable it (perhaps a special function call?)
3. How would we be able to pass them between functions? In C++ you use pointers, but what about in AS? Also, will you be able to pass C++ arrays (or vectors, I hardly ever use array's) to the AS program and vice versa?

Sorry for all the questions, I'm just being curious on their limitations.

Anyways, keep up the good work!
Advertisement
Rain Dog:

True, with fixed multidimensional arrays the compiler can optimize the multiplication to become a simple addition.

The arrays in AngelScript are not fixed, and can therefore not use such optimizations.

desertcube:

There will be someway of setting the size of the array in the declaration, but wether this will be with string[] a(4), or string[] a = string[4], I haven't decided upon. Maybe both will work.

1. It will not be possible in this first version, but I'm looking in to allowing the application to extend the array functionality.

2. Yes the arrays are bounds checked, for security. I will not provide a way for the scripts to access the arrays without boundschecking, but the C++ application can do so if it wishes.

3. The arrays can be passed between script functions just like any other variables. The array buffer is shared between the variable instances though. Arrays can be passed to the C++ application by reference. The application will then receive a pointer to a public interface with which it will be able to manipulate the arrays. Because the array object relies on information registered in the engine it will not be possible to move the array outside the engine. And certainly not between engines.

You can only pass a C++ vector to the scripting engine by registering a object type for it. I'm planning to allow the application to register special array objects that will replace the default array. To the script there will be no difference between these special arrays and the default array but the application will be able to receive the array object in the form it wishes (e.g. std::vector).

I've also been thinking about a way to allow the application to register a function parameter with a special modifier. When the script passes an array to a parameter that has this modifier the engine automatically calls a method to retrieve the internal buffer pointer from the array. That will allow you to receive a normal C++ pointer to an array without any special work.


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

What kind of data structure are you using for the arrays? Are it vectors, deques, ...? It would also be intersting to provide map support with custom defined index type (strings, for example). This is probably too much to ask for for, so I'm expecting this in this (or the next) version.
It is a simple array/vector. Nothing fancy.

A more complex structure, such as a map, can easily be registered by the application so I don't think I need to supply it in the library by default.

My goal with version 1.10.0 is the support for simple arrays of any type. It was the one major language feature missing from the library in my opinion. With the arrays AngelScript has everything that a basic language has. From here I will move into a higher level with structures and eventually classes with inheritance, polymorphism, etc.

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

Right now, you can already register any stl container, with the problem that you have to re-register a unique type for every kind of thing you want to stick in them. I'm doing it. With a bit of template and macro trickery, you can automagically register a vector of any type.

Create a function of the form 'template <class T> registerVector(std::string type_name)' that does the actual work of registering the type, and a macro such as '#define RegisterVector(A) { registerVector<A>(##A); }' to invoke it properly. The code to register the type then becomes 'RegisterVector(TypeName)'. Don't expect this to work in MSVC6.0 - for that, you'll need to include a paramter of type T in the template function, and modify the macro to declare and supply it. I think I'm going to go do this, right now. :D Any suggestions on how I should format the typename I register with angelscript?
Deyja, can you include a sample of this?
It's not as hard as you think. Right now, I am just wrapping up my vectors in global functions. I am working on some auto-binding code, though.

#define RegisterVector(A,B) { registerVector<A>(#A,B); }template <typename T>class registerHelper{public:	static void Construct(std::vector<T>* in)	{		new (in) std::vector<T>;	}				static void Destruct(std::vector<T>* in)	{		in->~vector();	}};template <typename T>registerVector(const std::string name, asIScriptEngine* engine){	assert(engine && "Passed NULL engine pointer to registerVector");	std::string n = "VectorOf";	n += name;	engine->RegisterObjectType(n.c_str(), sizeof(std::vector<T>), asOBJ_GUESS);		engine->RegisterObjectBehaviour(n.c_str(), 		asBEHAVE_CONSTRUCT, 		"void f()", 		asFUNCTION(registerHelper<T>::Construct), 		asCALL_CDECL_OBJLAST);	engine->RegisterObjectBehaviour(n.c_str(),		asBEHAVE_DESTRUCT,		"void f()",		asFUNCTION(registerHelper<T>::Destruct),		asCALL_CDECL_OBJLAST);		}


That will register the vector, and angelscript will be able to properly create and destroy them. Of course, they won't -do- anything. I just started working on this, what, half an hour ago? I have an idea of how to do the behavior. It's going to rely on the user registering the contained-type before registering the vector.
This will be a very good add-on code for the library. If you make it real nice looking I would like to distribute it in the standard download, just as I do with the bstr and std::string add-on.

I will make AngelScript allow the registration of array objects that override the native array. For the script writer this will be transparent, but the application will be able to receive the exact object type that it wants to work with. The registered array object will most likely be faster than the native array as well, as the native array is completely dynamic and needs to look up the contained object's methods as it is constructed, destroyed, or resized.





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

Since Angelscript's arrays are dynamic, vector doesn't add anything angelscript can't already do. In fact, as soon as you get script-defined classes working, I could liberally apply my pre-processor and create generic lists right in angelscript itself. Macros can go a long way towards mimicing templates.

But I will see if I can produce a generic vector binder for you. Any particular features you are looking for?
My needs for a vector are really simple.

I need a vector<int> and a vector< vector<int> >

I am entirely too unfamiliar with the way that binding these objects in angelscript works.

This topic is closed to new replies.

Advertisement