# Array question.

This topic is 2160 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

if i have an array called [b]int level[99][/b] and i use the following code [b]++level[99] [/b]does that increment the array's element.

Example say we are at level 3 in our game meaning the level array would be at array[2] for our exp til next level, if i were to level up and use ++level would it be at level[[3]?

##### Share on other sites
If you have an array of 99 element then level[99] refers to the 100th element, so referencing it is not even allowed. In short, you cannot increment the value of the 100:th element of an array containing only 99 elements.

##### Share on other sites
Is there a way to increment to the next data element in an array when the player levels up?

##### Share on other sites
What do you mean? An array contains a sequence of elements, but there is no such thing as a current or a next element of an array. From your first post, why are you using an array and not just use a single value to keep track of the level? You do you need an array to keep track of which level you are at when a single value seems to be enough, unless you can be at multiple levels at the same time.

##### Share on other sites
sorry, i got confused. the array holds the exp amount needed to level up (see below)

int exp;
int exptilnextlevel[99];

if(exp>=exptilnextlevel[99])
{
++level;
}

would this work?

##### Share on other sites
No, you're still trying to use the 100th element of a 99 long array.

##### Share on other sites
No. You have an array with 99 element, and the index 99 refers to the 100th element. It is outside the array, so it's not a valid array element.

But ignoring that; yes, the logic is otherwise OK, as long as you index the array properly. You probably want this instead though:
[source]
if(exp>=exptilnextlevel[level])
{
++level;
}
[/source]

##### Share on other sites
Of course you probably want a bounds check so:

[code]if(level < 99 && exp >= exptilnextlevel[level])
{
++level;
}[/code]

##### Share on other sites
[quote name='Brother Bob' timestamp='1329522031' post='4914054']
No. You have an array with 99 element, and the index 99 refers to the 100th element. It is outside the array, so it's not a valid array element.

But ignoring that; yes, the logic is otherwise OK, as long as you index the array properly. You probably want this instead though:
[source]
if(exp>=exptilnextlevel[level])
{
++level;
}
[/source]
[/quote]

To slam home the point, you might should check you're not outside the bounds of the array 1st
[source]
// if you're below level 98, then you're good to go
// if you're at level 98, the array only goes up to exptilnextlevel[98], so don't increase the level any more
if (level < 98) {
if(exp>=exptilnextlevel[level])
{
++level;
}
}
[/source]

(Edit Beaten)

##### Share on other sites
Counting in array references starts from 0, not 1 (as others have said).
That means that level 1 will be array[0], level 80 will be array[79], and level 100 will be array[99].
This is called zero-based indexing.

If I may suggest, you may want to calculate your player levels using a math function.
It seems to me that your players may be able to level up an arbitrary number of times, making it
impossible to pre-define the experience requirements for all levels. (You can define a specific
subset of levels, but that's out of the scope of this discussion).

You can use a site like [url="http://go.hrw.com/math/midma/gradecontent/manipulatives/GraphCalc/graphCalc.html"]this[/url] to visualize your level curve.

[code]
int CalcExpToLevelUp( int level )
{
// y = x^2
// Where y is your xp to level and x is your current level
return level*level;
}

int Level = 0;
int Exp = 0;
int ExpToLevel = 0;

bool Player_Update()
{
...

// Can we level up?
if( Exp >= ExpToLevel )
{
Exp = 0;
++Level;
ExpToLevel = CalcExpToLevel( Level );
}

...
}
[/code]

This may not necessarily be the best or fastest method, but it is flexible.
Keep in mind this is pseudo-code, and not compiled or tested. It is meant
only as a conceptual reference.

I hope this all makes sense, it's been a long night and I'm pretty tired.
I'm sure if there's something I was fuzzy on someone will correct me.
Best of luck!