Quick question about the language

Started by
7 comments, last by KuroSei 12 years ago
I'm looking into using Angelscript for my engine and I just wanted to be clear on instantiation. Does Angelscript allow instances to be created on the "heap" (for lack of a better term)?

For example, in a previous project I was able to do something like this:


var array = [ ];
for (i in 1...10)
{
array.push(new Entity());
}


If I'm going to be able to do any kind of dynamic entity creation this needs to be possible in the language I choose for embedding. I didn't see "new" in the list of reserved keywords, and I wanted to be sure this was possible before investing too much time implementing it,

Thanks,
Jake
Advertisement
The keyword 'new' is not part of the language, but all reference types are allocated on the heap.

Only value types are allocated locally on the stack.

In AngelScript your example might look like the following:


array<Entity@> arr; // array of handles to instances of Entity
for( int i = 1; i <= 10; i++ )
arr.insertLast(Entity()); // allocate new Entity and store a handle to it in the array


A handle is sort of like a smart pointer in C++. As long as there are valid handles referring to an object on the heap the object will be kept alive.




I've been considering introducing the 'new' keyword in the language for some time, just to make it more explicit that the object is allocated on the heap.

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

Yup. You just don't use new, your example would look like

Entity@[] array;

for( uint32 i = 1; i < 10; ++i )
{
array.[color=#000000]

insertLast( Entity() );
}



EDIT: Ninja'd. I would back the 'new' keyword idea, though.

EDIT2 : GDNet source tag, y u so bad
clb: At the end of 2012, the positions of jupiter, saturn, mercury, and deimos are aligned so as to cause a denormalized flush-to-zero bug when computing earth's gravitational force, slinging it to the sun.
Fantastic! That's exactly what I needed to know. Thanks for the quick reply (edit: replies). :)
I encountered a similar problem.
It has to do with "lamba" arguments and is exactly what you encountered, i guess. The problem is, that i need to store the reference.

Simplified my problem is this: I have a "generic" List e.g. ParamList. It has the method void " addParam(void* param, int tId); " registered as " void addParam(const?&in) ".
Calling the Method works fine. The reference works okayish in the addParam method itself. My problem is, that i need to store the reference.

Three ways to do so now, i guess:

1.

ParamList par();
string foo="345";
par.addParam(bleh);
BaseEntity@ ent3=System.instanciate("AbstractNPC","AbstractNPC @AbstractNPC(int,string)",par);

Works fine, as long as i use the Parameter list in the scope in that foo exists.
But when i store the ParamList for caching issues or stuff it kinda breaks. :P

2. A Wrapper of a reference type like the any addon around the parameter itself
( Like addParam(any(param)); )

3. Registering an interface of the ParameterList and writing a scriptClass implementing that Interface. It would automaticly increase the reference count then if i stored it there.


But im not very comfortable with those solutions. I wonder if there is a better way, or if i can register the addParam method in a different way so the copy of the reference wont be destroyed when the lamba goes out of scope.
Help me, oh grandmaster of angelic scripting, I need your wisdom and swear to obey. ._.

Really, im crawling around the source for some time now and I am growing tired of this problem.
I would simply copy the contents if it wasnt a void* i got there. Makes allocating memory hardly possible, ka?
If you want to store a handle to script object, you need to call AddRefScriptObject. Also garbage collector must be aware that your class is storing handles or you will end up with memory leaks.

scriptdictionary add-on provides good example on how to implement and register such class.
I will look into that.
Thanks so far. :)
If you plan on storing a reference to the object you need to make sure you're receiving a handle, otherwise you may not be receiving a reference to the original object.

The &in parameter type sends the argument by reference but it will not always be a reference to the original object. In some cases AngelScript makes a local copy of the object and sends the reference to the local object instead. This is done to guarantee the validness of the reference throughout the execution of the function.

The script dictionary add-on is a good place to look, as is the script any add-on.

Alternatively you may want to take a look at the script handle add-on, which is a generic handle that can point to any type. It is probably easier to use than the variable argument ?&in.

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

I worked myself through the any-addon. It seemed quite hilarious what effort one has to put in, but now that it works, its not as bad as i thought at the beginning. The mechanics are quite simple but thats what makes it hard for me to comprehend. :D

Anyway, keep up the nice work, Andreas. I fell in lvoe with angelscript, even tho i rly feel kinda stupid sometimes. Well, thats life, isn't it?

This topic is closed to new replies.

Advertisement