Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How can I access data from an array from a specific class?

Discussion in 'Scripting' started by jleven22, Sep 7, 2023.

  1. jleven22

    jleven22

    Joined:
    Mar 26, 2019
    Posts:
    397
    Tried so hard to work this one out on my own, but I'm in over my head. Officially need help.

    So basically I need to access the list below from another script, then check what the int is for
    rangedDataPrefsID
    . I already struggle with for loops, but I think if I can get the access of this specific class figured out, I can probably resolve the rest.

    Here's the script I'm trying to access
    CSVRangedData[] rangedData
    from:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5.  
    6. public class CSVReaderRanged : MonoBehaviour
    7. {
    8.  
    9.     public TextAsset textAssetData;
    10.  
    11.     [System.Serializable]
    12.     public class CSVRangedData
    13.     {
    14.         //public GameObject weaponObject;
    15.         public int elementNumber;
    16.         public string rangedDataPrefsID;
    17.         public string weaponName;
    18.         public string chestType;
    19.         public int xpRequired;
    20.         public int magicCost;
    21.         public int itemCost;
    22.         public int firePointSize;
    23.         public float timeBetweenShots;
    24.         public float speed;
    25.         public int baseDamage;
    26.         public float spread;
    27.         public float knockbackForce;
    28.  
    29.     }
    30.  
    31.     [System.Serializable]
    32.     public class CSVRangedDataList
    33.     {
    34.         public CSVRangedData[] rangedData;
    35.     }
    36.  
    37.     public CSVRangedDataList rangedDataList;
    38.  
    39.  
    40.  
    41.     // Start is called before the first frame update
    42.     void Awake()
    43.     {
    44.         ReadCSV();
    45.     }
    46.  
    47.     public void ReadCSV()
    48.     {
    49.         string[] data = textAssetData.text.Split(new string[] { ",", "\n" }, StringSplitOptions.None);
    50.         int tableSize = data.Length / 13 - 1;
    51.  
    52.         rangedDataList.rangedData = new CSVRangedData[tableSize];
    53.  
    54.         for (int i = 0; i < tableSize; i++)
    55.  
    56.         {
    57.             rangedDataList.rangedData[i] = new CSVRangedData();
    58.  
    59.             rangedDataList.rangedData[i].elementNumber = int.Parse(data[13 * (i + 1)]);
    60.             rangedDataList.rangedData[i].rangedDataPrefsID = data[13 * (i + 1) + 1];
    61.             rangedDataList.rangedData[i].weaponName = data[13 * (i + 1) + 2];//<== fixed here
    62.             rangedDataList.rangedData[i].chestType = data[13 * (i + 1) + 3];//<== fixed here
    63.             rangedDataList.rangedData[i].xpRequired = int.Parse(data[13 * (i + 1) + 4]);//<== fixed here
    64.             rangedDataList.rangedData[i].magicCost = int.Parse(data[13 * (i + 1) + 5]);//<== fixed here
    65.             rangedDataList.rangedData[i].itemCost = int.Parse(data[13 * (i + 1) + 6]);//<== fixed here
    66.             rangedDataList.rangedData[i].firePointSize = int.Parse(data[13 * (i + 1) + 7]);//<== fixed here
    67.             rangedDataList.rangedData[i].timeBetweenShots = float.Parse(data[13 * (i + 1) + 8]);//<== fixed here
    68.             rangedDataList.rangedData[i].speed = float.Parse(data[13 * (i + 1) + 9]);//<== fixed here
    69.             rangedDataList.rangedData[i].baseDamage = int.Parse(data[13 * (i + 1) + 10]);//<== fixed here
    70.             rangedDataList.rangedData[i].spread = float.Parse(data[13 * (i + 1) + 11]);//<== fixed here
    71.             rangedDataList.rangedData[i].knockbackForce = float.Parse(data[13 * (i + 1) + 12]);//<== fixed here
    72.  
    73.             //rangedDataList.rangedData[i].weaponObject = CSVReader.instance.availableRanged.;
    74.  
    75.             //CSVReader.instance.availableRanged.Add(weaponClone);
    76.  
    77.  
    78.  
    79.         }
    80.     }
    81.  
    82. }
    83.  
    I know it's a lot of copy paste here, just after googling and searching the forums, I'm not finding much info on how to access that array from a specific class. Any ideas?
     
  2. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    so you declare a new class:
    Code (CSharp):
    1. public class CSVRangedData {}
    But you never make a new instance of this. So I'm confused after that point, because anything after that should be easily understood. And this part definitely throws me for a loop:
    Code (CSharp):
    1. public class CSVRangedDataList
    2.     {
    3.         public CSVRangedData[] rangedData;
    4.     }
    Although I'm confused, take that with a grain of salt, because I'm sure more experienced minds might know what it is you're trying to accomplish. But as far as I'm aware, you make a class containing variables, make a new instance of it, then use that "container" to your hearts content..

    No, I think I'm still confused, ignore me...
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,143
    Your class is CSVReaderRanged. And since it's a MonoBehaviour, that means you have an instance in your scene somewhere.

    If that instance already exist, then you need to get that instance. Either you have a variable on another GameObject

    Code (CSharp):
    1. CSVReaderRanged readerRanged;
    And you connect that script to that variable in the inspector, or you connect it by code. Then it's just

    Code (CSharp):
    1. readerRanged.rangedDataList[someIndex];
    This is of course assuming all your other code is setup correctly.
     
  4. jleven22

    jleven22

    Joined:
    Mar 26, 2019
    Posts:
    397
    Honestly I thought it was, but again in over my head a little bit here.

    My plan was to use another script to do more of the management operation, kind of like a game manager.

    Started that here:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CSVReader : MonoBehaviour
    6. {
    7.  
    8.     public static CSVReader instance;
    9.  
    10.     public CSVReaderRanged CSVReaderRanged;
    11.     public CSVReaderMelee CSVReaderMelee;
    12.  
    13.     public GameObject[] availableRanged;
    14.     public GameObject[] availableMelee;
    15.  
    16.  
    17.     private void Awake()
    18.     {
    19.         instance = this;
    20.  
    21.         CSVReaderRanged = GetComponent<CSVReaderRanged>();
    22.         CSVReaderMelee = GetComponent<CSVReaderMelee>();
    23.  
    24.  
    25.     }
    26.  
    27. }
    28.  
    By this logic I was hoping to be able to go:

    CSVReader.instance.CSVReaderRanged.readerRanged.rangedDataList[someIndex]


    Then run through it and do some checking of that data. But it's throwing an error on "instance"
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,143
    What is the error?
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,711
    Tracking pre-authored game content in an external CSV files is probably one of the hardest ways to go about it.

    The only point would be if you had a team WITHOUT access to Unity who need to manipulate fields via spreadsheet.

    Instead, put your data in to ScriptableObjects. They are first class citizens in Unity and can be shaped and sculpted to precisely represent your necessary data, edited right in the inspector, serialized and saved right inside your project, and attached to pretty much anything within Unity, including other assets and prefabs and scenes.

    ScriptableObject usage in RPGs:

    https://forum.unity.com/threads/scr...tiple-units-in-your-team.925409/#post-6055289

    https://forum.unity.com/threads/cre...ssigned-in-the-inspector.946240/#post-6174205

    Usage as a shared common data container:

     
  7. jleven22

    jleven22

    Joined:
    Mar 26, 2019
    Posts:
    397
    I appreciate all the responses. I've been told multiple times to use Scriptable Objects, but for my purposes using a spreadsheet makes more sense because I have a lot of data to keep track of.

    That said, I have found a way forward with this. I ended up basically having to rethink the structure of how I'm storing things, and I think I'm good here. Thanks all!
     
  8. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,843
    We've suggested it before, but you really should just use this CSV data to generate scriptable objects based on the data at editor-time. Then you don't have to go through all this rigamarole with injecting data into game objects and instead just reference objects via the inspector as normal.

    And I would only really consider it 'a lot of data' if there was tens of thousands of individual items.
     
  9. CodeRonnie

    CodeRonnie

    Joined:
    Oct 2, 2015
    Posts:
    284
    I agree with Kurt and Spiney. I would not want to do so much content authoring work outside of the built-in tools in Unity. However, your situation does remind me of another developer that shipped a game by authoring much of the content outside of Unity. The game is West of Loathing. They don't give all of the technical details about exactly how it was engineered, but in a few of their developer talks they do give a high level overview of how it all worked. Having worked on another web-based game for many, many years, they were more comfortable with web development tools, and the lead developer wanted to be able to work on the game from anywhere without having to recompile a new Unity build. So, a different engineer on the team that was more well versed with Unity was able to create a system for importing new content directly into existing builds. The Unity player build functioned more like a client, and they were able to create new content with a workflow that they preferred and worked for them. Bear in mind, all of the art is stick figure art, and it sounds like they weren't able to author any animations in their custom tools. To each their own, but it would not be my cup of tea.