AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Optimize/fix my code (https://forums.alliedmods.net/showthread.php?t=75906)

[X]-RayCat 08-14-2008 18:36

Optimize/fix my code
 
Code:
#include <amxmodx> #include <fakemeta>   #define PLUGIN "HNS Buymenu" #define AUTHOR "X-RayCat" #define VERSION "1.0"   #define OFFSET_MONEY 115 #define cs_get_user_money(%1) get_pdata_int(%1, OFFSET_MONEY)   new pcvars[5]; new Messages[5][] ={      "You bought silent footsteps! (Noone can hear your footsteps)",      "You bought health! (150 health)",      "You bought sunglasses (No flash blinding)!",      "You bought steahlt! (50% invisible)",      "You bought armor (150 armor)!" }   new Names[6][] ={      "",      "Silent Footsteps",      "150 health",      "Sunglasses (No flash)",      "Steahlt-Suit",      "150 Armor" }   new bool:has_noflash[33], has_health[33], has_footsteps[33], has_steahlt[33], has_armor[33]; new g_nMsgScreenFade;   public plugin_init() {      register_plugin(PLUGIN, VERSION, AUTHOR)        pcvars[0] = register_cvar("hns_footsteps_price", "2000");      pcvars[1] = register_cvar("hns_health_price", "12000");      pcvars[2] = register_cvar("hns_noflash_price", "8000");      pcvars[3] = register_cvar("hns_stealth_price", "4000");      pcvars[4] = register_cvar("hns_armor_price", "6000");        register_clcmd("say /hnsshop","HnsBuyMenu");      register_clcmd("say_team /hnsshop","HnsBuyMenu");        register_event ("ScreenFade","FlashedEvent","be","4=255","5=255","6=255","7>199");      register_event("ResetHUD", "Event_ResetHud", "be");      register_event("DeathMsg", "hook_death", "a", "1>0");      g_nMsgScreenFade = get_user_msgid("ScreenFade"); }   public hook_death(id) {      new victim = read_data(2);        has_armor[victim] = false;      has_footsteps[victim] = false;      has_health[victim] = false;      has_noflash[victim] = false;      has_steahlt[victim] = false;        set_pev(id, pev_flTimeStepSound, 400); } public client_disconnect(id) {      has_armor[id] = false;      has_footsteps[id] = false;      has_health[id] = false;      has_noflash[id] = false;      has_steahlt[id] = false; }   public ResetHUD(id) {      set_task(0.1, "give_ability", id); }   public HnsBuyMenu(id) {      new menu = menu_create("\rHide 'N Seek Shop:", "menu_handler");        new i = 1, nr[3]      new Form[50]      for ( i = 1; i < 9; i++)      {          format(nr, 2,"%i", i)          format(Form, 49, "%s: ($%i)", Names[i], get_pcvar_num(pcvars[i]))          menu_additem(menu, Form, nr, 0);      }        menu_setprop(menu, MPROP_EXIT, MEXIT_ALL);      menu_display(id, menu, 0);      return 0; }   public menu_handler(id, menu, item) {      if (item == MENU_EXIT)      {           client_print(id, print_chat, "[HNS-Shop] You have exited from the shopmenu!")           menu_destroy(menu);           return 1;      }        new szCommand[6] , szName[64];      new access , callback;        menu_item_getinfo(menu, item, access, szCommand, 5, szName, 63, callback);        new i = str_to_num(szCommand);      new money = cs_get_user_money(id);      new cost = get_pcvar_num(pcvars[i]);        if(money < cost)      {          client_print(id, print_chat, "[HNS-Shop] You don't have enough money ($%i Needed).", cost);          return 1;      }        cs_set_user_money(id, money-cost, 1);      client_print(id, print_chat, "[HNS-Shop] %s", Messages[i]);        switch(i)      {          case 1:{               //Default: 400 / Silent: 999               set_pev(id, pev_flTimeStepSound, 999);               has_footsteps[id] = true;                   menu_display(id, menu, 0);               return 1;          }            case 2:{              set_pev(id, pev_health, 150.0);              has_health[id] = true;                  menu_display(id, menu, 0);          }          case 3:{             has_noflash[id] = true;             menu_display(id, menu, 0);                 return 1;          }          case 4:{            fm_set_user_rendering(id, kRenderFxGlowShell, 0, 0, 0, kRenderTransAlpha, floatround(255.0 * 0.50)); //50%            has_steahlt[id] = true;                menu_display(id, menu, 0);            return 1;         }         case 5:{           set_pev(id, pev_armorvalue, 150.0);           menu_display(id, menu, 0);           return 1;         }      }      menu_destroy(menu)      return 1; }   public give_ability(id) {      if(has_armor[id]) {        set_pev(id, pev_armorvalue, 150.0);      } else if(has_footsteps[id]) {        set_pev(id, pev_flTimeStepSound, 999);      }  else if(has_health[id]) {       set_pev(id, pev_health, 150.0)      } else if(has_steahlt[id]) {          fm_set_user_rendering(id, kRenderFxGlowShell, 0, 0, 0,   kRenderTransAlpha, floatround(255.0 * 0.50));      } }   public FlashedEvent( id ) {      if(!has_noflash[id])          return 0;        message_begin( MSG_ONE, g_nMsgScreenFade, {0,0,0}, id );      write_short( read_data( 1 ) ); // Duration      write_short( read_data( 2 ) ); // Hold time      write_short( read_data( 3 ) );      write_byte ( 0 );   //Red      write_byte ( 0 );   // Green      write_byte ( 0 ); // Blue      write_byte ( 0 ); // Alpha      message_end();        return 1; }   stock cs_set_user_money(index, money, flash = 1)   {      set_pdata_int(index, OFFSET_MONEY, money);        message_begin(MSG_ONE, get_user_msgid("Money"), _, index);      write_long(money);      write_byte(flash ? 1 : 0);      message_end(); } stock fm_set_user_rendering(entity, fx = kRenderFxNone, r = 255, g = 255, b = 255, render = kRenderNormal, amount = 16) {      new Float:RenderColor[3];      RenderColor[0] = float(r);      RenderColor[1] = float(g);      RenderColor[2] = float(b);        set_pev(entity, pev_renderfx, fx);      set_pev(entity, pev_rendercolor, RenderColor);      set_pev(entity, pev_rendermode, render);      set_pev(entity, pev_renderamt, float(amount));        return 1; }

The menu doesnt open it say something like "Index out of bounds". :)

danielkza 08-14-2008 18:40

Re: Optimize/fix my code
 
I think it's not very 'polite' to just throw your code at us and expected us to do all the dirty work for you.

And you need to specify where and how you're getting errors. Enable debug mode in plugins.ini and post the specific error line.

Iwon 08-14-2008 19:39

Re: Optimize/fix my code
 
Hmm, ill check this piece of code out tormorrow.

fxfighter 08-15-2008 06:47

Re: Optimize/fix my code
 
Quote:

Originally Posted by [X]-RayCat (Post 669695)
Code:
new Names[6][] ={
"",
"Silent Footsteps",
"150 health",
"Sunglasses (No flash)",
"Steahlt-Suit",
"150 Armor" }

for ( i = 1; i < 9; i++)
{
format(nr, 2,"%i", i)
format(Form, 49, "%s: ($%i)", Names[i], get_pcvar_num(pcvars[i])) menu_additem(menu, Form, nr, 0);
}



The menu doesnt open it say something like "Index out of bounds". :)

The names array is to small use sizeof or make the number nine smaller

[X]-RayCat 08-15-2008 07:45

Re: Optimize/fix my code
 

Quote:

Originally Posted by danielkza (Post 669697)
I think it's not very 'polite' to just throw your code at us and expected us to do all the dirty work for you.

Quote:

Originally Posted by danielkza (Post 669697)

And you need to specify where and how you're getting errors. Enable debug mode in plugins.ini and post the specific error line.

i said:
Quote:

Originally Posted by [X
-RayCat]
The menu doesnt open it say something like "Index out of bounds". :)

You know the public what is called when the command is used. And i said "index out of bounds". That can't be more obvious?

xPaw 08-15-2008 13:30

Re: Optimize/fix my code
 
stealing ?
http://forums.alliedmods.net/showthread.php?t=75468

[X]-RayCat 08-15-2008 13:40

Re: Optimize/fix my code
 
Quote:

Originally Posted by xPaw (Post 670044)

May i point out that, i were the first one working on this. I even got it finished, but the menu didnt work.

http://forums.alliedmods.net/showthread.php?t=75027

Exolent[jNr] 08-15-2008 14:53

Re: Optimize/fix my code
 
Code:
// ... new Names[6][] ={      "",      "Silent Footsteps",      "150 health",      "Sunglasses (No flash)",      "Steahlt-Suit",      "150 Armor" } // ... public HnsBuyMenu(id) {      // ...      for ( i = 1; i < 9; i++)      {          format(nr, 2,"%i", i)          format(Form, 49, "%s: ($%i)", Names[i], get_pcvar_num(pcvars[i]))          menu_additem(menu, Form, nr, 0);      }        // ... }

Notice how the loop goes through 9, when you only have 6.
You could do:

Code:
public HnsBuyMenu(id) {      // ...      for ( i = 1; i < sizeof(Names); i++)      {          format(nr, 2,"%i", i)          format(Form, 49, "%s: ($%i)", Names[i], get_pcvar_num(pcvars[i]))          menu_additem(menu, Form, nr, 0);      }        // ... }

That way, you won't have to worry about changing the number every time you add an item, and the loop will still be fixed.
But, please notice that you are also looping through the pcvars array, so make sure you check the size of that one if the Names and the pcvars have different sizes.

Code:
public HnsBuyMenu(id) {      // ...      // get the smallest size so we dont do out of bounds      new total = min(sizeof(Names), sizeof(pcvars));      for ( i = 1; i < total; i++)      {          format(nr, 2,"%i", i)          format(Form, 49, "%s: ($%i)", Names[i], get_pcvar_num(pcvars[i]))          menu_additem(menu, Form, nr, 0);      }        // ... }


All times are GMT -4. The time now is 03:11.

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