Search Unity

To many things in Void Update

Discussion in 'Scripting' started by GoofBall101, Oct 4, 2015.

  1. GoofBall101

    GoofBall101

    Joined:
    Jul 25, 2015
    Posts:
    57
    Hello, I'm wondering if I have one to many lines of code in my Void Update. Theirs 300 lines of code with only this repeated 30 times.Seems like it would kill the IOS/Android device.
    Code (csharp):
    1.  
    2. void Update(){
    3.  
    4. if (my item){
    5. PlayerPrefs.GetString("item", item );
    6. bool = false;
    7.       }
    8.  
    9. }
    10.  
     
    Last edited: Oct 4, 2015
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    You shouldn't read from or write to PlayerPrefs in Update without any proper control. It seems that some of your conditions are permanently true.
    Load the values once, cache them in variables and save them when needed.
     
    Kiwasi likes this.
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    No one is going to steal your wonderful idea. But there are plenty if better ways to code this then repeating the same code 30 times over. For a starter use a loop.

    There is very little point to accessing player prefs in update. I would suggest loading all of the variables once into an array. Then reference the array data. When it's time to exit save the data back to player prefs.
     
    Suddoha and Ironmax like this.
  4. Ironmax

    Ironmax

    Joined:
    May 12, 2015
    Posts:
    890
    Make a data manager with a singleton, and event delegets , data stream should never be in update, as boredmormon stated here also.
     
  5. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    So you want us to provide you with code and help for your own personal gain, but won't share your "wonderful" idea with us?

    That's pretty scummy mate.
     
    Suddoha and Ironmax like this.
  6. Yash987654321

    Yash987654321

    Joined:
    Oct 22, 2014
    Posts:
    729
    Or uses a coroutine if you are desperate for doing things again and again.
     
  7. GoofBall101

    GoofBall101

    Joined:
    Jul 25, 2015
    Posts:
    57
    I just asked would having 300 lines of code kill the IOS/Android device.
     
  8. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    You could put 3000 lines in and it will still run better than accessing playerprefs in update.

    You want a bottle neck? because thats what you made.

    I think you could improve it with this code...


    Code (csharp):
    1.  
    2. void Update(){
    3.  
    4. while(true) {
    5. if (my item){
    6. PlayerPrefs.GetString("item", item );
    7. bool = false;
    8. }
    9.       }
    10.  
    11. }
    12.  
     
    GroZZleR likes this.
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    The number of lines of code is irrelevant to performance. One slow call can do for more damage to performance then 50,000 fast calls.

    Several competent people have suggested that a more intelligent data structure could improve performance. I suggest you consider their advice.

    And finally, when it comes to performance, the profiler is always the answer.
     
  10. GoofBall101

    GoofBall101

    Joined:
    Jul 25, 2015
    Posts:
    57
    i do.
    Code (csharp):
    1.  
    2. if(Z15 == true) {
    3. PlayerPrefs.SetInt("QuestionNumber", Question108);
    4. Z15 = false;
    5. Z100 = true;
    6. Z98 = true;
    7. }
    8.  
    Basically it just starts with one and when someone chooses a option it goes to the 2 other options and saves the players position in the level. their 100's of questions which is why I have so many lines. Thanks for all the help.