Sign in to follow this  
Oscar1

Object Handle vs Object reference

Recommended Posts

Oscar1    115

Since I can use Object reference as parameters in my functions and the object handle is in fact NOT a pointer, is there really any reason of using (or even having) object handles? I just can't find that good reason.

 

The only difference I see is that I can reseat the object handle with different object and I can also use it as a return parameter of function.

 

I really don't know, I wanted to wrap my head around the need for object handles - especially the reason why I would want to use them, but since I can pass reference in functions, I can't find any reasonable reason to use them.

Is there some safety issue, speed issue or something else? Is there something obvious I don't see?

 

Give me some ideas. Thanks. Love the angelscript, I just want to understand it better.

Edited by Oscar1

Share this post


Link to post
Share on other sites
FDsagizi    514
obj ref - use ref couner to variable, if ref count > 0 varialbe is live!
obj handle not use this and can be not a valid. Edited by FDsagizi

Share this post


Link to post
Share on other sites
saejox    714

They are very different. You cant assign a reference for example.

Handle is a safe refcounted/garbage collected pointer, ( like shared_ptr in c++ but safer )

Reference is same as reference in c++ but not assignable.

 

void Func( Object &obj )

{

   Object &o = obj; // not possible in angelscript

}

Share this post


Link to post
Share on other sites
WitchLord    4677

The difference is similar to * and & in C++. A handle @ can be null, but a reference & can't be null. A @ can be reassigned to point to another object after initialization, a & can't be reassigned.

 

In C++ to declare a function that returns a new object in an argument you would write something like this:

 

void CreateObject(obj **);

 

In AngelScript the same would look like this:

 

void CreateObject(obj@ &out);

 

When declaring a function in AngelScript to take a @ as argument you're telling the caller that the argument can be null, and when you declare the argument as & then you tell the caller that the argument cannot be null and must be given a valid object. In C++ you tell the same thing with * and &.

 

There is no difference in safety. AngelScript is a sandboxed (at least if the application only registers types and functions that behave properly), so both @ and & are equally safe and neither will result in risk of memory leak or dangling pointers, etc.

 

There can be a slight difference in performance when using one over the other, but it is not something you should be counting on as it can vary a lot from situation to situation.

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