Raised This Month: $ Target: $400
 0% 

Optimize/fix my code


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 08-15-2008 , 14:53   Re: Optimize/fix my code
Reply With Quote #8

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);      }        // ... }
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
 



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 23:52.


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