Advertisement

Question about value types

Started by March 31, 2013 09:16 PM
3 comments, last by DevilWithin 11 years, 8 months ago

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.

You can pass a reference to the object. Ex: void foo(MyObject & inout).
Advertisement

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

Sprite s;

changeThisSprite(@s); // or something similar

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

Thanks for the answer Jake! It clarifies things!

This topic is closed to new replies.

Advertisement