Search Unity

[SOLVED] Accessing large multidimensional arrays?

Discussion in 'Scripting' started by xolotlll, Jul 27, 2017.

  1. xolotlll

    xolotlll

    Joined:
    Jul 16, 2017
    Posts:
    18
    Hey all,

    I've created a rather large multidimensional array as follows:
    Code (CSharp):
    1.  public static int [,,,,] l = new int [40, 40, 40, 40, 40] { };
    How do I access specific numbers in the array? Obviously if this were a one-dimensional array, I'd just do something like this:
    Code (CSharp):
    1.  l[2] = 4;
    But how would I specify that I want to access or modify the second number in the third dimension or fourth dimension of the array for instance?

    Is there an easier way to organise so many different integers? It's for a branching dialogue system I'm making. The first number would be the index of the node in that particular conversation. The second number would be the behaviour of that node - if it's 0, pressing space just continues to the next node numerically. But if it's 1, it presents a choice to the player. The other three numbers represent the destination node of each option - for instance, if the third number is set to 4, it means choosing option 1 will go to node 4 in that conversation. If the fourth number is set to 10, it means choosing option 2 will go to node 10 in the conversation - and so on. I hope this makes sense? I don't know how necessary it is for my question anyway.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
  3. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    You use commas the same way you declare it:

    Code (csharp):
    1. array[2,3,5,7,12] = 6;
    But I'm going to go out on a limb and say if you couldn't figure out how to google the answer to this question, then you might want to hold off on trying to implement a dialogue system until you've done a few more tutorials. ;) I would not use a multidimensional array for dialogue; you will probably want to use a node graph sort of structure, or use one of the plugins available out there on the asset store and beyond.
     
    Kiwasi likes this.
  4. xolotlll

    xolotlll

    Joined:
    Jul 16, 2017
    Posts:
    18
    Thank you for your input. It does seem too unwieldy. I'm trying to use lists instead, but keep getting an error.

    Here's what I have so far:

    Code (CSharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. [Serializable]
    8. public class nodeInfo  : MonoBehaviour //properties for each dialogue node
    9. {
    10.     public static int nod; //node index number
    11.     public static int b; //type of behaviour for the node: 0 is go ahead to next node, 1 is show some branching choices
    12.     public static int o1; //the destination of the first option
    13.     public static int o2; //the destination of the second option
    14.     public static int o3; //the destination of the third option
    15.     public static int o4; //the destination of the fourth option
    16. }
    17.  
    18. public class dialogueIndex : MonoBehaviour
    19. {
    20.         public static List<nodeInfo> node = new List<nodeInfo> ();
    21.  
    22. }
    23.  
    So later on in dialogueIndex, I try to test out the list with this:
    Code (csharp):
    1. node [4].b = 1;
    I'm trying to set the 'behaviour' of 'node 1' to '1' (branching dialogue), but it comes up with an error and doesn't allow me to access the list:
    Absolutely a fair point, but I'm learning quite a bit by throwing myself in the deep end.
     
    Last edited: Jul 27, 2017
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    https://www.assetstore.unity3d.com/en/#!/content/3124 It's an older asset, so I don't know if it will work. But this is an option.

    As far as what that error is. Chances are you don't want static variables. The fact that you are making almost everything static concerns me...I do suggest a little research on what static actually does. :)