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

Correct way to handle Vectors


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
xerox8521
Senior Member
Join Date: Sep 2011
Old 03-11-2016 , 18:43   Correct way to handle Vectors
Reply With Quote #1

Hi,

so been trying to get into SM extensions so far everything i made worked. Now i wanted to create a simple GetWeaponShootPosition native which takes a client and returns a float array. I got that setup but it seems im doing something wrong because it crashes right away. Looking through the source sdk 2013 code the function Weapon_ShootPosition seem to return a vector. So im not sure how i would handle the vector correctly as pointer or normal variable because the function itself doesn't return a vector pointer.

PHP Code:
cell_t Native_GetWeaponShootPosition(IPluginContext *pContext, const cell_t *params)
{
    static 
ICallWrapper *pWrapper NULL;
    if(!
pWrapper)
    {
        
REGISTER_NATIVE_ADDR("CBasePlayer::Weapon_ShootPosition",
            
PassInfo pass[1]; \
            
PassInfo ret;\
            
pass[0].flags PASSFLAG_BYVAL; \
            
pass[0].size sizeof(CBaseEntity *); \
            
pass[0].type PassType_Basic; \
            
            
ret.flags PASSFLAG_BYVAL;\
            
ret.size sizeof(Vector);\
            
ret.type PassType_Basic;\
            
pWrapper g_pBinTools->CreateCall(addrCallConv_ThisCall, &retpass1));
    }
    
CBaseEntity *pEntity;
    if(!(
pEntity GetCBaseEntity(params[1], true)))
    {
        return 
pContext->ThrowNativeError("Client index %d is invalid",params[1]);
    }
    
    
cell_t *pPos;
    
pContext->LocalToPhysAddr(params[2], &pPos);
    
    
unsigned char vstk[sizeof(CBaseEntity *)];
    
unsigned char *vptr vstk;
    
    *(
CBaseEntity **)vptr pEntity;
    
vptr +=sizeof(CBaseEntity *);
    
Vector pos;
    
pWrapper->Execute(vstk, &pos);
    
    
pPos[0] = sp_ftoc(pos.x);
    
pPos[1] = sp_ftoc(pos.y);
    
pPos[2] = sp_ftoc(pos.z);
    return 
1;

xerox8521 is offline
psychonic

BAFFLED
Join Date: May 2008
Old 03-12-2016 , 09:03   Re: Correct way to handle Vectors
Reply With Quote #2

While this doesn't answer your question, you can just call GetClientEyePosition in SM to get the same Vector, rather than needing to create an extension for it. Most or all games use the eye position at the shoot position unless the client is in a vehicle.

(Eyeballing it, I don't see the issue with your code however)

Last edited by psychonic; 03-12-2016 at 09:07.
psychonic is offline
donrevan
AlliedModders Donor
Join Date: Jul 2010
Old 03-13-2016 , 07:32   Re: Correct way to handle Vectors
Reply With Quote #3

You can try calling it via a typedef to see if it is working.
Code:
typedef Vector (__thiscall *tGetShootPos)(CBasePlayer*);
tGetShootPos shootPos = (tGetShootPos)addr;
Vector vec = shootPos(pPlayer);
Isn't Weapon_ShootPosition virtual? You don't need a signature scan to call it. Maybe you're accidently calling the wrong function(wrong signature?)

Last edited by donrevan; 03-13-2016 at 07:40.
donrevan is offline
xerox8521
Senior Member
Join Date: Sep 2011
Old 03-13-2016 , 16:13   Re: Correct way to handle Vectors
Reply With Quote #4

Ok so i have rebuild this with SM using this signature (@_ZN11CBasePlayer20Weapon_ShootPositionEv) game is Zombie Panic Source. Both versions work the virtual and Signature thing.

PHP Code:
    StartPrepSDKCall(SDKCall_Player);
    
PrepSDKCall_SetFromConf(conf,SDKConf_Virtual,"CBasePlayer::Weapon_ShootPosition");
    
//PrepSDKCall_SetFromConf(conf,SDKConf_Signature,"CBasePlayer::Weapon_ShootPosition");
    
PrepSDKCall_SetReturnInfo(SDKType_Vector,SDKPass_ByValue);
    
hWeaponGetShootPosition EndPrepSDKCall(); 
and this works. So i dont really understand why the extension version does not work. Btw the extension does some function detouring also im trying to get into sm extensions
xerox8521 is offline
donrevan
AlliedModders Donor
Join Date: Jul 2010
Old 03-14-2016 , 06:07   Re: Correct way to handle Vectors
Reply With Quote #5

Quote:
Originally Posted by xerox8521 View Post
So i dont really understand why the extension version does not work. Btw the extension does some function detouring also im trying to get into sm extensions
ptr +=sizeof(CBaseEntity *);
is unnecessary(as you dont have any other param) but doesn't cause the crash.

pWrapper = g_pBinTools->CreateCall(addr, CallConv_ThisCall, &ret, pass, 1));
You pass 1 as paramcount but Weapon_ShootPosition doesn't have any parameters(thisptr is implicit). See the documentation..
This means you don't need to pass pass either. Try this instead:
pWrapper = g_pBinTools->CreateCall(addr, CallConv_ThisCall, &ret, NULL, 0));

Last edited by donrevan; 03-14-2016 at 06:09.
donrevan is offline
xerox8521
Senior Member
Join Date: Sep 2011
Old 03-18-2016 , 12:40   Re: Correct way to handle Vectors
Reply With Quote #6

NVM. did it differently using sourcepawn now.


Hm yea tried that but doesn't seem to work either.

PHP Code:
cell_t Native_GetWeaponShootPosition(IPluginContext *pContext, const cell_t *params)
{
    static 
ICallWrapper *pWrapper NULL;
    if(!
pWrapper)
    {
        
REGISTER_NATIVE_ADDR("CBasePlayer::Weapon_ShootPosition",
        
            
PassInfo ret;\
            
            
ret.flags PASSFLAG_BYVAL;\
            
ret.size sizeof(Vector);\
            
ret.type PassType_Basic;\
            
pWrapper g_pBinTools->CreateCall(addrCallConv_ThisCall, &retNULL0));
    }
    
CBaseEntity *pEntity;
    if(!(
pEntity GetCBaseEntity(params[1], true)))
    {
        return 
pContext->ThrowNativeError("Client index %d is invalid",params[1]);
    }
    
    
cell_t *pPos;
    
pContext->LocalToPhysAddr(params[2], &pPos);
    
    
unsigned char vstk[sizeof(CBaseEntity *)];
    
unsigned char *vptr vstk;
    
    *(
CBaseEntity **)vptr pEntity;
    
//vptr +=sizeof(CBaseEntity *);
    
Vector pos;
    
pWrapper->Execute(vstk, &pos);
    
    
pPos[0] = sp_ftoc(pos.x);
    
pPos[1] = sp_ftoc(pos.y);
    
pPos[2] = sp_ftoc(pos.z);
    return 
1;


Last edited by xerox8521; 03-18-2016 at 12:46.
xerox8521 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 07:50.


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