Sign in to follow this  
Quittouff

References and copy operator

Recommended Posts

Hi,

I'm little confused as it seems that using only references still requires a copy operator. Here is the configuration:
I declare a MyObject type with flags asOBJ_POD |asOBJ_VALUE | asOBJ_APP_CLASS_CD
then 2 functions with the signatures "const MyObject& GetMyObject()const" and "void SetMyObject(const MyObject&in)" and when i do:

SetMyObject(GetMyObject()); in the script I get the "ERROR: There is no copy operator for this type available."

why would I need a copy operator exposed to angelscript when all I use is some const refs?
Is there anything I can do to work around this without adding some copy operator?

Share this post


Link to post
Share on other sites
AngelScript uses safe references by default, which guarantees the lifetime of the objects referenced. This means that sometimes with in references it makes a copy of the referenced object so it won't get destroyed during the scope of the function. I don't know for sure, but changing from regular references to unsafe references may eliminate the need for the copy operator for the type. You'll need to enable the engine property in addition to changing syntax.

Share this post


Link to post
Share on other sites
Ok so it seems it's the same issue that I found some months ago. I'm not sure about using unsafe reference as I don't really understand what it implies. I think I'll use some work around for now.

Share this post


Link to post
Share on other sites
As SiCrane mentioned. As AngelScript has to guarantee that a reference is always valid, regardless how badly a script might have been written, it is necessary to make copies of objects from time to time. By making this copy to a local and temporary variable when calling the SetMyObject function AngelScript will be sure that the reference sent to the function is indeed valid throughout the entire execution of the function.

Using the asEP_ALLOW_UNSAFE_REFERENCES engine property, relaxes this a bit, in that AngelScript will no longer have to guarantee the life time of the references. This means that a badly written script can cause crashes or possible security flaws in your application, but if you trust your scripts then this shouldn't be a problem for you.

However, as you registered the type with asOBJ_POD AngelScript should have been able to make this copy without you registering an explicit copy operator. I'll need to check on this.

Share this post


Link to post
Share on other sites
Sorry I answer so late but I have a sh*load of work and I come back to AS by cycles.

So, no I didn't try Unsafe references because I already have a hard time using AS correctly with secured references and I can't afford adding another hazard in my code for now. I still have troubles with references and refcounting. It's always the same class that causes me headaches, it's one of the nerves of my communication between my engine and scripts and every time I think I resolved my problems (thanks to you guys) couple days/weeks later, something else come bite me in the @...

When I'll have some more stability, I'll try unsafe Ref and keep you posted though
Thanks.

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