Advertisement

Object Handle vs Object reference

Started by May 16, 2013 12:51 AM
2 comments, last by WitchLord 11 years, 6 months ago

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.

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.
Advertisement

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

}

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.

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