Jump to content

  • Log In with Google      Sign In   
  • Create Account

- - - - -

AngelScript "this"?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 AnantaDas   Members   -  Reputation: 109

Like
0Likes
Like

Posted 20 January 2013 - 08:16 AM

Hello, I've got a question about AngelScript. I need to pass a reference to a script class instance into C++ for future access, so I created a function and exported it to AScript using "void InitScriptInstance( ?&in )". It works fine but the problem is I don't know how to pass a reference to current object (like 'this' in C++) - when I try to write:

 

class MyClass
{
   MyClass()
   {
      InitScriptInstance( this );
   }
}

 

The program goes into an infinite loop. If I replace the 'this' with something else (like 5) it works well but this obviously isn't what I need. Anyone knows the correct syntax?

 

Thanks



Sponsor:

#2 Andreas Jonsson   Moderators   -  Reputation: 3455

Like
0Likes
Like

Posted 20 January 2013 - 09:00 AM

The problem is that with &in the reference is not to the actual object. Instead the reference is to a copy of the object, in order to guarantee that the original is not modified. As the copy is performed within the constructor the infinite loop occurs.

You should pass a reference to a handle instead, i.e. InitScriptInstance(@this);

This will pass a reference to a handle to the object. Observe that your c++ function will need to dereference the reference in order to get the actual object.

Regards,
Andreas
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

#3 AnantaDas   Members   -  Reputation: 109

Like
0Likes
Like

Posted 20 January 2013 - 09:41 AM

The problem is that with &in the reference is not to the actual object. Instead the reference is to a copy of the object, in order to guarantee that the original is not modified. As the copy is performed within the constructor the infinite loop occurs.

You should pass a reference to a handle instead, i.e. InitScriptInstance(@this);

This will pass a reference to a handle to the object. Observe that your c++ function will need to dereference the reference in order to get the actual object.

Regards,
Andreas

 

Thanks for the reply, however, I don't think I understand it properly. Let me summarize:

 

1. In AngelScript I will have InitScriptInstance( @this ); as you wrote

 

2. In C++ I will register the function using declaration "void InitScriptInstance( ?&in )", is this still correct?

 

3. The function itself will look like this

void InitScriptInstance( void* pointer, int type )
{
   asIScriptObject* obj = (asIScriptObject*)pointer;
}

 

This does not work, so I guess one of the three steps caused misunderstanding on my side. Would you please care to elaborate?

Thanks

 

P.S.: If I rewrite the 'pointer' getter in the function above like this:

asIScriptObject** obj = (asIScriptObject**)pointer;

the dereference *obj is then NULL.



#4 Andreas Jonsson   Moderators   -  Reputation: 3455

Like
0Likes
Like

Posted 21 January 2013 - 03:16 PM

1. Correct.

 

2. Correct.

 

3. The function should look something like this:

 

void InitScriptInstance( void *pointer, int typeId )
{
  asIScriptObject *obj = 0;
  if( (typeId & asTYPEID_OBJHANDLE) && (typeId & asTYPEID_SCRIPTOBJECT) )
    obj = *reinterpret_cast<asIScriptObject**>(pointer);
  else
  {
    // The script didn't give us a handle to a script class. Throw an exception
    asIScriptContext *ctx = asGetActiveContext();
    ctx->SetException("InitScriptInstance require a handle to a script class");
    return;
  }
 
   // Do something with the object received (remember to increase the reference if the handle is to be stored)
   ...
}

 

The obj pointer shouldn't be null with this. If it is then something is wrong, perhaps even a bug in AngelScript.


AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

#5 AnantaDas   Members   -  Reputation: 109

Like
0Likes
Like

Posted 22 January 2013 - 04:40 AM

Thanks for help, I revised my code and everything is working fine now :)






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS