PDA

View Full Version : smart weapon response



Ni3ls
15th December 2014, 15:20
Hi all,

I got a menu file for weapons. All scriptmenuresponses are 1 ,2 till 41.

normally u have

if(response == 1)
self giveweapon("greasegun_mp");
Etc for all responses.

Is there a smarter way to do this? I was thinking about this


weapon1="greasegun_mp";
weapon2="mp40_mp";
weapon3="pps42_mp";
weapon4="ppsh_mp";
weapon5="sten_mp";
weapon6="thompson_mp";
weapon7="mac10_mp";
weapon8="ak74_mp";
weapon9="mp5_mp";
weapon10="ump45_mp";
weapon11="p90_mp";
weapon12="bar_mp";
weapon13="bren_mp";
weapon14="";
weapon15="fg42_mp";
weapon16="rpd_mp";
weapon17="acr_mp";
weapon18="g43_mp";
weapon19="m1garand_mp";
weapon20="m1carbine_mp";
weapon21="mp44_mp";
weapon22="svt40_mp";
weapon23="r700_mp";
weapon24="barret_mp";
weapon25="kar98k_mp";
weapon26="enfield_mp";
weapon27="mosing_nagant_mp";
weapon28="springfield_mp";
weapon29="";
weapon30="m4";
weapon31="aug_mp";
weapon32="sig_mp";
weapon33="ak47_mp";
weapon34="famas_mp";
weapon35="shotgun_mp";
weapon36="winchester_mp";
weapon37="db_mp";
weapon38="benneli_mp";
weapon39="kar98k_sniper_mp";
weapon40="enfield_scope_mp";
weapon41="mosin_nagant_sniper_mp";
And then
self giveweapon(weapon+response);
Something like that. But that doesnt work :P Who can help me out?

serthy
15th December 2014, 16:01
But that doesnt work
why does this not work for you?
I'd go with that, but maybe not hardcoding the weaponnames. you could fetch the names by reading a file or mysql

vanfreddy
15th December 2014, 16:25
maybe u missed the "" ?
self giveweapon("weapon"+response);

Ni3ls
15th December 2014, 16:29
weapon0="colt_mp";
weapon1="kar98k_mp";
weapon2="springfield_mp";

if(self.name == "Niels1")
response =1;
if(self.name == "Niels2")
response =2;
else
response =0;

self takeallweapons();
self giveweapon(weapon+response);

Error

uninitialised variable 'weapon': (file 'maps/mp/gametypes/sd.gsc', line 990)
self giveweapon(weapon+response);

IzNoGoD
15th December 2014, 16:31
init()
{

precacheitem("gnnr_desert");
precacheitem("gnnr_desert_dt");
precacheitem("gnnr_desert_soh");
precacheItem("fraggrenade_mp");
precacheitem("fraggrenade_idle_mp");
precacheItem("smokegrenade_mp");
precacheitem("stun_mp");
precacheitem("flashbang_mp");

precacheItem("binoculars_mp");

level.weaponnames = [];
level.weaponnames[level.weaponnames.size] = "mw_ak47_mp";
level.weaponnames[level.weaponnames.size] = "mw_ak74_mp";
level.weaponnames[level.weaponnames.size] = "mw_barrett_mp";
level.weaponnames[level.weaponnames.size] = "mw_dragunov_mp";
level.weaponnames[level.weaponnames.size] = "mw_g3_mp";
level.weaponnames[level.weaponnames.size] = "mw_g36_mp";
level.weaponnames[level.weaponnames.size] = "mw_m4_mp";
level.weaponnames[level.weaponnames.size] = "mw_m14_mp";
level.weaponnames[level.weaponnames.size] = "mw_m21_mp";
level.weaponnames[level.weaponnames.size] = "mw_m40a3_mp";
level.weaponnames[level.weaponnames.size] = "mw_m60_mp";
level.weaponnames[level.weaponnames.size] = "mw_m249_mp";
level.weaponnames[level.weaponnames.size] = "mw_m1014_mp";
level.weaponnames[level.weaponnames.size] = "mw_mp5_mp";
level.weaponnames[level.weaponnames.size] = "mw_p90_mp";
level.weaponnames[level.weaponnames.size] = "mw_r700_mp";
level.weaponnames[level.weaponnames.size] = "mw_rpd_mp";
level.weaponnames[level.weaponnames.size] = "mw_winchester_mp";

level.weapons = [];
for(i=0;i<level.weaponnames.size;i++)
{
precacheitem(level.weaponnames[i]);
precacheitem(level.weaponnames[i]+"_soh");
precacheitem(level.weaponnames[i]+"_dt");
level.weapons[level.weaponnames[i]]=spawnstruct();
if(getsubstr(level.weaponnames[i],level.weaponnames[i].size-3,level.weaponnames[i].size)=="_mp")
{
level.weapons[level.weaponnames[i]].server_allowcvar="scr_allow_"+getsubstr(level.weaponnames[i],0,level.weaponnames[i].size-3);
level.weapons[level.weaponnames[i]].client_allowcvar="ui_allow_"+getsubstr(level.weaponnames[i],0,level.weaponnames[i].size-3);
level.weapons[level.weaponnames[i]].min_rank_cvar="scr_minrank_"+getsubstr(level.weaponnames[i],0,level.weaponnames[i].size-3);
}
else
{
level.weapons[level.weaponnames[i]].server_allowcvar="scr_allow_"+level.weaponnames[i];
level.weapons[level.weaponnames[i]].client_allowcvar="ui_allow_"+level.weaponnames[i];
level.weapons[level.weaponnames[i]].min_rank_cvar="scr_minrank_"+level.weaponnames[i];
}
}

for(i = 0; i < level.weaponnames.size; i++)
{
weaponname = level.weaponnames[i];

if(getCvar(level.weapons[weaponname].server_allowcvar) == "")
{
level.weapons[weaponname].allow = 1;
setCvar(level.weapons[weaponname].server_allowcvar, level.weapons[weaponname].allow);
}
else
level.weapons[weaponname].allow = getCvarInt(level.weapons[weaponname].server_allowcvar);
if(getCvar(level.weapons[weaponname].min_rank_cvar) == "")
{
level.weapons[weaponname].min_rank = 0;
setCvar(level.weapons[weaponname].min_rank_cvar, level.weapons[weaponname].min_rank);
}
else
level.weapons[weaponname].min_rank = getCvarInt(level.weapons[weaponname].min_rank_cvar);
}

for(;;)
{
updateAllowed();
wait 5;
}
}

replaces the init() in _weapons.gsc (which is waaaay too long tbh).

Only donwside: original init also controlled frag/smokegrenades, you'll need to hack that in somewhere else
Now you can easily giveweapon(level.weaponnames[number]);

serthy
15th December 2014, 16:41
well vanfreddy and the console show'd u the answer to ur problem
izno gave u a best practice example

Ni3ls
15th December 2014, 16:45
I dont understand that script, too complicated for me. I dont see where it responds to a scriptresponse and where you actually give/set the weapon

Ni3ls
15th December 2014, 16:48
maybe u missed the "" ?
self giveweapon("weapon"+response);

I already tried that too, but then it says
Unknown weapon weapon0
It doesn't check for the corresponding weapon :/

Tally
15th December 2014, 16:49
weapon0="colt_mp";
weapon1="kar98k_mp";
weapon2="springfield_mp";

if(self.name == "Niels1")
response =1;
if(self.name == "Niels2")
response =2;
else
response =0;

self takeallweapons();
self giveweapon(weapon+response);

Error

uninitialised variable 'weapon': (file 'maps/mp/gametypes/sd.gsc', line 990)
self giveweapon(weapon+response);

Using your idea of weapon + number + corresponding weapon, you would have to build a player array out of it:


self.weapon1="greasegun_mp";
self.weapon2="mp40_mp";
self.weapon3="pps42_mp";
self.weapon4="ppsh_mp";
self.weapon5="sten_mp";
self.weapon6="thompson_mp";
self.weapon7="mac10_mp";
self.weapon8="ak74_mp";
self.weapon9="mp5_mp";
self.weapon10="ump45_mp";
self.weapon11="p90_mp";
self.weapon12="bar_mp";
self.weapon13="bren_mp";
self.weapon14="";
self.weapon15="fg42_mp";
self.weapon16="rpd_mp";
self.weapon17="acr_mp";
self.weapon18="g43_mp";
self.weapon19="m1garand_mp";
self.weapon20="m1carbine_mp";
self.weapon21="mp44_mp";
self.weapon22="svt40_mp";
self.weapon23="r700_mp";
self.weapon24="barret_mp";
self.weapon25="kar98k_mp";
self.weapon26="enfield_mp";
self.weapon27="mosing_nagant_mp";
self.weapon28="springfield_mp";
self.weapon29="";
self.weapon30="m4";
self.weapon31="aug_mp";
self.weapon32="sig_mp";
self.weapon33="ak47_mp";
self.weapon34="famas_mp";
self.weapon35="shotgun_mp";
self.weapon36="winchester_mp";
self.weapon37="db_mp";
self.weapon38="benneli_mp";
self.weapon39="kar98k_sniper_mp";
self.weapon40="enfield_scope_mp";
self.weapon41="mosin_nagant_sniper_mp";


Build that onPlayerConnected(). Now, your giveWeapon() function will work:


self giveWeapon( self.weapon + response );

Tally
15th December 2014, 16:54
Using IznoGod's idea of the weapon array in maps\mp\gametypes\_weapons::init(), you can do this:


level.weapons["m1carbine_mp"] = spawnstruct();
level.weapons["m1carbine_mp"].server_allowcvar = "scr_allow_m1carbine";
level.weapons["m1carbine_mp"].client_allowcvar = "ui_allow_m1carbine";
level.weapons["m1carbine_mp"].allow_default = 1;
level.weapons["m1carbine_mp"].response_number = 1;

level.weapons["m1garand_mp"] = spawnstruct();
level.weapons["m1garand_mp"].server_allowcvar = "scr_allow_m1garand";
level.weapons["m1garand_mp"].client_allowcvar = "ui_allow_m1garand";
level.weapons["m1garand_mp"].allow_default = 1;
level.weapons["m1garand_mp"].response_number = 2;

--------- ETC -------------------


Which would allow you to check the response_number against the menu response:



for(i = 0; i < level.weaponnames.size; i++)
{
weaponname = level.weaponnames[i];

if( level.weapons[weaponname].response_number == response )
self giveWeapon( weaponname );
}

serthy
15th December 2014, 16:59
Build that onPlayerConnected(). Now, your giveWeapon() function will work:


self giveWeapon( self.weapon + response );

Are you sure? :confused::cool:

Ni3ls
15th December 2014, 17:08
Tally what do you mean with "Build a player array out of it"?

Tally
15th December 2014, 17:16
Tally what do you mean with "Build a player array out of it"?

I mean create an array. Basic Scripting 101 - only "self" and "level" definitions can be used throughout a script - you can pass the definition across to other functions. Otherwise, the definition would only work in a particular function. Using the word "weapon" wont carry its struct across to other functions. Hence why you were getting the "undefined" error - you created the weapon + number array in a function where and once your script left that function, the definition was dead (i.e. no longer valid).

Tally
15th December 2014, 17:17
Are you sure? :confused::cool:

LOL no! I'm not sure! Completely untested, but it looks right to me. Why? Do you foresee a problem with it?

Ni3ls
15th December 2014, 17:39
I mean create an array. Basic Scripting 101 - only "self" and "level" definitions can be used throughout a script - you can pass the definition across to other functions. Otherwise, the definition would only work in a particular function. Using the word "weapon" wont carry its struct across to other functions. Hence why you were getting the "undefined" error - you created the weapon + number array in a function where and once your script left that function, the definition was dead (i.e. no longer valid).

Oke now i feel really dumb. I had an exam today and my brain is still messed... I know how to build a player array, but I don't know why to do that and what should inside that array.

Tally
15th December 2014, 18:05
Oke now i feel really dumb. I had an exam today and my brain is still messed... I know how to build a player array, but I don't know why to do that and what should inside that array.

Try my second idea. It uses an existing array, and you wont have to create a new one.

YuriJurek
15th December 2014, 18:07
Why all this hassle?



level.weapon = [];

level.weapon[0] = "greasegun_mp";
level.weapon[1] = "mp40_mp";
level.weapon[2] = "pps42_mp";
level.weapon[3] = "ppsh_mp";
level.weapon[4] = "sten_mp";
level.weapon[5] = "thompson_mp";
level.weapon[6] = "mac10_mp";
level.weapon[7] = "ak74_mp";
level.weapon[8] = "mp5_mp";
level.weapon[9] = "ump45_mp";
level.weapon[10] = "p90_mp";
level.weapon[11] = "bar_mp";
level.weapon[12] = "bren_mp";
level.weapon[13] = "";
level.weapon[14] = "fg42_mp";
level.weapon[15] = "rpd_mp";
level.weapon[16] = "acr_mp";
level.weapon[17] = "g43_mp";
level.weapon[18] = "m1garand_mp";
level.weapon[19] = "m1carbine_mp";
level.weapon[20] = "mp44_mp";
level.weapon[21] = "svt40_mp";
level.weapon[22] = "r700_mp";
level.weapon[23] = "barret_mp";
level.weapon[24] = "kar98k_mp";
level.weapon[25] = "enfield_mp";
level.weapon[26] = "mosing_nagant_mp";
level.weapon[27] = "springfield_mp";
level.weapon[28] = "";
level.weapon[29] = "m4";
level.weapon[30] = "aug_mp";
level.weapon[31] = "sig_mp";
level.weapon[32] = "ak47_mp";
level.weapon[33] = "famas_mp";
level.weapon[34] = "shotgun_mp";
level.weapon[35] = "winchester_mp";
level.weapon[36] = "db_mp";
level.weapon[37] = "benneli_mp";
level.weapon[38] = "kar98k_sniper_mp";
level.weapon[39] = "enfield_scope_mp";
level.weapon[40] = "mosin_nagant_sniper_mp";


for(i = 0; i < level.weapon.size; i++)
{
if(int(response) == i)
self giveweapon(level.weapon[i]);
}

You can also alter the "i" value if the response doesn't match the weapon due to an array starting with index of 0.

Tally
15th December 2014, 18:12
Why all this hassle?



level.weapon = [];

level.weapon[0] = "greasegun_mp";
level.weapon[1] = "mp40_mp";
level.weapon[2] = "pps42_mp";
level.weapon[3] = "ppsh_mp";
level.weapon[4] = "sten_mp";
level.weapon[5] = "thompson_mp";
level.weapon[6] = "mac10_mp";
level.weapon[7] = "ak74_mp";
level.weapon[8] = "mp5_mp";
level.weapon[9] = "ump45_mp";
level.weapon[10] = "p90_mp";
level.weapon[11] = "bar_mp";
level.weapon[12] = "bren_mp";
level.weapon[13] = "";
level.weapon[14] = "fg42_mp";
level.weapon[15] = "rpd_mp";
level.weapon[16] = "acr_mp";
level.weapon[17] = "g43_mp";
level.weapon[18] = "m1garand_mp";
level.weapon[19] = "m1carbine_mp";
level.weapon[20] = "mp44_mp";
level.weapon[21] = "svt40_mp";
level.weapon[22] = "r700_mp";
level.weapon[23] = "barret_mp";
level.weapon[24] = "kar98k_mp";
level.weapon[25] = "enfield_mp";
level.weapon[26] = "mosing_nagant_mp";
level.weapon[27] = "springfield_mp";
level.weapon[28] = "";
level.weapon[29] = "m4";
level.weapon[30] = "aug_mp";
level.weapon[31] = "sig_mp";
level.weapon[32] = "ak47_mp";
level.weapon[33] = "famas_mp";
level.weapon[34] = "shotgun_mp";
level.weapon[35] = "winchester_mp";
level.weapon[36] = "db_mp";
level.weapon[37] = "benneli_mp";
level.weapon[38] = "kar98k_sniper_mp";
level.weapon[39] = "enfield_scope_mp";
level.weapon[40] = "mosin_nagant_sniper_mp";


for(i = 0; i < level.weapon[size]; i++)
{
if(int(response) == i)
self giveweapon(level.weapon[i]);
}

You can also alter the "i" value if the response doesn't match the weapon due to an array starting with index of 0.

You wouldn't need to cycle through them all:


self giveweapon( level.weapon[ int( response ) ] );

YuriJurek
15th December 2014, 18:13
You wouldn't need to cycle through them all:


self giveweapon( level.weapon[ int( response ) ] );

Thanks, even better :)