void OnDamage(Entity @attacker, int amount)
{
Npc @npc = null;
if(attacker.GetType() == ET_NPC)
npc = entity;
else
return;
npc.DoDamage(amount / 2);
}
🎉 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
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
Is this currently possible in AngelScript?
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.
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:
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.
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?
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;}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement