• Advertisement
Sign in to follow this  

Question about value types

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a doubt about angelscript that I need to clarify.

 

For lightweight classes which are efficiently passed by value, is there a way to still store handles to the object?

 

Explaining with an example: I have a Sprite class which is quite simple, having only a few members. I registered this as a Ref type because i wanted to pass it around through functions, modifying the original instance.

 

However, such a lightweight class could fit well as a value type , I believe... But if I do that, I am not sure I can still pass a sprite to a function(in-script) and modify the original Sprite object rather than its copy. Or can I?

 

Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement

I mean inside the scripts.. Not from / to C++.

 

Sprite s;

 

changeThisSprite(@s); // or something similar

Share this post


Link to post
Share on other sites

You can only use object handles with reference types, but inout references will work with value types if you allow the script engine to use unsafe references.

engine->SetEngineProperty(asEP_ALLOW_UNSAFE_REFERENCES, true);

 

You can then use them inside scripts as well as passing tom C++.

void modify(Sprite & inout s)
{
    // modify the sprite
}

 

Be aware that you are now responsible for ensuring that parameters passed as inout remain valid for as long as they're in use. The script engine can't guarantee their lifetime, so it's possible to crash your application by passing temporary values into C++.

Edited by Jake Albano

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement