AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Random Numbers (https://forums.alliedmods.net/showthread.php?t=47182)

The Specialist 11-11-2006 20:27

Random Numbers
 
Ok for my "weapon Jams" plugin im trying to make a random weapon jam function . but what I have seems to be crashing my server . any ideas whats wrong with this function here?
Code:
// Random number generator public random_jam_gen() {  // if random cvars is 0 (off) end function    if(get_pcvar_num(g_iRandom)==0)  {   return PLUGIN_HANDLED;  }else{   // else ratio == a random number between random cvar and ratio cvar     g_iRatio =  random_num(get_pcvar_num(g_iRandom),get_pcvar_num(g_iRatio));  }  return PLUGIN_HANDLED; }
Thanks

teame06 11-11-2006 20:42

Re: Random Numbers
 
Everything look fine but I don't know why you did that above. Your just putting more use code into your plugin that you really don't need too. Checking if it the cvar is zero and returning. When you can do like below and remove unnecessary code.

I suggest you try loading up the AMX Mod X Bin Logger. It will tell you the exact line it crashed on. It a useful tool when debugging stuff too

Code:
// Random number generator public random_jam_gen() {     // if random cvars is 0 (off) end function         if(get_pcvar_num(g_iRandom))     {         g_iRatio =  random_num(get_pcvar_num(g_iRandom), get_pcvar_num(g_iRatio));     }     return PLUGIN_HANDLED; }

The Specialist 11-11-2006 20:59

Re: Random Numbers
 
well the idea was that if the random cvars is 0 , which is the default , then not to run that function at all . other wise it will generate a random number between 0 and 200 the other cvar. so first it checks to see if there using the random function cvar , if they are and the number is not 0 , then it will generate a number. i found the problem that made it crash .
Code:
// Random number generator public random_jam_gen() {  // if random cvars is 0 (off) end function    if(get_pcvar_num(g_iRandom)==0)  {   return PLUGIN_HANDLED;  }else{   // else ratio == a random number between random cvar and ratio cvar     g_iRatio =  random_num(get_pcvar_num(g_iRandom),get_pcvar_num(g_iRatio));  }  return PLUGIN_HANDLED; }

using only 1 = sign (assignemnt) made it crash , but its still not generating a random number any ideas ???:cry:

k007 11-11-2006 21:16

Re: Random Numbers
 
try this and tell me
Code:
// Random number generator public random_jam_gen() {  if(!get_pcvar_num(g_iRandom))   return PLUGIN_HANDLED;    g_iRatio =  random_float(get_pcvar_float(g_iRandom),get_pcvar_float(g_iRatio));    return PLUGIN_HANDLED; }

k007 11-11-2006 21:18

Re: Random Numbers
 
i didn't mean to bump but when u try editing a post with a small tags it will goes screwed up..anyways thats not the point if that didn't work try switchin from random_float to random_num and leaving the pcvars on floarts and then try doing the oppiste

The Specialist 11-11-2006 21:46

Re: Random Numbers
 
yes i know the psots are very weird when you try to edit it ends up a very weird code . lol . np . :) but if the cvars are both integers why try them as flaots ? here let me show my whole code LOL
Code:
#include <amxmodx> #include <amxmisc> #include <fakemeta> // global shot varaible new g_iShots[33]; new g_iRatio; new weapon; new ammo; new g_iRandom; public plugin_init() {  register_plugin("Weapon Jam","0.5","The Specialist");  register_cvar("amx_weapon_jam","1");  g_iRatio = register_cvar("amx_jam_ratio","200");  g_iRandom = register_cvar("amx_random_jam","0");  register_event("CurWeapon", "weapon_event", "be", "1=1", "3>0", "2!4", "2!6", "2!9", "2!25", "2!29");  register_forward(FM_PlayerPreThink, "weapon_jam");  register_forward(FM_UpdateClientData, "weapon_jam_update", 1); } // current weapon event public weapon_event(id) {  // if plugin is off end function    if(get_cvar_num("amx_weapon_jam")== 0)  {   return PLUGIN_HANDLED;  }else{     // if shots < ratio cvar increment varaible     if(g_iShots [id]<= get_pcvar_num(g_iRatio))   {    ++g_iShots[id];    random_jam_gen();    jam_sounds(id);    client_print(id,print_chat,"THe shots fired is %i",g_iShots[id]);   }  }  return PLUGIN_CONTINUE; } // pre-think public weapon_jam(id ) {  // if shots == cvar execute functions    if( g_iShots[id]  >= get_pcvar_num(g_iRatio))  {   // get the currently held weapon and jam that weapon only     if(get_user_weapon(id,weapon ,ammo)== weapon)   {    // show messages      set_hudmessage(255, 255, 255, -1.0, 0.33, 0, 6.0, 12.0)    show_hudmessage(id, "Weapon Jam Press Use To Clear")      // block attack button      set_pev( id, pev_button, pev(id,pev_button) & ~IN_ATTACK );      // execute clear chamber and sounds      clear_chamber(id);   }else{    return PLUGIN_HANDLED;   }  }  return FMRES_HANDLED; } // client update data public weapon_jam_update(id,sendweapons,cd_handle ) {  // if shots are = cvar execute functions    if(  g_iShots[id] >= get_pcvar_num(g_iRatio))  {   // get the currently held weapon and jam that weapon only     if(get_user_weapon(id,weapon ,ammo)== weapon)   {        // show messages      set_hudmessage(255, 255, 255, -1.0, 0.33, 0, 6.0, 12.0)    show_hudmessage(id, "Weapon Jam Press Use To Clear")      // block weapon cd id      set_cd(cd_handle, CD_ID, 0);        // clear chamber and play sounds      clear_chamber(id);    return FMRES_HANDLED;   }else{    return PLUGIN_HANDLED;   }  }  return FMRES_HANDLED; } public jam_sounds(id) {  // if shots is = cvar execute function    if( g_iShots [id]>= get_pcvar_num(g_iRatio))  {   // play sounds     emit_sound(id,CHAN_AUTO, "weapons/dryfire_rifle.wav", 1.0, ATTN_NORM, 0, PITCH_NORM);  } } public clear_chamber(id) {  // if user presses use button or user is bot end functions    if(pev(id,pev_button) &  IN_USE   || is_user_bot(id)==1)  {     //re-set varaible to 0     g_iShots[id] = 0;     // play sounds     emit_sound(id,CHAN_AUTO,"weapons/m4a1_boltpull.wav",1.0,ATTN_NORM,0,PITCH_NORM);     // show messages     set_hudmessage(255, 255, 255, -1.0, 0.3, 0, 6.0, 12.0);   show_hudmessage(id, "Chamber Clear");     // and stop functions     return PLUGIN_HANDLED;  }  return PLUGIN_HANDLED; } // Random number generator public random_jam_gen() {  // if random cvars is 0 (off) end function    if(get_pcvar_num(g_iRandom)==0)  {   return PLUGIN_HANDLED;  }else{   // else ratio == a random number between random cvar and ratio cvar     g_iRatio ==  random_num(get_pcvar_num(g_iRandom),get_pcvar_num(g_iRatio));  }  return PLUGIN_HANDLED; }
thanks again guys :up:

jim_yang 11-11-2006 22:35

Re: Random Numbers
 
g_iRatio == random_num(get_pcvar_num(g_iRandom),get_pcvar _num(g_iRatio));

to

g_iRatio = random_num(get_pcvar_num(g_iRandom),get_pcvar _num(g_iRatio));

jim_yang 11-11-2006 22:38

Re: Random Numbers
 
also when you check a value if equals zero. you don't need to "if(a==0)" just"if(!a)"

The Specialist 11-11-2006 23:50

Re: Random Numbers
 
no that was the first way i did it . with 2 == instead of 1 . and it crashed my server . then i took it out and 2 worked but didnt generate a radnom number :cry:

Brad 11-12-2006 00:13

Re: Random Numbers
 
It's stupid that you're not following teame06's advice. While it doesn't make too much of a performance difference, logic would dictate you do it the way teame06 advised. If nothing else, it looks a hell of a lot cleaner his way.


All times are GMT -4. The time now is 06:49.

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