Array question.

Started by
8 comments, last by SeiryuEnder 12 years, 2 months ago
if i have an array called int level[99] and i use the following code ++level[99] 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]?
Advertisement
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.
Is there a way to increment to the next data element in an array when the player levels up?
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.
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?
No, you're still trying to use the 100th element of a 99 long array.
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]
Of course you probably want a bounds check so:

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

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]


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)

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

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 this to visualize your level curve.


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 );
}

...
}


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!

This topic is closed to new replies.

Advertisement