🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Math Conversion Problems

Started by
1 comment, last by wilberolive 10 years, 4 months ago

I'm trying to use my own Vector class and am running into issues.

I have two functions in my Vector class that allow it to multiple with a float in any order like this.


friend Vector operator+ (const Vector& t, float f) {return Vector(t.x+f,t.y+f,t.z+f);}
friend Vector operator+ (float f, const Vector& t) {return Vector(t.x+f,t.y+f,t.z+f);}

I don't know how to register this with AngelScript so that I can multiple a Vector with a float in any order. I've tried registering it like this, but it gives an error that it failed to register the second function. I'm guessing because I've declared opMul already with the same declaration?


engine->RegisterObjectMethod("Vector", "Vector opMul(float f)", asFUNCTIONPR(operator*, (const Vector&, float), Vector), asCALL_CDECL_OBJFIRST);
engine->RegisterObjectMethod("Vector", "Vector opMul(float f)", asFUNCTIONPR(operator*, (float, const Vector&), Vector), asCALL_CDECL_OBJLAST);

If I comment out either line above, so I'm only registering one of the functions (doesn't matter which one), then it works in script in the order of Vector * float. But it fails if I try to do float * Vector. I get an error that says "No conversion from 'Vector' to math type available."

How can I set up my Vector class so I can multiply if by a float in any order, i.e. Vector * float or float * Vector?

Advertisement

You can register a reversed version of most operators. In your case, opMul_r. See: http://www.angelcode.com/angelscript/sdk/docs/manual/doc_script_class_ops.html

Oh wow, thank you! I even read that page before posting, but just didn't pick up on that point. This has solved the problem.

This topic is closed to new replies.

Advertisement