Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Using int arrays faster than multiple ints?

Discussion in 'Scripting' started by Nsingh13, Mar 5, 2019.

  1. Nsingh13

    Nsingh13

    Joined:
    Dec 17, 2014
    Posts:
    18
    So I'm making a cooking game and I'm trying to store how much you have in stock for each item/ingredient. Is it faster performance-wise to use an array of integers with each place in the array being an item and the value in that place being the amount you have of that item.. or is it faster to create multiple integers for each item instead of an array. Code organization wise the first is a whole lot better but I'm not really sure performance-wise.

    Ex approach 1:

    Code (CSharp):
    1. public int[] itemsInStock = new int[40];
    2.  
    3. // itemsInStock[10] might store how many carrots you have
    4. // itemsInStock[12] might store how much milk you have
    Ex approach 2:

    Code (CSharp):
    1. public int carrotsInStock = 0;
    2. public int milkInStock = 0;
    3. public int almondsInStock = 0;
    4. // and so on...
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    736
    With 40 integers it isn't going to make a blind bit of difference.

    Although, for your game organization you may want to use scriptable objects or a class for each item. You will want some way of identifying each ingredient rather than accessing an index in an array or individual variable.
     
    Ryiah and Joe-Censored like this.
  3. Nsingh13

    Nsingh13

    Joined:
    Dec 17, 2014
    Posts:
    18
    Do you know of any tutorials on how to do this? I've never really worked with custom classes. Also, is the accessing an index method bad for performance?
     
  4. Kobaltic1

    Kobaltic1

    Joined:
    Jan 22, 2015
    Posts:
    142
  5. Nsingh13

    Nsingh13

    Joined:
    Dec 17, 2014
    Posts:
    18
  6. Nsingh13

    Nsingh13

    Joined:
    Dec 17, 2014
    Posts:
    18