- Home /
whats this Argument Out Of Range error when navigating between 0 and max value?
Im making a combat menu like this:
<-- Skills: skill name -->
<-- Magics: magic name -->
<-- Items : item name -->
skill name's variable is called skillbagCursor
magic name's variable is called magicbagCursor
item name's variable is called itembagCursor
pressing left or right arrow should change the value in the targetted horizontal row. that works, but when i go left or right i get an error, i used a Debug.Log(Master.itembag.Count); to see the size of the list and it always say 2 or more, it never says 1 or 0 though I add 3 total items at 3 seperate times(should say 1, then 2, then 3).. im stumped. Would anyone like to help me figure this out?
import System.Collections.Generic;
var firstPlay : boolean = true;
var itembagCursor : int = 0;
static var itembagCursorMax : int = 0;
var skillbagCursor : int = 0;
static var skillbagCursorMax : int = 0;
var magicbagCursor : int = 0;
static var magicbagCursorMax : int = 0;
static var selectedSkill : String;
static var selectedMagic : String;
static var selectedItem : String;
static var activeCursor : String = "skillbag";
function Start(){
if(firstPlay){
Master.itembag.Add("Empty"); //add item to inventory
Master.skillbag.Add("Rush Cut");
Master.magicbag.Add("Fire");
firstPlay = false;//save this value on saving!!!***~~~
}
}
function Update(){
itembagCursor = Mathf.Clamp(itembagCursor, 0, itembagCursorMax);
itembagCursorMax = Master.itembag.Count;
skillbagCursorMax = Master.skillbag.Count;
magicbagCursorMax = Master.magicbag.Count;
selectedItem = Master.itembag[itembagCursor];
selectedMagic = Master.magicbag[magicbagCursor];
selectedSkill = Master.skillbag[skillbagCursor];
Interface.messageWindow = selectedItem;
/* SET THE ACTIVE CURSOR */
if(Input.GetKeyDown(KeyCode.UpArrow) == true)
{
switch(activeCursor)
{
case "skillbag":
activeCursor = "itembag";
break;
case "magicbag":
activeCursor = "skillbag";
break;
case "itembag":
activeCursor = "magicbag";
break;
}
}
else if(Input.GetKeyDown(KeyCode.DownArrow) == true)
{
switch(activeCursor)
{
case "skillbag":
activeCursor = "magicbag";
break;
case "magicbag":
activeCursor = "itembag";
break;
case "itembag":
activeCursor = "skillbag";
break;
}
}
/* ENABLE ACTIVE CURSOR TO NAVIGATE ITEMS <- & -> */
if(Input.GetKeyDown(KeyCode.LeftArrow) == true)
{
switch(itembagCursor)
{
case itembagCursor > 0:
itembagCursor = itembagCursor - 1;
break;
case itembagCursor == 0:
itembagCursor = itembagCursorMax;
break;
}
}
if(Input.GetKeyDown(KeyCode.RightArrow) == true)
{
switch(itembagCursor < itembagCursorMax)
{
case true:
itembagCursor = itembagCursor + 1;
break;
case false:
itembagCursor = 0;
break;
}
}
if(Input.GetKeyDown(KeyCode.LeftArrow) == true)
{
switch(magicbagCursor)
{
case magicbagCursor > 0:
magicbagCursor = magicbagCursor - 1;
break;
case itembagCursor == 0:
magicbagCursor = magicbagCursorMax;
break;
}
}
if(Input.GetKeyDown(KeyCode.RightArrow) == true)
{
switch(magicbagCursor)
{
case magicbagCursor < magicbagCursorMax:
magicbagCursor = magicbagCursor + 1;
break;
case magicbagCursor == magicbagCursorMax:
magicbagCursor = 0;
break;
}
}
if(Input.GetKeyDown(KeyCode.LeftArrow) == true)
{
switch(skillbagCursor)
{
case skillbagCursor > 0:
skillbagCursor = skillbagCursor - 1;
break;
case itembagCursor == 0:
skillbagCursor = skillbagCursorMax;
break;
}
}
if(Input.GetKeyDown(KeyCode.RightArrow) == true)
{
switch(skillbagCursor)
{
case skillbagCursor < skillbagCursorMax:
skillbagCursor = skillbagCursor + 1;
break;
case skillbagCursor == skillbagCursorMax:
skillbagCursor = 0;
break;
}
}
//USE THE ITEMS WHEN ENTER PRESSED IN COMBAT MENU//
if(Input.GetKeyDown(KeyCode.Return) && activeCursor == "itembag" && selectedItem == "Potion" && Master.potion > 0){
Statistics.playerCurHp = Statistics.playerCurHp + 100;
Interface.messageWindow = "You used a 'Potion'.";
Master.potion = Master.potion - 1;
}
if(Master.potion == 0 && Master.itembag.Count > 1){
Master.itembag.Remove("Potion");
}
if(Master.potion == 0 && Master.itembag.Count == 1){
Master.itembag.Add("Empty");
Master.itembag.Remove("Potion");
}
if(Input.GetKeyDown(KeyCode.Return) && activeCursor == "itembag" && selectedItem == "Ether" && Master.ether > 0){
Statistics.playerCurMp = Statistics.playerCurMp + 50;
Interface.messageWindow = "You used an 'Ether'.";
Master.ether = Master.ether - 1;
}
if(Master.ether == 0 && Master.itembag.Count > 1){
Master.itembag.Remove("Ether");
}
if(Master.ether == 0 && Master.itembag.Count == 1){
Master.itembag.Add("Empty");
Master.itembag.Remove("Ether");
}
}
by the way, the error i get is here at thie comment message/selectedItem
function Update(){
itembagCursor = $$anonymous$$athf.Clamp(itembagCursor, 0, itembagCursor$$anonymous$$ax);
itembagCursor$$anonymous$$ax = $$anonymous$$aster.itembag.Count;
skillbagCursor$$anonymous$$ax = $$anonymous$$aster.skillbag.Count;
magicbagCursor$$anonymous$$ax = $$anonymous$$aster.magicbag.Count;
selectedItem = $$anonymous$$aster.itembag[itembagCursor]; //ERROR HERE
selected$$anonymous$$agic = $$anonymous$$aster.magicbag[magicbagCursor];
selectedSkill = $$anonymous$$aster.skillbag[skillbagCursor];
Interface.messageWindow = selectedItem;
Answer by Dave-Carlile · Jan 24, 2013 at 12:53 AM
It looks like you're setting the max itembagCursor to the count of items. Array's are relative to 0, so valid indexes for Master.itembag are 0 to Master.itembag.Count - 1.
Try this:
itembagCursorMax = Master.itembag.Count - 1;
Also, shouldn't you move that line above the clamp?
It worked like a charm i cant believe i missed that! I find i miss a lot of behind the scenes stuff... thanks a lot.
also you probably shouldnt use clamp. Clamp is for floats really.
if you want to make sure its equal to 0 1 or 2 use the modulo operator and just keep adding. $$anonymous$$odulo is the remainder left after deviding. So
number % 3 = 0
number % 3 = 1
number % 3 = 2
there are no other options. A number divided by 3 can either have a remainder of 1, a remainder of 2 or if there is 3 more it goes into it one more time gonig into it perfectly and there is no remainder.
so for yours for example
if there are 4 choices, or more generally Item$$anonymous$$axSize number of choices.
the choice your on is
(Cursor % Cursor$$anonymous$$ax)
assu$$anonymous$$g the cursor starts at 0
and cursormax is 4 for example
4 goes into 0, 0 times and the remainder is 0
cursor++;
4 goes into 1, 0 times and the remainder is 1
cursor++
4 goes into 2, 0 times and the remainder is 2.
corusor++
4 goes into 3, 0 times and the remainder is 3.
cursor++
4 goes into 4, 1 time and the remainder is 0.
4 goes into 5, 1 time and the remainder is 1.
as you can see we have created an infinite loop whereby we only arrive at one of 4 points and by setting it to cursor$$anonymous$$ax we can change the value later to account for new items or subtract to remove them.
Cool. Thats understandable. Ill change the Clamp into a $$anonymous$$odulo as soon as i fix my current issue... same error as above this time with items being used. Should i make a new question for the same script, same issue, but at different lines?
EDIT: On second thought, i ran a check with printing my inventory size, and my amount of items dont -- inventory size. I think i found a great lead to solve it myself.
thank you for all of your help @sparkzbarca
inventory size and accessing an element of an inventory arent the same thing because of the 0 difference.
an inventory with 1 item has a size of 1
that item is not however inventory[size];
or inventory[1]
its inventory[size -1]
or inventory[0]
thats because the FIRST item is stored in the ZERO slot. likewise the last item is stored in the last-1 slot and in general the iterator to access any element is its number -1;
you need that because you cant say a list with 0 elements is size 0 and also say a list with one elements is size 0. It has to be size 1
however starting from 0 with arrays is needed because an array of size 0 is techinically size 1
an empty array contains 1 item. its a special item that denotes the end of the list. strings have this as well a character that denotes the end of the string. That allows the computer to know when the string or list ends and know its size and when to stop iterating.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
List of type Button has size set to zero at runtime 0 Answers
Problem declaring List variable in JS 2 Answers
Java Script command list 3 Answers
FindIndex in a List 1 Answer