Sign in to follow this  
behc

Reference is read-only, error

Recommended Posts

hi i have got some confusing error messages about read-only references when calling behaviors (or regular functions)
//as
(...)
CCamera@ pCam = wp.GetCamera();
vec3 end = pCam.Pos + pCam.Dir*1000;


//cpp (some important functions)
r = engine->RegisterGlobalBehaviour(asBEHAVE_ADD,"vec3 f(const vec3 &in, const vec3 &in)",asFUNCTION(vec3add),asCALL_CDECL); assert(r >= 0);
r = engine->RegisterGlobalBehaviour(asBEHAVE_SUBTRACT,"vec3 f(const vec3 &in, const vec3 &in)",asFUNCTION(vec3sub),asCALL_CDECL); assert(r >= 0);

r = engine->RegisterGlobalBehaviour(asBEHAVE_MULTIPLY,"vec3 f(const float, const vec3 &in)",asFUNCTION(vec3mul1),asCALL_CDECL); assert(r >= 0);
r = engine->RegisterGlobalBehaviour(asBEHAVE_MULTIPLY,"vec3 f(const vec3 &in, const float)",asFUNCTION(vec3mul2),asCALL_CDECL); assert(r >= 0);

//and camera class props
r = pSE->RegisterObjectProperty(pClassName,"const vec3 Pos",offsetof(CGRCamera,Pos)); assert(r>=0);
r = pSE->RegisterObjectProperty(pClassName,"const vec3 Dir",offsetof(CGRCamera,Dir)); assert(r>=0);
when i remove 'const' from camera's RegisterObjectProperty everything works fine and when i compile script below, everything is ok too (which is the most confusing)
//as
const vec3 v1(0,2,0);
const vec3 v2(1,1,0);
vec3 ple = v1 + v2*1000;
are object properties 'const' in different way than local variables? (or can i register behaviors to be more const? :) ) (i've tested that on wip and last stable version (2.7)) hans

Share this post


Link to post
Share on other sites
Object properties cannot be registered as const. AngelScript don't know how to do that (at least not yet).

You can overload methods and behaviours to handle const objects. Just add const after the method declaration, i.e. "void f() const".

What message is confusing to you? I may need to change it.

Regards,
Andreas

Share this post


Link to post
Share on other sites
hm.. so if i can't register const object properties, the function below should fail? (the 'Pos' property is not const in cpp, but i want it to be read-only in as, (and vec3 is simple 3d vector class))

r = pSE->RegisterObjectProperty(pClassName,"const vec3 Pos",offsetof(CGRCamera,Pos)); assert(r>=0);

anyway that call seems to work, i can (inside script) copy 'Pos' to another variable and i can't change it directly (as gives me: "Reference is read-only" error, like it should be), but i can't pass it as 'const vec3 &in' to functions/behaviors because of the same error. (i would understand that error when trying to pass 'Pos' as 'vec3 &in')

if this is some unimplemented feature then it's ok, there are some easy workarounds i can use,
i just thought it might be a bug :)

hans

Share this post


Link to post
Share on other sites
Now that you show me the error messages I do remember implementing support for registering properties as const.

I'll have to investigate why it gives an error when passing the property to a const &in parameter though. It could very well be a bug in AS.

Thanks,
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