Problem with new Functions of EntHumanPlayerT

Get technical support about the C++ source code and about Lua scripts for maps, entities, GUIs, the console, materials, etc. Also covered are the Cafu libraries and APIs, as well as compiling, linking, and the build system.
Post Reply
TheMatrix
Posts:20
Joined:2004-10-19, 10:06
Problem with new Functions of EntHumanPlayerT

Post by TheMatrix » 2004-10-20, 13:23

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??
User avatar
Shadow
Posts:195
Joined:2004-08-28, 06:00
Location:Minesota, USofA
Contact:

Post by Shadow » 2004-10-20, 13:40

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:
Image
PBX CONTINUES!!!
CLICK HERE!!
Camille
Posts:11
Joined:2004-08-25, 10:03

Post by Camille » 2004-10-20, 14:35

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...)
TheMatrix
Posts:20
Joined:2004-10-19, 10:06

Post by TheMatrix » 2004-10-20, 14:57

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
User avatar
Carsten
Site Admin
Posts:2170
Joined:2004-08-19, 13:46
Location:Germany
Contact:

Post by Carsten » 2004-10-20, 19:07

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.
Best regards,
Carsten
Post Reply

Who is online

Users browsing this forum: No registered users and 19 guests