PDA

View Full Version : Update Ammo HUD


Wilson [29th ID]
09-01-2006, 06:45 PM
This tutorial is for scripters who have made a plugin or too - I didn't go extremely indepth with it to provide for newbies yet.

You'll notice if you give a client ammo, their HUD either does not reflect it or it gets messed up and doesn't display all of it. There is a way to fix that, but first you must understand there is a difference between "clip ammo" and "backpack ammo."

Clip ammo - the ammo you're currently firing that's inside your gun - clip/magazine/whatever it's called for that gun.

Backpack ammo - the ammo that's "in your backpack" - the extra clips/magazines

Updating backpack ammo is actually easier than updating clip ammo. To do this, you use the event "AmmoX". In order to actually update through AmmoX, however, you need to know the channel of the weapon you're trying to update. These channels can be found through a message logger such as Damaged Soul's, but fortunately for you I've found them all and they're in this tutorial.

It helps to define the channels so you don't have to remember them. This is similar to a const file, but you simply add it to the top of your plugin.

The commented text to the right of the value is what weapons use that channel. This list includes all weapons in day of defeat, so enjoy.

#define AMMO_SMG 1 // thompson, greasegun, sten, mp40
#define AMMO_ALTRIFLE 2 // carbine, k43, mg34
#define AMMO_RIFLE 3 // garand, enfield, scoped enfield, k98, scoped k98
#define AMMO_PISTOL 4 // colt, webley, luger
#define AMMO_SPRING 5 // springfield
#define AMMO_HEAVY 6 // bar, bren, stg44, fg42, scoped fg42
#define AMMO_MG42 7 // mg42
#define AMMO_30CAL 8 // 30cal
#define AMMO_GREN 9 // grenades (should be all 3 types)
#define AMMO_ROCKET 13 // bazooka, piat, panzerschreck

Now to actually update the backpack ammo, you can create a function like below.

public set_ammo(channel, ammo)
message_begin(MSG_ONE,gmsgAmmoX,{0,0,0},id);
write_byte(channel);
write_byte(ammo);
message_end();
}

Then you can call the function like below. This will set the K98 to have 3 backpack clips (they each contain 5 rounds)

set_ammo(AMMO_RIFLE, 15)

Also keep in mind that AmmoX only updates the HUD - it does not actually adjust any pdata which contains the actual variable of how much ammunition the player has. That is what dod_set_user_ammo() does but I'll let you read the .inc files for that.
Basically, if you only do the above, when you reload, it will go to the number it was at before (minus 1 since you reloaded)

--------------------------------------------------------
Now setting the clip itself is a lot harder and it took a while to figure out. Just use this function.

This will set the k98 clip to have only 4 rounds.
set_clip(id, "weapon_kar", 4)

public set_clip(id,const weapon[],clip) {

new currentent = -1, gunid = 0

// get origin
new Float:origin[3];
entity_get_vector(id,EV_VEC_origin,origin);

while((currentent = find_ent_in_sphere(currentent,origin,Float:1.0)) != 0) {
new classname[32];
entity_get_string(currentent,EV_SZ_classname,class name,31);

if(equal(classname,weapon))
gunid = currentent

}

set_pdata_int(gunid,108,clip); // set their ammo

return PLUGIN_CONTINUE
}

Note that the clip setting does affect pdata and not just the HUD.

Like I said, this is for intermediate level scripters. Most new scripters won't find this as usefull as others who have a few plugins they couldn't release cause the hud gets screwed up :-P

diamond-optic
09-01-2006, 09:08 PM
good stuff...

wish you posted this months ago haha :-)

Hell Phoenix
09-03-2006, 12:52 PM
Wow thats awesome! Gotta update some plugins now ;D

Zor
09-03-2006, 02:49 PM
At the moment I'm kinda stuck with making my bleeding plugin into a module. I am trying to find the message for the screen in the game so that I can display the bandage icon and the patching icon. Anybody know? Its like this in the plugin:


// In the plugin_init
register_event("Object", "hook_object", "b")

// Show the blood icon in the function
message_begin(MSG_ONE, gmsgObject, {0,0,0}, victim)
write_string(fileNames[drop])
message_end()

Now the code I used has the following:

struct sUserMsg
{
const char *name;
int* id;
FunctionEventCall function;
}

g_user_msg[] =
{
{ "CurWeapon", &gmsgCurWeapon, Client_CurWeapon },
{ "Object", &gmsgObject, Client_Object },
{ 0,0,0 }
};

And I have all the code to go with this shit, so I know that it works with CurWeapon, it get called, but the object part dont...at least I dont think so...

Cheers!

diamond-optic
09-03-2006, 09:53 PM
zor.. what the hecks that gotta do with updating ammo on the hud... lmao

Wilson [29th ID]
09-03-2006, 10:35 PM
Nah Zor, you'd be better off asking that in amxmodx forums...Twilight would know.

Zor
09-04-2006, 01:20 PM
Its in the same place as the ammo...and its a module, so yeah...I was working on it and its not doing what its suppose to one way or another!

Cheers!

Spectral2
04-25-2007, 11:40 PM
so if i wanted to make it so people had unlimited ammo with their current weapon and they didn't have to ever reload i would just set the pdata of 108 to 999 and once the ammo gets to 100 just reset it to 999?

Wilson [29th ID]
04-26-2007, 04:50 PM
Yeah, actually that should work - I think I have done that before - gave my thompson like 100 rounds. Man, that was fun.

Tank
11-30-2008, 04:05 PM
Nevermind, guess I was an idiot. Had issues compiling a plugin containing one of above posted functions, just forgot to include engine and fakemeta. Thanks for the info Wilson, much appriciated

ghzero
03-02-2009, 08:27 PM
how to catch msgid for AmmoX ?

gmsgAmmoX = get_user_msgid("???????");

diamond-optic
03-02-2009, 08:47 PM
get_user_msgid("AmmoX")

ghzero
03-04-2009, 12:51 PM
question: is set_ammo(id, channel, ammo) by channel only for the shown info on the hud (right/bottom corner) or does it setup the useable ammo for the weapon too?

ghzero
03-04-2009, 03:08 PM
whats the reason for this error:
Plugin called message_begin with an invalid message id (0).
[AMXX] Run time error 10: native error (native "message_begin")
set_ammo (line 1644)

by using this code:
public set_ammo(id, channel, ammo)
{
message_begin(MSG_ONE,gmsgSetAmmoX,{0,0,0},id);// LINE 1644 !!!!
write_byte(channel);
write_byte(ammo);
message_end();
}


EDIT:
OMG - I have forgotten to catch the msgid by/in plugin_init():
gmsgSetAmmoX = get_user_msgid("AmmoX");

:-)
(scusi)


EDIT:
maybe this functions may help you..

public dod_ghget_ammochannel(wpnid)
{//to check if ammo type is compatible by their channels
switch( wpnid )
{
case DODW_THOMPSON, DODW_GREASEGUN, DODW_STEN, DODW_MP40 :
return AMMO_SMG;
case DODW_M1_CARBINE, DODW_K43, DODW_MG34 :
return AMMO_ALTRIFLE;
case DODW_GARAND, DODW_ENFIELD, DODW_SCOPED_ENFIELD, DODW_KAR, DODW_SCOPED_KAR :
return AMMO_RIFLE;
case DODW_COLT, DODW_LUGER, DODW_WEBLEY :
return AMMO_PISTOL;
case DODW_SPRINGFIELD :
return AMMO_SPRING;
case DODW_BAR, DODW_BREN, DODW_STG44, DODW_FG42, DODW_SCOPED_FG42 :
return AMMO_HEAVY;
case DODW_MG42 :
return AMMO_MG42;
case DODW_30_CAL :
return AMMO_30CAL;
case DODW_HANDGRENADE, DODW_STICKGRENADE, DODW_STICKGRENADE_EX, DODW_HANDGRENADE_EX, DODW_MILLS_BOMB :
return AMMO_GREN;
case DODW_BAZOOKA, DODW_PANZERSCHRECK, DODW_PIAT :
return AMMO_ROCKET;
}

return 0;
}

public dod_ghset_hud_ammo(id, wpnid, ammo)
{//frontend for set_ammo(channel, ammo) to auto-select the right channel
/*
#define AMMO_SMG 1 // thompson, greasegun, sten, mp40
#define AMMO_ALTRIFLE 2 // carbine, k43, mg34
#define AMMO_RIFLE 3 // garand, enfield, scoped enfield, k98, scoped k98
#define AMMO_PISTOL 4 // colt, webley, luger
#define AMMO_SPRING 5 // springfield
#define AMMO_HEAVY 6 // bar, bren, stg44, fg42, scoped fg42
#define AMMO_MG42 7 // mg42
#define AMMO_30CAL 8 // 30cal
#define AMMO_GREN 9 // grenades (should be all 3 types)
#define AMMO_ROCKET 13 // bazooka, piat, panzerschreck
*/
switch( wpnid )
{
case DODW_THOMPSON, DODW_GREASEGUN, DODW_STEN, DODW_MP40 :
set_ammo(id, AMMO_SMG, ammo );
case DODW_M1_CARBINE, DODW_K43, DODW_MG34 :
set_ammo(id, AMMO_ALTRIFLE, ammo );
case DODW_GARAND, DODW_ENFIELD, DODW_SCOPED_ENFIELD, DODW_KAR, DODW_SCOPED_KAR :
set_ammo(id, AMMO_RIFLE, ammo );
case DODW_COLT, DODW_LUGER, DODW_WEBLEY :
set_ammo(id, AMMO_PISTOL, ammo );
case DODW_SPRINGFIELD :
set_ammo(id, AMMO_SPRING, ammo );
case DODW_BAR, DODW_BREN, DODW_STG44, DODW_FG42, DODW_SCOPED_FG42 :
set_ammo(id, AMMO_HEAVY, ammo );
case DODW_MG42 :
set_ammo(id, AMMO_MG42, ammo );
case DODW_30_CAL :
set_ammo(id, AMMO_30CAL, ammo );
case DODW_HANDGRENADE, DODW_STICKGRENADE, DODW_STICKGRENADE_EX, DODW_HANDGRENADE_EX, DODW_MILLS_BOMB :
set_ammo(id, AMMO_GREN, ammo );
case DODW_BAZOOKA, DODW_PANZERSCHRECK, DODW_PIAT :
set_ammo(id, AMMO_ROCKET, ammo );
}
}

public set_ammo(id, channel, ammo)
{//refresh the hud view of ammo (right bottom corner) to new amount..
if(gmsgSetAmmoX>0)
{
message_begin(MSG_ONE,gmsgSetAmmoX,{0,0,0},id);
write_byte(channel);//look top defines of channels
write_byte(ammo);
message_end();
}
}

netmad
11-20-2010, 10:22 PM
but only same ammo weapon can show ammo, if change Bren to MG34 ammo HUD,dont show bpammo