• Create Account

Array question.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

9 replies to this topic

#1jblevins1991  Members

99
Like
0Likes
Like

Posted 17 February 2012 - 05:07 PM

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]?

#2Brother Bob  Moderators

10102
Like
0Likes
Like

Posted 17 February 2012 - 05:16 PM

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.

#3jblevins1991  Members

99
Like
0Likes
Like

Posted 17 February 2012 - 05:20 PM

Is there a way to increment to the next data element in an array when the player levels up?

#4Brother Bob  Moderators

10102
Like
0Likes
Like

Posted 17 February 2012 - 05:27 PM

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.

#5jblevins1991  Members

99
Like
0Likes
Like

Posted 17 February 2012 - 05:35 PM

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?

#6SiCrane  Moderators

11522
Like
0Likes
Like

Posted 17 February 2012 - 05:39 PM

No, you're still trying to use the 100th element of a 99 long array.

#7Brother Bob  Moderators

10102
Like
0Likes
Like

Posted 17 February 2012 - 05:40 PM

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:

if(exp>=exptilnextlevel[level])

{

++level;

}



#8walterk  Members

102
Like
1Likes
Like

Posted 17 February 2012 - 06:02 PM

Of course you probably want a bounds check so:

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

#9BeerNutts  Members

4287
Like
0Likes
Like

Posted 17 February 2012 - 06:06 PM

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:

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


To slam home the point, you might should check you're not outside the bounds of the array 1st
// 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;
}
}


(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)

#10SeiryuEnder  Members

199
Like
0Likes
Like

Posted 18 February 2012 - 07:09 AM

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!

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.