🎉 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!

C++ class casting

Started by
1 comment, last by Maddi 18 years ago
Hi everyone, I just started using angelscript and got quiet used to it. Now I have a question regarding class casting. Consider this:

// C++
class CInstance
{
 setTemplate(CTemplate* pTemplate);
};

class CTemplate
{
 void render();
}

class CSphere : public CTemplate
{
 void render();
}
I have registered all of these classes in the AngelScriptEngine, except the relation CSphere:CTemplate. Now if I try to do, in angelscript code, something like this:

instance inst;
sphere sp;
inst.setTemplate(sp);
I get the error that it cannot be casted ( in the setTemplate call ). Is there some sort of cast operator in Angelscript ? Thx in advance, Marcus
Advertisement
Not yet, but I'm getting closer to implement this.

For now you can register a function that does an explicit cast:

CTemplate *cast(CSphere *o){  return (CTemplate*)o;}engine->RegisterGlobalFunction("template &cast(sphere &)", asFUNCTION(cast), asCALL_CDECL);


Or you can register an overloaded setTemplate() method that accept a sphere object.

Regards,
Andreas

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

I've had many problems with declaring the c++ function with * and the scriptcall with &, so I recently switched to object handels, they seem to be far more stable / easy to use.

I did now use a little hack to circumvent the casting, which is not as nice as your solution, by simply registering the same function with different scriptcall declarations, e.g. i registered "void setTemplate(sphere@)" and "void setTemplate(cube@)". in the c++ setTemplate I then dynamic_cast the Pointer to CTemplate. This works quiet well too, but for sure will be more work than declaring a cast for the classes once.

Thx for your replay,
Maddi

This topic is closed to new replies.

Advertisement