Sign in to follow this  
TechRogue

Quick question about the language

Recommended Posts

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:

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

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

Share this post


Link to post
Share on other sites
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:

[code]
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
[/code]

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.

Share this post


Link to post
Share on other sites
Yup. You just don't use new, your example would look like
[code]
Entity@[] array;

for( uint32 i = 1; i < 10; ++i )
{
array.[color=#000000][size=3]insertLast( Entity() );[/size][/color]
}
[/code]

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

EDIT2 : GDNet source tag, y u so bad

Share this post


Link to post
Share on other sites
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.
[code]
ParamList par();
string foo="345";
par.addParam(bleh);
BaseEntity@ ent3=System.instanciate("AbstractNPC","AbstractNPC @AbstractNPC(int,string)",par);
[/code]
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this