Sign in to follow this  
DevilWithin

Question about value types

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this