Jump to content
  • Advertisement
Sign in to follow this  
Quittouff

References and copy operator

This topic is 2526 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

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!