Page 1 of 1

Problem with new Functions of EntHumanPlayerT

Posted: 2004-10-20, 13:23
by TheMatrix
I want to be able to heal a player, so i write a member Function of EntHumanPlayerT TakeHeal() that gives the Player +20 Health

So in Think() I write a Code that starts a trace, get an EntityID if the crosshair is on a player i would heal.

So now the problem
With GetBaseEntityByID i get just a BaseEntityT, but this doesnt have the member function TakeHeal. So i need a EntHumanPlayerT.

Could I convert the BaseEntityT to EntHumanPlayerT or could I get a EntHumanPlayerT by a EntityID??

Posted: 2004-10-20, 13:40
by Shadow
i belive the second one but im not positive and havent played with much yet so try it and find out!!! you cant hurt anything... too bad... just save first. :twisted:

Posted: 2004-10-20, 14:35
by Camille
if "ptr" is the pointer to the BaseEntityT

so "(EntHumanPlayerT *) ptr" is a pointer to the same entity but seen as a EntHumanPlayerT.

but don't forget to test if the entity is really a player, not an other entity (invisible or a weapon...)

Posted: 2004-10-20, 14:57
by TheMatrix
So I write
BaseEntityT* HitEntity = EngineFunctions->GetBaseEntityByID(EngineFunctions->Ca3DEWorldHandle, HitEntityID);
((EntCompanyBotT*)HitEntity)->TakeHeal();

Edit: I include "CompanyBot.hpp" and it works fine :)

Edit:
Hmm, with EntHumanPlayerT instead of EntCompanyBotT it works fine
BaseEntityT* HitEntity = EngineFunctions->GetBaseEntityByID(EngineFunctions->Ca3DEWorldHandle, HitEntityID);
((EntCompanyBotT*)HitEntity)->TakeHeal();
Thank you very much :D

Posted: 2004-10-20, 19:07
by Carsten
Nice to see that you and the others solved the problem even before I noticed the first post. 8) :D

Camille is right: If you are sure that the BaseEntityT* actually points to a EntHumanPlayerT*, casting the pointer as you did is no problem.

Another approach would be to keep the code that you have in "TakeHeal()" just where you want to apply it, and work with the BaseEntityT* directly, like this:

Code: Select all

BaseEntityT* HitEntity = EngineFunctions->GetBaseEntityByID(EngineFunctions->Ca3DEWorldHandle, HitEntityID);

if (HitEntity->TypeID==TYPEID_HUMANPLAYER)
{
    // "Give" heal.   (This would be the code from TakeHeal().)
    HitEntity->State.Health+=20;
    if (HitEntity->State.Health>100) HitEntity->State.Health=100;
    // etc.
}
This variant is not very OO, but also works without the potentially unsafe pointer cast.