Project: Fantasy RPG - Character Creation - Stat System
I have an array: int[ ] that stores additional stats in each element based on a player class selection.
These points are added to base stats that derive from a race selection.
The class selection stats are added together for a total sum.
I have a separate int of 50 called bonus points.
The sum of the class selection stats are subtracted from 50 for a final total that players can then distribute to their stat tree.
The problem I am running into is that there are some elements in the array I would like for this sum calculation to ignore, and only add together the basic stats (elements 0 through 6) and ignore the resistance stats (elements 7 through 12).
Short of splitting the stats into two arrays and coding around this, is there any way to ignore these elements in the array?
My current solution is a bit of an array hack. (See attached images)
I lock the array to 14 elements, then add 7 elements at the start of the array to “push” the elements I don’t want calculated out, and it retains the ones I want kept in - but this just feels like a dirty fix that could produce bugs later - and because this is a character creator, I need to be sure to pass the correct information forward before the character is stored into save data.
Then just write a for-loop that only enumerates the first 7 elements? I feel like you’ve missed the most obvious solution here.
It also might be worthwhile using a more meaningful data type than just an integer. A wrapper class that can express additional information would probably be more useful.
Well yes, the obvious solution is the one I was seeking lol - I know what I wanna do, just didn’t have the syntax for it. “Enumerate” was the key word in this, I think.
I’m still really new to coding, I only started a week or so ago.
Googling “enumerate elements in an array” got me some results - so this was helpful. Thanks.
I’ve considered converting to Dictionary, or List from Array, but I need to study more on those.
Well you could use .Take(), but in all cases you’ll enumerate over the result it gives you, so why not enumerate over the first n elements of your array directly, and avoid one useless operation (.Take) :
// Enumerate over the first 7 elements in your array
// Just make sure your array has at least 7 elements
// or else you'll get an IndexOutOfBounds exception
for (var i = 0; i < 7; i++)
{
var item = yourArray[i];
// work with the item...
}