AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Index out of bounds (https://forums.alliedmods.net/showthread.php?t=223600)

Podarok 08-15-2013 09:53

Index out of bounds
 
So I have got a menu and prices for it and in handler i remove amout of money required to buy item, but console is showing me an error : Index is out of bounds, how can I cope with it ?

Code:
public shopmenu_handler( id, menu, item )  {     if( item == MENU_EXIT )  {         menu_destroy( menu );         return PLUGIN_HANDLED;     }     new money = cs_get_user_money(id)     cs_set_user_money(id, money-str_to_num(g_Cost[item]))            // <----- This Line

Code:
    static const g_Cost[][] = {     { "10000" },        { "13000" },        { "16000" },        { "7500" },         { "7000" },         { "12000" },        { "7500" },         { "15000" },        { "16000" } }

~Ice*shOt 08-15-2013 09:59

Re: Index out of bounds
 
how much items are?

YamiKaitou 08-15-2013 10:00

Re: Index out of bounds
 
What is the value of item? IIRC, the New Menu style does not use a Zero based item scale, so you would need to cope with that.

Backstabnoob 08-15-2013 10:08

Re: Index out of bounds
 
Yami is right, use str_to_num( g_Cost[ item - 1 ] ).

Podarok 08-15-2013 10:34

Re: Index out of bounds
 
Quote:

Originally Posted by YamiKaitou (Post 2013742)
What is the value of item? IIRC, the New Menu style does not use a Zero based item scale, so you would need to cope with that.

If I press 0 - aka Exit, it shows me this error...

Quote:

Originally Posted by Backstabnoob (Post 2013753)
Yami is right, use str_to_num( g_Cost[ item - 1 ] ).

Also this is not right.

guipatinador 08-15-2013 10:37

Re: Index out of bounds
 
Quote:

Originally Posted by Backstabnoob (Post 2013753)
Yami is right, use str_to_num( g_Cost[ item - 1 ] ).

No.
If I press key one, item = 0.
g_Cost[ 0 ] = 10000

Podarok 08-15-2013 10:41

Re: Index out of bounds
 
Quote:

Originally Posted by guipatinador (Post 2013782)
No.
If I press key one, item = 0.
g_Cost[ 0 ] = 10000

Yes thats what I am talking about. This error occurs when u press 0. Aka Exit!

YamiKaitou 08-15-2013 10:43

Re: Index out of bounds
 
Quote:

Originally Posted by Podarok (Post 2013789)
Yes thats what I am talking about. This error occurs when u press 0. Aka Exit!

No it doesn't. You have caught the MENU_EXIT. If you believe it is the other way, then DEBUG it

Podarok 08-15-2013 14:46

Re: Index out of bounds
 
Quote:

Originally Posted by YamiKaitou (Post 2013792)
No it doesn't. You have caught the MENU_EXIT. If you believe it is the other way, then DEBUG it

I opened 2 windows, CS and My Remote Connection to the server i pressed every key on the menu and only when I pressed 0 aka Exit this error occured.. I was also wondered why because as u said i have caught the MENU_EXIT ? Maybe I should do it like this : if(item == -1)... ?

YamiKaitou 08-15-2013 14:54

Re: Index out of bounds
 
This is the only way to know what the true value of item is

Code:
public shopmenu_handler( id, menu, item )  { server_print("item == %d", item);     if( item == MENU_EXIT )  {         menu_destroy( menu );         return PLUGIN_HANDLED;     } server_print("item was not MENU_EXIT");     new money = cs_get_user_money(id)     cs_set_user_money(id, money-str_to_num(g_Cost[item]))            // <----- This Line


All times are GMT -4. The time now is 15:55.

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