Raised This Month: $51 Target: $400
 12% 

Getting the real 'this' in a hooked function


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
TommyV
Member
Join Date: Nov 2005
Old 04-11-2006 , 16:31   Getting the real 'this' in a hooked function
Reply With Quote #1

Is it possible to get to the 'this' of the class instance that owns the original function that is being hooked? Currently if I use 'this' in a function I am hooking to it interprets it, quite logically, to mean the instance of my plugin.

I am hooking CSSPlayer::Weapon_CanUse and I need to get to the CSSPlayer instance. Any tips on how could go about it?
TommyV is offline
L. Duke
Veteran Member
Join Date: Apr 2005
Location: Walla Walla
Old 04-11-2006 , 18:39  
Reply With Quote #2

There is a macro for that in sourcehook.h. Here's how I use it:
Code:
CBaseEntity *pBase = META_IFACEPTR(CBaseEntity);
L. Duke is offline
showdax
Senior Member
Join Date: Dec 2004
Old 04-11-2006 , 20:02  
Reply With Quote #3

CBaseEntity > CBaseAnimating > CBaseAnimatingOverlay > CBaseFlex > CBaseCombatCharacter > CBasePlayer > CCSPlayer

That's the heirarchy for CCSPlayer, in terms of inheritance. There's no multiple inheritance involved, so the "this" pointer should be the same from top to bottom, as long as your instance really is a CCSPlayer instance.

Anyway, what this means is you could use edict_t::GetUnknown() and IServerUnknown::GetBaseEntity() to get an instance.
showdax is offline
Send a message via MSN to showdax
L. Duke
Veteran Member
Join Date: Apr 2005
Location: Walla Walla
Old 04-11-2006 , 21:40  
Reply With Quote #4

I use
Code:
pBase->edict();
and so far it seems to work.
L. Duke is offline
c0ldfyr3
AlliedModders Donor
Join Date: Aug 2005
Location: Ireland
Old 04-11-2006 , 23:31  
Reply With Quote #5

Quote:
Originally Posted by showdax
CBaseEntity > CBaseAnimating > CBaseAnimatingOverlay > CBaseFlex > CBaseCombatCharacter > CBasePlayer > CCSPlayer

That's the heirarchy for CCSPlayer, in terms of inheritance. There's no multiple inheritance involved, so the "this" pointer should be the same from top to bottom, as long as your instance really is a CCSPlayer instance.

Anyway, what this means is you could use edict_t::GetUnknown() and IServerUnknown::GetBaseEntity() to get an instance.
He means inside of a hooked function which hooks multiple instances of the same class.
What L. Duke said is correct in what he needed to know =)
__________________
c0ldfyr3 is offline
Send a message via MSN to c0ldfyr3 Send a message via Yahoo to c0ldfyr3
showdax
Senior Member
Join Date: Dec 2004
Old 04-12-2006 , 00:58  
Reply With Quote #6

Quote:
Originally Posted by c0ldfyr3
He means inside of a hooked function which hooks multiple instances of the same class.
What L. Duke said is correct in what he needed to know =)
I assumed he was asking two questions: how to get a this pointer inside a hook and how to get an instance to hook in the first place.
showdax is offline
Send a message via MSN to showdax
TommyV
Member
Join Date: Nov 2005
Old 04-12-2006 , 01:44  
Reply With Quote #7

Perfect, thanks. That macro is exactly what I was looking for.
TommyV is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 18:15.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode