Search Unity

Question [SOLVED] Is there a function or method to ignore elements within an array?

Discussion in 'Scripting' started by knardhocks, Feb 24, 2024.

  1. knardhocks

    knardhocks

    Joined:
    Jan 10, 2024
    Posts:
    3
    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.
     

    Attached Files:

    Last edited: Feb 24, 2024
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,812
    The continue keyword can be used to move onto the next enumeration of a loop: https://learn.microsoft.com/en-us/d...ements/jump-statements#the-continue-statement

    That said:
    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.
     
    Nad_B likes this.
  3. knardhocks

    knardhocks

    Joined:
    Jan 10, 2024
    Posts:
    3
    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.

    Going to try Linq.Take()
    https://dotnettutorials.net/lesson/take-operator-in-linq/
     
    Last edited: Feb 25, 2024
  4. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    712
    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) :

    Code (CSharp):
    1. // Enumerate over the first 7 elements in your array
    2. // Just make sure your array has at least 7 elements
    3. // or else you'll get an IndexOutOfBounds exception
    4. for (var i = 0; i < 7; i++)
    5. {
    6.     var item = yourArray[i];
    7.  
    8.     // work with the item...
    9. }