Raised This Month: $ Target: $400
 0% 

[TF2] Changing ammo in clip


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
thacursedpie
Member
Join Date: May 2008
Old 06-20-2009 , 05:01   [TF2] Changing ammo in clip
Reply With Quote #1

Hello again .

I'm trying to change the ammo the player currently has in his clip. (I want to do it with a plugin, not with the .ctx's, since they require a server-restart).

So far I've been able to change the ammo in the ammo-reserve:

Code:
//Top of code:
new Handle:ammoOffset;
new Handle:clipOffset;

//OnPluginStart:
ammoOffset = FindSendPropInfo("CTFPlayer", "m_iAmmo");
clipOffset = FindSendPropInfo("CTFWeaponBase", "m_iClip1");

//Setting the reserve ammo to 0: (working)
for (new n = 1; n < 4; n++)
{
     if (IsValidEdict(n)) {
            SetEntData(i, ammoOffset + (n * 4), 0);
     }
}

//Setting the clip ammo to 1: (not working)
for (new n = 0; n < 3; n++)
{
    if (IsValidEdict(n)) {
        new iWeapon = GetEntDataEnt2(i, FindSendPropInfo("CTFPlayer", "m_hActiveWeapon"));
        SetEntData(iWeapon, clipOffset, 1);
    }
}
Any suggestions? Thanks in advance!

Last edited by thacursedpie; 06-20-2009 at 05:05.
thacursedpie is offline
paegus
Senior Member
Join Date: Nov 2004
Location: Extreme low earth orbit
Old 06-20-2009 , 08:55   Re: [TF2] Changing ammo in clip
Reply With Quote #2

Code:
SetEntProp(eWeapon, Prop_Send, "m_iClip1", iValue)

at least i think it's m_iClip1... am at work right now so can't check
__________________
Live and learn or die and teach by example.
Plugins Mine | Hidden:SourceMod
paegus is offline
p3tsin
Senior Member
Join Date: Sep 2005
Location: Finland
Old 06-20-2009 , 13:16   Re: [TF2] Changing ammo in clip
Reply With Quote #3

Quote:
Originally Posted by thacursedpie View Post
Hello again .

I'm trying to change the ammo the player currently has in his clip. (I want to do it with a plugin, not with the .ctx's, since they require a server-restart).

So far I've been able to change the ammo in the ammo-reserve:

Code:
//Top of code:
new Handle:ammoOffset;
new Handle:clipOffset;

//OnPluginStart:
ammoOffset = FindSendPropInfo("CTFPlayer", "m_iAmmo");
clipOffset = FindSendPropInfo("CTFWeaponBase", "m_iClip1");

//Setting the reserve ammo to 0: (working)
for (new n = 1; n < 4; n++)
{
     if (IsValidEdict(n)) {
            SetEntData(i, ammoOffset + (n * 4), 0);
     }
}

//Setting the clip ammo to 1: (not working)
for (new n = 0; n < 3; n++)
{
    if (IsValidEdict(n)) {
        new iWeapon = GetEntDataEnt2(i, FindSendPropInfo("CTFPlayer", "m_hActiveWeapon"));
        SetEntData(iWeapon, clipOffset, 1);
    }
}
Any suggestions? Thanks in advance!
It looks ok for most part, except you gotta get rid of both of the IsValidEdict checks, "n" is not supposed to be an edict in this case. Also, not effecting the functionality, but offsets are plain integers, not handles... the compiler probably gave you a bunch of tag mismatch warnings because of that.

EDIT: I would recommend checking if iWeapon is a valid entity before using it.
__________________
plop

Last edited by p3tsin; 06-20-2009 at 13:18.
p3tsin is offline
thacursedpie
Member
Join Date: May 2008
Old 06-20-2009 , 14:16   Re: [TF2] Changing ammo in clip
Reply With Quote #4

@Petsin.
Well, my debug-screen is literally filled with tag mismatches. (even before I added this into my plugin). So I didn't notice the new ones .

I'm still new to SourcePawn (and programming in general)

@Petsin & paegus
Will try out your suggestions now.

Thanks!

EDIT:
Paegus' suggestion isn't working. Testing Petsin's now.
Ok, found the problem!

You already saw the piece of code that didn't work. But this one does:
Code:
//Top of code:
new Handle:ammoOffset;
new Handle:clipOffset;

//OnPluginStart:
ammoOffset = FindSendPropInfo("CTFPlayer", "m_iAmmo");
clipOffset = FindSendPropInfo("CTFWeaponBase", "m_iClip1");

//Setting the reserve ammo to 0: (working)
for (new n = 1; n < 4; n++)
{
     if (IsValidEdict(n)) {
            SetEntData(i, ammoOffset + (n * 4), 0);
            PrintToConsole(i, "[HNS]- Setting secondary ammo to 0!")
            PrintToServer("[HNS]- Setting secondary ammo to 0!");
                        
        new iWeapon = GetEntDataEnt2(i, FindSendPropInfo("CTFPlayer", "m_hActiveWeapon"));
        //IsValidEntity(iWeapon);
         while (!IsValidEntity(iWeapon)) {
          iWeapon = GetEntDataEnt2(i, FindSendPropInfo("CTFPlayer", "m_hActiveWeapon"));
          }
          PrintToConsole(i, "[HNS]- Setting primary ammo to 0!");
          PrintToServer("[HNS]- Setting primary ammo to 0!");
          SetEntProp(iWeapon, Prop_Send, "m_iClip1", 1);
     }
}
I started with adding the IsValidEntity(iWeapon) loop. It surprised me that it didn't crash the server. Then I added in the Prints and well, my guess is this was the issue:

Code:
//Snip

for (new n = 0; n < 3; n++)
{
 //blah blah
}

Last edited by thacursedpie; 06-20-2009 at 15:49.
thacursedpie is offline
p3tsin
Senior Member
Join Date: Sep 2005
Location: Finland
Old 06-20-2009 , 16:16   Re: [TF2] Changing ammo in clip
Reply With Quote #5

Quote:
Originally Posted by thacursedpie View Post
Code:
//Top of code:
new Handle:ammoOffset;
new Handle:clipOffset;

//OnPluginStart:
ammoOffset = FindSendPropInfo("CTFPlayer", "m_iAmmo");
clipOffset = FindSendPropInfo("CTFWeaponBase", "m_iClip1");

//Setting the reserve ammo to 0: (working)
for (new n = 1; n < 4; n++)
{
     if (IsValidEdict(n)) {
            SetEntData(i, ammoOffset + (n * 4), 0);
            PrintToConsole(i, "[HNS]- Setting secondary ammo to 0!")
            PrintToServer("[HNS]- Setting secondary ammo to 0!");
                        
        new iWeapon = GetEntDataEnt2(i, FindSendPropInfo("CTFPlayer", "m_hActiveWeapon"));
        //IsValidEntity(iWeapon);
         while (!IsValidEntity(iWeapon)) {
          iWeapon = GetEntDataEnt2(i, FindSendPropInfo("CTFPlayer", "m_hActiveWeapon"));
          }
          PrintToConsole(i, "[HNS]- Setting primary ammo to 0!");
          PrintToServer("[HNS]- Setting primary ammo to 0!");
          SetEntProp(iWeapon, Prop_Send, "m_iClip1", 1);
     }
}
1) "n" is still not supposed to be an edict, you WILL run into weird behavior if you dont remove the check.
2) that while loop with IsValidEntity in it wont do you any good. GetEntDataEnt2 will keep returning the exact same invalid entity index resulting your server to freeze, maybe crash if youre lucky.

PHP Code:
iWeapon GetEntDataEnt2(iactiveWeaponOffset);
if(
IsValidEntity(iWeapon)) {
    
PrintToConsole(i"[HNS]- Setting primary ammo to 0!");
    
PrintToServer("[HNS]- Setting primary ammo to 0!");
    
SetEntProp(iWeaponProp_Send"m_iClip1"1);

__________________
plop
p3tsin is offline
thacursedpie
Member
Join Date: May 2008
Old 06-21-2009 , 09:57   Re: [TF2] Changing ammo in clip
Reply With Quote #6

I will fixe those things the next time I'll be working on my plugin . Thanks for your help.
thacursedpie 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 12:40.


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