Cast Handle to reference (like parameter reference)

Started by
0 comments, last by ThyReaper 10 years, 4 months ago

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

Advertisement

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

This topic is closed to new replies.

Advertisement