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

Scriptable Objects and String Interpolation

Discussion in 'General Discussion' started by msurz89, May 24, 2022.

  1. msurz89

    msurz89

    Joined:
    Apr 13, 2021
    Posts:
    8
    Is this possible? If so, how would it be accomplished? I have SO for card data for a card game im making. I fill in all the relevant data like name, description, effect, damage, etc. and another script to output all that information onto a drawn card. I would like there to be a way for the displayed card to interpolate the string in description as to display the cards current damage or effect in the description. For example, instead of hard coding a description like " Deals 5 damage " via the inspector of the SO, is it possible to do something like $"Deal {cardDamage} damage" so that all the relevant data is variable at runtime depending on other card effects or mechanics?
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,997
    Please use the proper forum topics to post questions. If you have Scripting questions, use Scripting topic, if you have editor modification questions, use the Editor topic...

    For the purpose you mentioned you will need to use the string.Format method. Just document what the numbers mean and when you display the description, run through a method and execute the
    string.Format
    method replacing the proper constants with actual values.
     
    Antypodish likes this.
  3. msurz89

    msurz89

    Joined:
    Apr 13, 2021
    Posts:
    8

    That surely did do the trick, and sorry about posting in the wrong spot. I do have some follow up questions though, is there a way to make this more modular? For example, I have multiple different damage types like fire, lightning, ice, etc. How would I go about outputting the relevant data for the specific card? Like instead of
    Code (CSharp):
    1. string s = string.Format(card[i].cardDes, card[i].cardDamage);
    2.         desText.text = s;
    how would I make it to where if cardDamage = 0 but the cards cardFireDamage = 5, it displays the fire amount instead?

    If that makes any sense.
     
  4. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,997
    Not really makes sense, because a ton of information is missing. Like can you have multiple damages simultaneously? Or just one should be >0 all others should be 0? Do you make any distinction between these or it's just textual? Like do you need the type of damage in code or it's just story and you use it in the UI?
    And so on, it is up to you to build the architecture.
    The simplest way to show the damage no matter what (and if you don't have multiple damages or you're okay to simply add them together) is just
    string s = string.Format(card[i].cardDes, card[i].cardDamage + card[i].cardFireDamage);

    When the damage is 3 and the fire damage is 0, it shows 3. If the damage is 0 and the fire damage is 5, it shows 5.
     
  5. msurz89

    msurz89

    Joined:
    Apr 13, 2021
    Posts:
    8

    You answered it in the last sentence, tyvm!