Search Unity

Using a string as a variable name

Discussion in 'Scripting' started by WhatMade, May 9, 2019.

  1. WhatMade

    WhatMade

    Joined:
    Jan 31, 2014
    Posts:
    21
    So hopefully I can explain this correctly...

    I'm making a dialogue system that can activate quests and change the quests value. I wanted to be able to input the name of the quest in the inspector on the NPC and then have the script call the variable by the string that I entered in another script. So basically you type in the string "QuestKillKing" for the variable 'ActiveQuest' on the dialogue script and it would then use that string to reference the QuestKillKing variable in the quests script. Just wondering if anyone had any advice on doing this. I've gotten everything down just not how to use the entered string to reference the variable in the other script. Thanks!
     
  2. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    493
  3. WhatMade

    WhatMade

    Joined:
    Jan 31, 2014
    Posts:
    21
    I will check that out. I had read up on it a little before but wasn't sure if that would work. Thanks!
     
  4. Shack_Man

    Shack_Man

    Joined:
    Jun 7, 2017
    Posts:
    372
    I would try to avoid using strings as references. A typo and you're done, and you're not even catching it when compiling, only when the game is running. I think a much better solution could be scriptable objects, depends on what data you are holding in the quest script.
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Could use a simple switch also.
     
  6. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    493
    I would avoid strings as well, but I didn't want to bother you with specifics, a dictionary will prove challenging on its own.
    How I would do it: I would use an enum that would hold all types of quests. After I select the enum (instead of typing in the string), I would simply convert the enum to a string and use it after. This makes minimal changes to your code with almost no expenses.

    Code (CSharp):
    1.     /// <summary>
    2.     /// Gets the name of the sound
    3.     /// based off of the given integer
    4.     /// </summary>
    5.     /// <param name="mySoundEnum">What sound are we playing?</param>
    6.     /// <returns>The name of the sound clip to play.</returns>
    7.     private string GetSoundNameFromEnum(int mySoundEnum)
    8.     {
    9.         switch (mySoundEnum)
    10.         {
    11.             case (int)Sounds.SoundName1:
    12.                 return "SoundName1";
    13.             case (int)Sounds.SoundName2:
    14.                 return "SoundName2";
    15.             case (int)Sounds.SoundName3:
    16.                 return "SoundName3";
    17.             default:
    18.                 return null;
    19.         }
    20.     }
     
  7. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,780
    Definitely enum, rather strings, what her possible.
     
  8. WhatMade

    WhatMade

    Joined:
    Jan 31, 2014
    Posts:
    21
    Thanks Dextozz, I'll give that a try instead.
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,749
    Beware that if you use enums and then serialize your data anywhere (like for save games), the serialization will only save the numeric value, NOT the semantic value of the enum. If you add a new enum in that renumbers the others, you will have some really weird bugs that are almost impossible to reason about.

    Personally I would use strings, because then your save files are extremely readable, and everything in your code is super-easy to debug compared to enums. Everybody chuffs about typos in strings but there are plenty of ways to guard against this with defensive coding, such as always validating against a known list of strings, or having default handling that complains bitterly and meaningfully when it encounters a string it doesn't know or doesn't find.
     
    spicerack and quietstorm-92 like this.
  10. Even if you do not serialize your data, it's very bad coding habit to completely remove and reuse numbering regarding enums. Consider the enum values forever taken and only deprecate ones you don't use anymore and add new ones. They are free.
     
  11. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,780
    Is very easy to prevent any issues with enums.
    Simply assign value to enum and don't allow to change it.
    For example
    enum MyEnym
    {
    undefined = 0,
    Health = 10,
    Car = 20,
    Doll = 14392
    }

    Then ordering adding, or removing makes no difference. Unless assigning value of existing fields changes. For example health become 11 instead 10. And that is only issue, if indeed storing data in save file, or similar.
    Otherwise, it doesn't matter.