Sign in to follow this  
Scarabus2

Bug with property accessors?

Recommended Posts

I think I've encountered a bug. I'm not really sure how to explain it because I don't fully understand it myself. I noticed that one of my scripts stopped behaving like it should. It's a simple script (executed every frame on a persistent object) for simulating a ball and the object no longer rolls properly. I tracked it down to a line of angelscript code that deals with its rotation property.
engine->RegisterObjectMethod("Object", "void set_rotation(float)", asMETHOD(ASObject, SetRotation), asCALL_THISCALL);
engine->RegisterObjectMethod("Object", "float get_rotation() const", asMETHOD(ASObject, GetRotation), asCALL_THISCALL);
Consider these Angelscript code snippets:
float elapsed = 1.0f; // just some float
obj.rotation = obj.rotation + elapsed * 1.0f;
float elapsed = 1.0f;
float temp = obj.rotation + elapsed * 1.0f;
obj.rotation = obj.rotation + elapsed * 1.0f;
The first snippet executes as you would expect. obj.rotation evaluates to 1, the next iteration it evalutates to 2, 3, etc. i.e. the ball is rolling. In the second snippet obj.rotation always evaluates to 2 for each and every iteration. When I debug it my GetRotation method returns 2.0f and the value that is passed to SetRotation from Angelscript is still 2.0f. 2 + 1 * 1 should be 3 right? The weird part is that if I change this code just slightly it works as expected again:
float elapsed = 1.0f;
float temp = obj.rotation + elapsed;
obj.rotation = obj.rotation + elapsed * 1.0f;
The code above was written to reproduce the bug. It's much harder for me to get rid of it in my actual script, which looks something like this:
// ... Ball simulation above ...
self.rotation = self.rotation + elapsed * FuncReturningAFloat(...);
This must seem pretty nonsensical but there's most likely some bug in there somewhere.

Share this post


Link to post
Share on other sites
It was indeed a bug with the property accessors.

The compiler was improperly re-using a temporary variable when evaluating the property get accessor. This caused the right hand expression in the + operator to overwrite the result gotten from the get accessor.

This didn't always happen because it depended on in which order the temporary variables had been released in previous expressions.


Thanks for pasting the code that let me reproduce the problem so easily.



You can get the fix from the SVN (rev 502). I've already begun work on 2.18.0, so if you do not want the new feature already implemented, though it shouldn't affect you, you should be able to just pick up the changes made in as_compiler.cpp for this fix.

Regards,
Andreas

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