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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Concatenating a Variable name?

Discussion in 'Scripting' started by kodarr, Dec 20, 2021.

  1. kodarr

    kodarr

    Joined:
    Sep 27, 2021
    Posts:
    8
    Ok so here is my problem. I have a ton of different variables with the difference being a basic name
    ex:
    Code (CSharp):
    1. public int fastFoodLv=1;
    2.     public int[] fastFood = {1,10,25,60, 200, 500, 1400, 3000, 5000}; //levels for job titles
    3.     public string[] fastFoodTitle = {"Trainee", "Janitor","Fry Cook", "Working the Grill", "Cashier", "Drivethrew Cashier", "Manager", "CEO", "Owner" };
    4.     public int fastFoodIndex = 0;
    5.     public float fastFoodXPCurrent = 0;
    6.     public float fastFoodXPNext = 500;
    7.     public int[] fastFoodTimeUsed = {3,3,3,3,3,2,2,2,1 };
    8.     public int[] fastFoodIncome = {1,2,3,4,5,6,7,8,9 };
    Then there are other jobs that have the same endings but different words in front so instead of fastFood it may have office.

    I'd like to be able to do something like

    Code (CSharp):
    1. string name = "fastFood";
    2.  
    3.  
    4. LV = "GameControl.instance."+name+"Title[index]";
    instead of using an if checking every job. I want to say I've seen this done somewhere before concatenating a variable name but no idea where and been searching for days with no solutions.
    Is this possible?
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    You can technically use reflection through the
    Type.GetProperty
    method, but I would highly recommend not doing that. Wouldn't it be much easier to have a base class like this
    Code (CSharp):
    1.     public int Level=1;
    2.     public int[] TitleLevel = {1,10,25,60, 200, 500, 1400, 3000, 5000}; //levels for job titles
    3.     public string[] Title = {"Trainee", "Janitor","Fry Cook", "Working the Grill", "Cashier", "Drivethrew Cashier", "Manager", "CEO", "Owner" };
    4.     public int Index = 0;
    5.     public float XPCurrent = 0;
    6.     public float XPNext = 500;
    7.     public int[] TimeUsed = {3,3,3,3,3,2,2,2,1 };
    8.     public int[] Income = {1,2,3,4,5,6,7,8,9 };
    and then use a Dictionary to keep track of the different ones

    Code (CSharp):
    1. // the dictionary to hold your jobs
    2. Dictionary<string,Job> JobDictionary = new Dictionary<string,Job>();
    3.  
    4. //how to access them or whatever
    5. JobDictionary["Food"].XPCurrent += 10;
     
    Bunny83 likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    Don't forget to do:

    JobDictionary["Food"] = new Job();


    before you try and stick things into it! :)
     
    Bunny83 and RadRedPanda like this.
  4. kodarr

    kodarr

    Joined:
    Sep 27, 2021
    Posts:
    8
    RadRedPanda you are a godsend. That's perfect no idea why I didn't think of this earlier. I was so fixated in trying to derive the variable name with concatenation I forgot dictonary data types existed. Now I can just make scriptable object jobs and add them as I complete each.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    Okay, then I amend my above: you cannot use new with a ScriptableObject or Monobehaviour, so the allocation would look like:

    JobDictionary["Food"] = ScriptableObject.CreateInstance<Job>();


    If you're going to index stuff with strings, you really should make them all const strings to avoid future typos.

    Code (csharp):
    1. const string s_Food = "Food";
    and then always use
    s_Food
    to access the dictionary.
     
  6. kodarr

    kodarr

    Joined:
    Sep 27, 2021
    Posts:
    8
    const stings is a good idea. as it is right now I've been using
    Code (CSharp):
    1. for (int i = 0; i < jobList.Length; i++)
    2.         {
    3.             AddJob(jobList[i].name, jobList[i]);
    4.         }
    The scriptable object's name is the string name I'm referring to it. But I can easily compile those into some constants if I need to type them anywhere. But right now everywhere they are needed using the reference of the script name is working.
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    Oh that's good too... as long as there is one authoritative place for the string!!

    Another thing you can do is use the actual
    .name
    of the ScriptableObject, as long as it is unique.