Sign in to follow this  
jblevins1991

Array question.

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this