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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Lag questions.

Discussion in 'Scripting' started by spil778, Feb 23, 2015.

  1. spil778

    spil778

    Joined:
    Mar 31, 2013
    Posts:
    57
    Hallo its my first real game I am making and this one is on a big scale. With this I am kinda concern about lag and what there is to prevent it. I have made a magic creation system and the switch statements are clogging up I was wondering general what there is to do about lag.

    Also will this be any big lag source? This will be excuted once you poke the button:
    http://pastebin.com/sjjv1Mrd

    Its an RPG.
     
  2. Deleted User

    Deleted User

    Guest

    Will this produce lag? probably not but you would need to messure that. Is there room for improvements? Hell yeah!
    Why are you using strings for all that? Enums are probably faster and easier to maintain
     
  3. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    This is quite a big switch case and although it may not cause that much of a lag because
    its just executed when you poke the button, I would avoid it.

    You could reduce the number of case checks significantly by having different arrays
    of strings - one such array for each type of element( fire, water, steam, gas etc )
    and just pass integer index to access the combo you want.

    Something like this maybe :

    Code (CSharp):
    1. string[] fireStrings = { "scorch", "steam", "lava", "gas" };
    2. string[] waterStrings = { "steam", "fluid", "tsunami", "ice" };
    3. ..etc
    4.  
    5. public string YingYangMixing(string element, int index) {
    6.  
    7. string returnString;
    8.  
    9.            switch(element){
    10.             case "fire" :
    11.              returnString = fireStrings[index];
    12.  
    13.              case "water" :
    14.              returnString = waterStrings[index];
    15.  
    16. ...etc
    17. }

    So for example if you want to get "tsunami", you can call YingYangMixing("water", 2);
     
  4. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    A further optimization would be to have even the first argument passed as an integer..i.e
    instead of passing "element" as a string , you can pass an int there too.
     
  5. spil778

    spil778

    Joined:
    Mar 31, 2013
    Posts:
    57
    Well the way it works as you can see is that there are element 1 and 2 these two combined adds a new one.
    The new ones will be added and then you can combine new elements with that.
    The idea with your gamer_boy_81 is that I replace the return string with an Array right?
    Though the if we take part in the example of: earth-water = wood.
    Why would enum make this easier? And how could the example be?

    For the first element and second element I use this:
    Code (CSharp):
    1. GUILayout.SelectionGrid(0,text,count,"toggle");
     
  6. spil778

    spil778

    Joined:
    Mar 31, 2013
    Posts:
    57
    so in general: int > string
    when it comes to performance?
     
  7. Deleted User

    Deleted User

    Guest

    yes int comparision are faster than strings. But magic numbers are also not a good idea. Thats why I mentioned enums.
    This is how I would do it
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections.Generic;
    4.  
    5. public class Test : MonoBehaviour
    6. {
    7.     /// <summary>
    8.     /// Different elements to use
    9.     /// </summary>
    10.     public enum Element
    11.     {
    12.         None = 0,
    13.         Fire = 1,
    14.         Water = 2,
    15.         Earth = 4,
    16.         Wind = 8
    17.     }
    18.  
    19.     /// <summary>
    20.     /// Effects
    21.     /// </summary>
    22.     public enum Effect
    23.     {
    24.         None,
    25.         Scorch,
    26.         Steam,
    27.         Lava,
    28.         Gas
    29.     }
    30.  
    31.  
    32.     /// <summary>
    33.     /// Elemet to Effect lookup table
    34.     /// </summary>
    35.     private readonly Dictionary<int, Effect> _lookUpTable = new Dictionary<int, Effect>()
    36.     {
    37.         { (int)Element.Fire, Effect.Lava },
    38.         { (int)( Element.Fire | Element.Water), Effect.Steam },
    39.     };
    40.  
    41.     public Effect YingYangMixing( Element element )
    42.     {
    43.         Effect ret = Effect.None;
    44.         if( !_lookUpTable.TryGetValue( (int)element, out ret ) )
    45.             Debug.LogError( "Element does not exist in Lookup table!" );
    46.  
    47.         return ret;
    48.     }
    49.  
    50.     private void Update()
    51.     {
    52.         // usage
    53.         Effect effect = YingYangMixing( Element.Fire | Element.Water);
    54.         if( effect == Effect.Steam )
    55.             DoSomething();
    56.     }
    57.  
    58. }
    59.  
     
    Last edited by a moderator: Feb 23, 2015
  8. spil778

    spil778

    Joined:
    Mar 31, 2013
    Posts:
    57
    Um how would you do the combination part? I just barely understand that code example lets say:
    earth + water = wood
    wood + fire = coal

    This is also a valid combination. I cannot see out from your example element_wsc how this would be achived?
     
  9. Deleted User

    Deleted User

    Guest

    you do that when you initialise the Dictionary
    Code (csharp):
    1.    
    2. private readonly Dictionary<int, Effect> _lookUpTable = new Dictionary<int, Effect>()  
    3. {      
    4.     // the elemt                                                     The effect
    5.     { (int)Element.Fire,                                         Effect.Lava },      
    6.     { (int)( Element.Fire | Element.Water),           Effect.Steam },  
    7.  };
    8.  
    and combine them with the bit wise or operator ( | ). so for your examples you would add
    Code (csharp):
    1.  
    2. { (int)( Element.Earth | Element.Water),           Effect.Wood },
    3. { (int)( Element.Wood | Element.Fire),              Effect.Coal },
    4.  
    ofcourse if you want wood to be an element you would also add that to your enum. If you want to know why and how that works, you need to first understand that enums are just int's nothing else. And for bit wise operation you just google it, there is plenty information about that out there
     
  10. Deleted User

    Deleted User

    Guest

    if you like to wath videos, here is a goog playlist about bitwise operations. He does it in C++ but the same principle and syntax applies to c#


    Edit:
    Damit the forum automatically just linkes the first video instead of the playlist. so here is the part after youtube.com
    /watch?v=XcGIE3LOwLY&list=PLRwVmtr-pp043Ah3GxAqNyoMGyo3_LDNh
     
  11. spil778

    spil778

    Joined:
    Mar 31, 2013
    Posts:
    57
    I have worked with bit wise opertation(while coding arduino) and actually already have an enum list :) Ty sorry it was a bit confusing
     
  12. spil778

    spil778

    Joined:
    Mar 31, 2013
    Posts:
    57
    Ok just to make this up again. I have about 32 elements do the last bit then have 2^32 as a value?
    That is a pretty high number. Not that its a problem just wondering.
     
  13. Deleted User

    Deleted User

    Guest

    yes that is correct. you can also write
    someElement = 1 << 32
    other = 1 << 33
    and so on. When you do it that way you need to start counting with 0
    Code (csharp):
    1.  
    2.     public enum Element
    3.     {
    4.         None     = 1 << 0,
    5.         Fire        = 1 << 1,
    6.         Water     = 1 << 2,
    7.         Earth     = 1 << 3,
    8.         Wind      = 1 << 4,
    9.         ...
    10.         SomeOther = 1 << 32
    11.     }
    12.  
    this might be a bit easier to write down