Sign in to follow this  
Calefaction

Polymorphism question

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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*.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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.

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