# Cast Handle to reference (like parameter reference)

This topic is 2145 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

(I've read this post about "pointers, references and values between C++ and AngelScript" http://www.gamedev.net/topic/608659-pointers-references-values-between-c-and-angelscript/ .. I'm not quite finding the answer to my question)

Let's say I've register a reference type TypeA (which the script cannot create, only operate on existing instances) and a TypeB.

RegisterObjectType("TypeA", 0, asOBJ_REF | asOBJ_NOCOUNT);
RegisterObjectType("TypeB", 0, asOBJ_REF | asOBJ_NOCOUNT);

...

RegisterObjectMethod("TypeA", "const TypeB& GetSomeComponent() const", asMETHODPR(TypeA, GetSomeComponent, (void) const, const TypeB&), asCALL_THISCALL);


In script I'm trying to do something like:

void main(TypeA& a)
{
const TypeB& b = a.GetSomeComponent();
...



This fails to compile the script  (with the error "Expected expression value").

If I change to the following, it compiles and runs:

// in C++
RegisterObjectMethod("TypeA", "const TypeB@ GetSomeComponent() const", asMETHODPR(TypeA, GetSomeComponent, (void) const, const TypeB&), asCALL_THISCALL);

// in script
const TypeB@ b = b.GetSomeComponent();


Questions:

• Is it OK to bind to a C++ function that's returning a reference (not a pointer) and feed that to AngelScript as a Handle?
• I'm guessing that the reference syntax is only available to function parameters? Is there no way to use it in the script body, even if I can guarantee that this object will never be NULL (so I don't want 'pointer'/handle syntax, which would imply that it could be NULL.. there's a reason I'm returning a reference and not a pointer in C++).

Thank you very much for your time and tips.

Edited by iraxef

##### Share on other sites

The C++ side can return a reference or a handle in that case. However, as you suspected, scripts can only use reference syntax in arguments. In your examples, both scripts would receive the return as a const TypeB@.

Edited by ThyReaper

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 9
• 11
• 15
• 21
• 26