Raised This Month: $ Target: $400
 0% 

Giving weapons to players


Post New Thread Reply   
 
Thread Tools Display Modes
BAILOPAN
Join Date: Jan 2004
Old 04-29-2004 , 05:08  
Reply With Quote #11

The reason for not using new inside a loop or if statements is multifold.

I don't know if this is still the case, but in early version of the small abstract machine, it wouldn't remove the variable from the stack properly, causing stack corruption. RavenousBugBlatter gives this way of crashing HLDS:
Code:
  for(new i=1;i<10;i++) {     break;   }

Again, I don't know if that is still the case. Within if statements, it leads to weird memory allocation problems that the compiler may not handle correctly. You should always define all variables at the start of a function, like in C.
__________________
egg
BAILOPAN is offline
SidLuke
Senior Member
Join Date: Mar 2004
Location: Poland, Chrzanow
Old 04-29-2004 , 06:15  
Reply With Quote #12

Code:
public give_weapon(admin_index,victim_index,weapon_give[]) {     new arg1[32]     read_argv(1,arg1,32)     new team[32]     get_user_team(victim_index,team,32)     new name[32]     get_user_name(victim_index,name,32)     if (equal(arg1,"@"))     {     if (equal(team,"CT"))         {         set_hudmessage(200, 50, 0, -1.0, 0.30, 0, 6.0, 6.0, 0.5, 0.15, 1)         show_hudmessage(0,"The Counter-Terrorists Have Been Given Weapons")         }         else if (equal(team,"TERRORIST"))         {         set_hudmessage(200, 50, 0, -1.0, 0.30, 0, 6.0, 6.0, 0.5, 0.15, 1)         show_hudmessage(0,"The Terrorists Have Been Given Weapons")         }     }     else     {     set_hudmessage(200, 50, 0, -1.0, 0.30, 0, 6.0, 6.0, 0.5, 0.15, 1)     show_hudmessage(0,"%s Has Been Given A Weapon",name)     } //Pistols     if ( equal(weapon_give,"usp") ){     give_item(victim_index,"weapon_usp")     give_item(victim_index,"ammo_45acp")     give_item(victim_index,"ammo_45acp")     give_item(victim_index,"ammo_45acp")     give_item(victim_index,"ammo_45acp")     give_item(victim_index,"ammo_45acp")     give_item(victim_index,"ammo_45acp")     give_item(victim_index,"ammo_45acp")     give_item(victim_index,"ammo_45acp")     }     else if ( equal(weapon_give,"glock"){     give_item(victim_index,"weapon_glock18")     give_item(victim_index,"ammo_9mm")     give_item(victim_index,"ammo_9mm")     give_item(victim_index,"ammo_9mm")     give_item(victim_index,"ammo_9mm")     give_item(victim_index,"ammo_9mm")     give_item(victim_index,"ammo_9mm")     give_item(victim_index,"ammo_9mm")     give_item(victim_index,"ammo_9mm")     }     else if ( equal(weapon_give,"deagle") ){     give_item(victim_index,"weapon_deagle")     give_item(victim_index,"ammo_50ae")     give_item(victim_index,"ammo_50ae")     give_item(victim_index,"ammo_50ae")     give_item(victim_index,"ammo_50ae")     give_item(victim_index,"ammo_50ae")     give_item(victim_index,"ammo_50ae")     give_item(victim_index,"ammo_50ae")     }     else if (equal(weapon_give,"p228") ){     give_item(victim_index,"weapon_p228")     give_item(victim_index,"ammo_357sig")     give_item(victim_index,"ammo_357sig")     give_item(victim_index,"ammo_357sig")     give_item(victim_index,"ammo_357sig")     give_item(victim_index,"ammo_357sig")     give_item(victim_index,"ammo_357sig")     } **********************************************     else     {     client_print(admin_index,print_console,"[AMXX] There is no such weapon")     return PLUGIN_CONTINUE     }     client_print(admin_index,print_console,"[AMXX] Client ^"%s^" has been given weapon",name)     return PLUGIN_CONTINUE } public admin_weapon(id){     if (!(get_user_flags(id)&ADMIN_LEVEL_A)){         client_print(id,print_console,"[AMXX] You have no access to that command")         return PLUGIN_HANDLED     }     new argc = read_argc()     if (argc < 3)     {         client_print(id,print_console,"[AMXX] Usage: amx_weapon <part of nick> or <@team> <weapon to give>")         return PLUGIN_HANDLED     }     new arg1[32]     new arg2[32]     new arg3[32]     read_argv(1,arg1,32)     read_argv(2,arg2,32)     read_argv(3,arg3,32) //Team     if (equal(arg1,"@"))     {         new players[32], inum         get_players(players,inum,"e",arg2)         for(new i = 0 ;i < inum ;++i)             give_weapon(id,players[i],arg3)         if (inum)             client_print(id,print_console,"[AMXX] * All clients from ^"%s^" got a weapon *",arg2)         else             client_print(id,print_console,"[AMXX] No clients in such team")     } //Index     if (equal(arg1,"#"))     {         if (is_user_connected(str_to_num(arg2)))             give_weapon(id,str_to_num(arg2),arg3)         else             client_print(id,print_console,"[AMXX] Client not found")     } //Part of Name     else     {         new player = find_player("lb",arg1)         if (player)             give_weapon(id,player,arg2)         else             client_print(id,print_console,"[AMXX] Client with that part of nick not found")     }     return PLUGIN_HANDLED }

Give weapon call example:
give_weapon(1,5,"glock") // 1-adminId, 5-playerId, "glock" - weapon

Add missing weapons and try it.
SidLuke is offline
Send a message via AIM to SidLuke Send a message via MSN to SidLuke
BigBaller
Veteran Member
Join Date: Mar 2004
Location: Everett, WA
Old 04-29-2004 , 07:34  
Reply With Quote #13

OK SidLuke, thanks for the help, I will try it when i am not sleepy hehe.
__________________

BigBaller is offline
karlos
Veteran Member
Join Date: Apr 2004
Location: Germany/Poland
Old 04-29-2004 , 13:22  
Reply With Quote #14

Quote:
Originally Posted by BAILOPAN
RavenousBugBlatter gives this way of crashing HLDS:
Code:
  for(new i=1;i<10;i++) {     break;   }
i just tested it : no crash, working normal
but i will remember for future

PS: i am learing from sourcecode and i can find this in many plugins
karlos is offline
PM
hello, i am pm
Join Date: Jan 2004
Location: Canalization
Old 04-29-2004 , 13:43  
Reply With Quote #15

i know that this caused stack corruption (that is no crash, only a runtime error)

Code:
if (1)    new hahaha[256];
EDIT:
Maybe this lol
Code:
if (0)    new hahaha[256];
__________________
hello, i am pm
PM is offline
BAILOPAN
Join Date: Jan 2004
Old 04-29-2004 , 14:12  
Reply With Quote #16

yes, many people do it one thing about small is that it is easy and allows people to make mistakes they do not know about.

I would never use a variable inlined into a variable expression, has a long history of stack corruption
__________________
egg
BAILOPAN is offline
BigBaller
Veteran Member
Join Date: Mar 2004
Location: Everett, WA
Old 04-29-2004 , 23:41  
Reply With Quote #17

Hey SidLuke, using what you gave me the compiler gives me the error

error 035: argument type mismatch (argument 1)

I looked at the line that the error occured and it shows that line being

Code:
if ( equal(weapon_give,"usp") )
__________________

BigBaller is offline
SidLuke
Senior Member
Join Date: Mar 2004
Location: Poland, Chrzanow
Old 04-30-2004 , 11:16  
Reply With Quote #18

post full code
SidLuke is offline
Send a message via AIM to SidLuke Send a message via MSN to SidLuke
Girthesniper
Senior Member
Join Date: Mar 2004
Location: Maryland
Old 05-02-2004 , 14:55  
Reply With Quote #19

Quote:
Originally Posted by ts2do
why not just give em all of the weapons
I did for my plugin. But the problem is, there is so many weapons, that your HUD goes way off screen.
__________________
BANNED
Girthesniper is offline
Send a message via AIM to Girthesniper Send a message via MSN to Girthesniper
AssKicR
Veteran Member
Join Date: Mar 2004
Location: Norway-Europe(GTM+1)
Old 05-02-2004 , 16:06  
Reply With Quote #20

maybe u can give weapons by setting EV_INT_weapons =/
__________________
My Plugins

Got ??
AssKicR 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 22:26.


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