Search Unity

Question regarding creating objects from txt file

Discussion in 'Scripting' started by ensiferum888, Nov 13, 2014.

  1. ensiferum888

    ensiferum888

    Joined:
    May 11, 2013
    Posts:
    317
    Good afternoon everyone! For the first time in a long time I'm running into a problem that have absolutely no idea how to get around.

    I have a system in my game that's extremely similar to Crusader Kings, where characters have traits and some stats. I have a few events that I handcrafted in Unity but it's very long and I'd like to allow players to add their own events. I'm already parsing all my traits from a text file because they're all the same structure and hardcoded values.

    Here is what a trait looks like:
    Code (CSharp):
    1. public class Trait{
    2.     public string Name;
    3.     public string Description;
    4.     public int[] modifiers;
    5.     public int SeasonalRenown;
    6.     public int SeasonalPiety;
    7.     public int HealthModifier;
    8.    
    9.     public Trait(int _id, string _name, string _description, int[] _modifiers,
    10.         int _renown, int _piety, int _health){
    11.             this.Name = _name;
    12.             this.Description = _description;
    13.             this.modifiers = _modifiers;
    14.             this.SeasonalRenown = _renown;
    15.             this.SeasonalPiety = _piety;
    16.             this.HealthModifier = _health;
    17.     }
    18. }
    And then when someone picks up a trait for example:
    Code (CSharp):
    1. public void PickUpTrait(string traitIndex){
    2.     if(traitList.Contains(traitIndex)){
    3.         return;
    4.     }
    5.     Trait tempTrait = TraitDictionary[traitIndex];
    6.     for(int i = 0 ; i < 5 ; i++){
    7.         this.ModifiedAttributes[i] += tempTrait.modifiers[i];
    8.     }
    9.     this.RenownPerSeason += trait.SeasonalRenown;
    10.     this.PietyPerSeason += trait.SeasonalPiety;
    11.     this.Health.AddPenalty(trait.HealthModifier);
    12.     this.traitsList.add(traitIndex);
    13. }
    That's all good, it's very modular and can be extended as much as a player wants by simply editing the text file. This is what a trait looks like for example:

    I'm comfortable enough to parse data like that into traits objects and using them in game. Now I'm trying to come up with the same scheme but for events.

    Imagine my file goes like this:
    In this case for example, I want the first response to be only available if your character has 100 golds or more. So somehow I should resolve the condition block to a bool. I was wondering if anyone could point me in the right direction?

    The only way I can think of right now is using a monolithic if/else like:

    Code (CSharp):
    1.  
    2. public bool AssessCondition(string key, int value){
    3.     ....
    4.     if(key == "gold"){
    5.         return Authority.Instance.leaderInventory.Coins >= value;
    6.     }
    7.     ....
    8. }
    9.  
    10. public void PerformAction(string key, int value){
    11.     ....
    12.     if(key == "spend_gold"){
    13.         Authority.Instance.leaderInventory.SpendCoins(value);
    14.         return;
    15.     }
    16.     if(key == "base_opinion"){
    17.         Authority.ModifyBaseOpinion(value);
    18.         return;
    19.     }
    20.     ....
    21. }
    Is there a more intelligent way to go about this or am I going down the right path?

    Thank you for your time and if there's any information you'd like to know I'll be happy to provide.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I think you're basically headed down the right path. You could tighten up the code a little bit (but not much) by using a switch statement; or there are various tricks you could do using introspection, but on the whole I think we way you've approached it is fine.
     
    djfunkey likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    This looks. Like the right way to me as well. As far as specifying your conditions, you can just list perhaps something called "prerequisites" that must all be true for the player to get the option, etc.

    It is often easiest (at first) to out just the prerequisite conditions you need in: has X gold, has player with ranged weapon, has somebody with good lock picking skills, etc. then when you finally outgrow that, only then bother to look into something more generalized like reflection or some other deeper more-generalized data inspection.