Day of Defeat Plugins Community
This site is an archive — everything is readable and downloadable, nothing can be posted. Admin
Archive › Old Forum Threads › Help › Tutorials › Update Ammo HUD
15 posts, 8 people · 4,358 views Thread Rating: ★★★★★
closedUpdate Ammo HUD
Started by Wilson [29th ID] on 09-01-2006, 05:45 PM
Wilson [29th ID]
started this thread
Posts: 623
Threads: 57
Joined: Oct 2005
09-01-2006, 05:45 PM #1

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.rnrnYou'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."rnrnClip ammo - the ammo you're currently firing that's inside your gun - clip/magazine/whatever it's called for that gun.rnrnBackpack ammo - the ammo that's "in your backpack" - the extra clips/magazinesrnrnUpdating 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.rnrnIt 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.rnrnThe 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.rnrn

#define AMMO_SMG 1 // thompson, greasegun, sten, mp40rn#define AMMO_ALTRIFLE 2 // carbine, k43, mg34rn#define AMMO_RIFLE 3 // garand, enfield, scoped enfield, k98, scoped k98rn#define AMMO_PISTOL 4 // colt, webley, lugerrn#define AMMO_SPRING 5 // springfieldrn#define AMMO_HEAVY 6 // bar, bren, stg44, fg42, scoped fg42rn#define AMMO_MG42 7    // mg42rn#define AMMO_30CAL 8 // 30calrn#define AMMO_GREN 9 // grenades (should be all 3 types)rn#define AMMO_ROCKET 13 // bazooka, piat, panzerschreck
rnrnNow to actually update the backpack ammo, you can create a function like below.rnrn
public set_ammo(channel, ammo)rn	message_begin(MSG_ONE,gmsgAmmoX,{0,0,0},id);rn	write_byte(channel);rn	write_byte(ammo);rn	message_end();rn}
rnrnThen you can call the function like below. This will set the K98 to have 3 backpack clips (they each contain 5 rounds)rnrn
set_ammo(AMMO_RIFLE, 15)
rnrnAlso 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.rnBasically, if you only do the above, when you reload, it will go to the number it was at before (minus 1 since you reloaded)rnrn--------------------------------------------------------rnNow setting the clip itself is a lot harder and it took a while to figure out. Just use this function.rnrnThis will set the k98 clip to have only 4 rounds.rn
set_clip(id, "weapon_kar", 4)
rnrn
public set_clip(id,const weapon[],clip) {rnrn	new currentent = -1, gunid = 0rnrn	// get originrn	new Float:origin[3];rn	entity_get_vector(id,EV_VEC_origin,origin);rn	rn	while((currentent = find_ent_in_sphere(currentent,origin,Float:1.0)) != 0) {rn		new classname[32];rn		entity_get_string(currentent,EV_SZ_classname,classname,31);rn	rn		if(equal(classname,weapon))rn			gunid = currententrn	rn	}rn	rn	set_pdata_int(gunid,108,clip); // set their ammorn	rn	return PLUGIN_CONTINUErn}
rnrnNote that the clip setting does affect pdata and not just the HUD.rnrnLike 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
posted here
Posts: 2,068
Threads: 103
Joined: May 2005
09-01-2006, 08:08 PM #2

good stuff...nn wish you posted this months ago haha :-)

H Hell Phoenix
posted here
Posts: 201
Threads: 17
Joined: Jan 2005
09-03-2006, 11:52 AM #3

Wow thats awesome! Gotta update some plugins now ;D

Zor
posted here
Posts: 688
Threads: 75
Joined: Jun 2006
09-03-2006, 01:49 PM #4

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:nn

n// In the plugin_initnregister_event("Object", "hook_object", "b")nn// Show the blood icon in the functionnmessage_begin(MSG_ONE, gmsgObject, {0,0,0}, victim)nwrite_string(fileNames[drop])nmessage_end()
nnNow the code I used has the following:nn
struct sUserMsgn{n    const char *name;n    int* id;n    FunctionEventCall function;n}nng_user_msg[] = n{n    { "CurWeapon", &gmsgCurWeapon, Client_CurWeapon },n    { "Object", &gmsgObject, Client_Object },n    { 0,0,0 }n};
nnAnd 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...nnCheers!

diamond-optic
posted here
Posts: 2,068
Threads: 103
Joined: May 2005
09-03-2006, 08:53 PM #5

zor.. what the hecks that gotta do with updating ammo on the hud... lmao

Wilson [29th ID]
started this thread
Posts: 623
Threads: 57
Joined: Oct 2005
09-03-2006, 09:35 PM #6

Nah Zor, you'd be better off asking that in amxmodx forums...Twilight would know.

Zor
posted here
Posts: 688
Threads: 75
Joined: Jun 2006
09-04-2006, 12:20 PM #7

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!nnCheers!

S Spectral2
posted here
Posts: 16
Threads: 1
Joined: Mar 2007
04-25-2007, 10:40 PM #8

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]
started this thread
Posts: 623
Threads: 57
Joined: Oct 2005
04-26-2007, 03:50 PM #9

Yeah, actually that should work - I think I have done that before - gave my thompson like 100 rounds. Man, that was fun.

Tank
posted here
Posts: 581
Threads: 59
Joined: Jan 2007
11-30-2008, 04:05 PM #10

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
posted here
Posts: 160
Threads: 42
Joined: Sep 2007
03-02-2009, 08:27 PM #11

how to catch msgid for AmmoX ?rnrngmsgAmmoX = get_user_msgid("???????");

diamond-optic
posted here
Posts: 2,068
Threads: 103
Joined: May 2005
03-02-2009, 08:47 PM #12

get_user_msgid("AmmoX")

ghzero
posted here
Posts: 160
Threads: 42
Joined: Sep 2007
03-04-2009, 12:51 PM #13

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
posted here
Posts: 160
Threads: 42
Joined: Sep 2007
03-04-2009, 03:08 PM #14

whats the reason for this error:rnPlugin called message_begin with an invalid message id (0).rn[AMXX] Run time error 10: native error (native "message_begin")rnset_ammo (line 1644)rnrnby using this code:rnpublic set_ammo(id, channel, ammo)rn{rn message_begin(MSG_ONE,gmsgSetAmmoX,{0,0,0},id);// LINE 1644 !!!!rn write_byte(channel);rn write_byte(ammo);rn message_end();rn}rnrnrnEDIT:rnOMG - I have forgotten to catch the msgid by/in plugin_init():rngmsgSetAmmoX = get_user_msgid("AmmoX");rnrn:-)rn(scusi)rnrnrnEDIT:rnmaybe this functions may help you..rn

rnpublic dod_ghget_ammochannel(wpnid)rn{//to check if ammo type is compatible by their channelsrn	switch( wpnid )rn	{rn		case DODW_THOMPSON, DODW_GREASEGUN, DODW_STEN, DODW_MP40 :rn			return AMMO_SMG;rn		case DODW_M1_CARBINE, DODW_K43, DODW_MG34 :rn			return AMMO_ALTRIFLE;rn		case DODW_GARAND, DODW_ENFIELD, DODW_SCOPED_ENFIELD, DODW_KAR, DODW_SCOPED_KAR :rn			return AMMO_RIFLE;rn		case DODW_COLT, DODW_LUGER, DODW_WEBLEY :rn			return AMMO_PISTOL;rn		case DODW_SPRINGFIELD :rn			return AMMO_SPRING;rn		case DODW_BAR, DODW_BREN, DODW_STG44, DODW_FG42, DODW_SCOPED_FG42 :rn			return AMMO_HEAVY;rn		case DODW_MG42 :rn			return AMMO_MG42;rn		case DODW_30_CAL :rn			return AMMO_30CAL;rn		case DODW_HANDGRENADE, DODW_STICKGRENADE, DODW_STICKGRENADE_EX, DODW_HANDGRENADE_EX, DODW_MILLS_BOMB :rn			return AMMO_GREN;rn		case DODW_BAZOOKA, DODW_PANZERSCHRECK, DODW_PIAT :rn			return AMMO_ROCKET;rn	}rn	rn	return 0;rn}rnrnpublic dod_ghset_hud_ammo(id, wpnid, ammo)rn{//frontend for set_ammo(channel, ammo) to auto-select the right channelrn/*rn #define AMMO_SMG 1		// thompson, greasegun, sten, mp40rn #define AMMO_ALTRIFLE 2	// carbine, k43, mg34rn #define AMMO_RIFLE 3	// garand, enfield, scoped enfield, k98, scoped k98rn #define AMMO_PISTOL 4	// colt, webley, lugerrn #define AMMO_SPRING 5	// springfieldrn #define AMMO_HEAVY 6	// bar, bren, stg44, fg42, scoped fg42rn #define AMMO_MG42 7		// mg42rn #define AMMO_30CAL 8	// 30calrn #define AMMO_GREN 9		// grenades (should be all 3 types)rn #define AMMO_ROCKET 13	// bazooka, piat, panzerschreckrn*/rn	switch( wpnid )rn	{rn		case DODW_THOMPSON, DODW_GREASEGUN, DODW_STEN, DODW_MP40 :rn			set_ammo(id, AMMO_SMG, ammo );rn		case DODW_M1_CARBINE, DODW_K43, DODW_MG34 :rn			set_ammo(id, AMMO_ALTRIFLE, ammo );rn		case DODW_GARAND, DODW_ENFIELD, DODW_SCOPED_ENFIELD, DODW_KAR, DODW_SCOPED_KAR :rn			set_ammo(id, AMMO_RIFLE, ammo );rn		case DODW_COLT, DODW_LUGER, DODW_WEBLEY :rn			set_ammo(id, AMMO_PISTOL, ammo );rn		case DODW_SPRINGFIELD :rn			set_ammo(id, AMMO_SPRING, ammo );rn		case DODW_BAR, DODW_BREN, DODW_STG44, DODW_FG42, DODW_SCOPED_FG42 :rn			set_ammo(id, AMMO_HEAVY, ammo );rn		case DODW_MG42 :rn			set_ammo(id, AMMO_MG42, ammo );rn		case DODW_30_CAL :rn			set_ammo(id, AMMO_30CAL, ammo );rn		case DODW_HANDGRENADE, DODW_STICKGRENADE, DODW_STICKGRENADE_EX, DODW_HANDGRENADE_EX, DODW_MILLS_BOMB :rn			set_ammo(id, AMMO_GREN, ammo );rn		case DODW_BAZOOKA, DODW_PANZERSCHRECK, DODW_PIAT :rn			set_ammo(id, AMMO_ROCKET, ammo );rn	}rn}rnrnpublic set_ammo(id, channel, ammo)rn{//refresh the hud view of ammo (right bottom corner) to new amount..rn	if(gmsgSetAmmoX>0)rn	{rn		message_begin(MSG_ONE,gmsgSetAmmoX,{0,0,0},id);rn		write_byte(channel);//look top defines of channelsrn		write_byte(ammo);rn		message_end();rn	}rn}rn
N netmad
posted here
Posts: 24
Threads: 5
Joined: May 2009
11-20-2010, 10:22 PM #15

but only same ammo weapon can show ammo, if change Bren to MG34 ammo HUD,dont show bpammo

Archive › Old Forum Threads › Help › Tutorials › Update Ammo HUD
DoD Plugins Archive · Repository · Old Forum Threads · Maps · Admin · built 14 Sep 2026 00:34 UTC