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

Polymorphism question

Started by
5 comments, last by Calefaction 18 years, 8 months ago
I am curious to know if AS currently supports polymorphism for host application registered types. For example, if I have a base type Entity, which represents a basic interactive entity in my world, and I subclass from that the type Npc. I can register Entity and Npc as seperate types to AS, and that works great, but what if I want to be able to simply pass a generic Entity in to a function, see if it's an Npc and "cast" it to the type Npc? For instance

void OnDamage(Entity @attacker, int amount)
{
    Npc @npc = null;
    if(attacker.GetType() == ET_NPC)
        npc = entity;
    else
        return;

    npc.DoDamage(amount / 2);
}

Is this currently possible in AngelScript?
Matt Holmes[ aka Calefaction ]Wildfire Games - General Programmerhttp://www.wildfiregames.com
Advertisement
Not directly. Polymorphism in the sense that calling virtual functions calls the function on the proper subtype works, if you write wrappers for the function. What you want to do doesn't work in C++ either; you have to cast to go from base to derived type. You can bind a conversion function that wraps a dynamic_cast on your basetype*.
Quote: Original post by Deyja
Not directly. Polymorphism in the sense that calling virtual functions calls the function on the proper subtype works, if you write wrappers for the function. What you want to do doesn't work in C++ either; you have to cast to go from base to derived type. You can bind a conversion function that wraps a dynamic_cast on your basetype*.


Right, in C++ I would normally use static_cast<> for this though, as it's a simple walk down the type tree.

How exactly do I bind a conversion function? Is that an ObjectBehavior? I didn't see one like that listed.
Matt Holmes[ aka Calefaction ]Wildfire Games - General Programmerhttp://www.wildfiregames.com
Actually, you should be using dynamic_cast for downcasting. Dynamic cast returns null if the cast is invalid.

And, er, no. Just a normal function, not a behavior or anything. It would look like this in script:

void OnDamage(Entity @attacker, int amount){    Npc @npc = null;    if(ConvertToNpc(attacker,npc))       npc.DoDamage(amount / 2);}


In this example, you'd have bound the function 'bool ConvertToNpc(Entity@, Npc@)'. This function would attempt the conversion and return false if it was impossible.
Quote: Original post by Deyja
Actually, you should be using dynamic_cast for downcasting. Dynamic cast returns null if the cast is invalid.

And, er, no. Just a normal function, not a behavior or anything. It would look like this in script:

void OnDamage(Entity @attacker, int amount){    Npc @npc = null;    if(ConvertToNpc(attacker,npc))       npc.DoDamage(amount / 2);}


In this example, you'd have bound the function 'bool ConvertToNpc(Entity@, Npc@)'. This function would attempt the conversion and return false if it was impossible.


Okay, if I do:

engine->RegisterGlobalFunction("bool ConverToNpc(Entity@, Npc@)", asFUNCTION(Entity::ConverToNpc), asCALL_CDECL);


What would the C++ native function signature look like?
Matt Holmes[ aka Calefaction ]Wildfire Games - General Programmerhttp://www.wildfiregames.com
I'm not exactly sure how handles are passed to the application. I'm still using 1.10.1d (!!!!), so I'd just use raw pointers. I think the C++ code just gets them as pointers. You'd need to make the NPC handle a handle-to-a-handle; eg pointer-to-pointer.

bool ConvertToNpc(Entity* ent, Npc** n){   *n = dynamic_cast<Npc*>(ent);   return *n != 0;}
Npc **n doesn't work, it always comes back NULL. I may have to return the handle instead.

Edit:

Yah, redefining the script function to "Npc @ConverToNpc(Entity @entity)" and check it's return against "null" fixed the issue.
Matt Holmes[ aka Calefaction ]Wildfire Games - General Programmerhttp://www.wildfiregames.com

This topic is closed to new replies.

Advertisement