ref is only available in the scripts if the application registers the support for it.The ref type works like a generic object handle. Normally a handle can only refer to objects of a specific type or those related to it, however not all object types are related, and this is where ref comes in. Being completely generic it can refer to any object type (as long as it is a reference type).
// Two unrelated types
class car {}
class banana {}
// A function that take the ref type as argument can work on both types
void func(ref @handle)
{
// Cast the handle to the expected type and check which cast work
car @c = cast<car>(handle);
banana @b = cast<banana>(handle);
if( c !is null )
print('The handle refers to a car\n');
else if( b !is null )
print('The handle refers to a banana\n');
else if( handle !is null )
print('The handle refers to a different object\n');
else
print('The handle is null\n');
}
void main()
{
// Assigning a ref handle works the same way as ordinary handles
ref @r = car();
func(r);
@r = banana();
func(r);
}
The ref object supports only a few operators as it is just a place holder for handles.
@= handle assignment
The handle assignment operator is used to set the object that the referred to by the ref type.
is, !is identity operator
The identity operators are used to compare the address of the object referred to by the ref type.
cast<type> cast operator
The cast operator is used to perform a dynamic cast to the desired type. If the type is not compatible with the object referred to by the ref type this will return null.