Search Unity

Anyone willing to optimize my code?

Discussion in 'Scripting' started by Josenifftodd, Dec 21, 2014.

  1. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    I have a script that does 100% of what I want, I'm not a professional coder as such but I know they might be some things in my script that would maybe work a different way, is they anyone willing to look over my coding through private message and if so are you willing to help me make some changes that could make my coding shorter rather than 200 lines long?
     
  2. toreau

    toreau

    Joined:
    Feb 8, 2014
    Posts:
    204
    Why don't you post your code here so that we all can have a look at it?
     
  3. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    Yeah can do man just wanted to see if anyone was up for it :) I'll go get it and post it :D
     
  4. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    Code (JavaScript):
    1. // Battle Settings
    2. private var turn : int = 1;
    3. private var phase : int = 0;
    4. public var timer : int = 10;
    5. public var attackEffects : GameObject;
    6. public var effectObj : GameObject[];
    7. private var arrayNo:int = 0;
    8.  
    9. // Player stats
    10. var playerMinAttack : float = 5.0;
    11. var playerMaxAttack : float = 10.0;
    12. var player : GameObject;
    13.  
    14. // Enemy Stats
    15. var enemyMinAttack : float = 5.0;
    16. var enemyMaxAttack : float = 10.0;
    17. var enemyhitpoint : GameObject;
    18. var enemyMonster : GameObject;
    19. // Enemy Settings
    20. var enemyToAttack : String;
    21. var enemyTurnActivated : boolean = false;
    22. var enemyAnimate : boolean = true;
    23. var enemyHit : boolean = false;
    24.  
    25.  
    26.  
    27. function Start () {
    28.    
    29. }
    30. function OnGUI () {
    31.        
    32.         var otherScript: MainGame1 = gameObject.Find("MainGame").GetComponent(MainGame1);
    33.         if(otherScript.inBattle == true) {
    34.                 if(turn == 1) {
    35.                         if(phase == 0) {
    36.                 if (GUI.Button (Rect (Screen.width / 2 - 600,Screen.height / 2 + 200,140,20), "Attack")) {
    37.                         phase = 1;
    38.                 }
    39.                 if (GUI.Button (Rect (Screen.width / 2 - 600,Screen.height / 2 +225,140,20), "Item")) {
    40.                         phase = 2;
    41.                 }
    42.                 if (GUI.Button (Rect (Screen.width / 2 - 600,Screen.height / 2 + 250,140,20), "Run")) {
    43.                         phase = 3;
    44.                 }
    45. }
    46.    
    47.                    if(phase == 1) {
    48.    
    49.                 if (GUI.Button (Rect (Screen.width / 2 - 600,Screen.height / 2 + 200,140,20), "Slice", effectObj[ arrayNo ].name)) {
    50.                         turn = 2;
    51.                         effectOn();
    52.                         enemyHit = true;
    53.                         enemyAnimations();
    54.                         enemyhitpoint.SendMessage("damageEnemy", Random.Range(1,10));
    55.                         //timer = 10;
    56.                         phase = 0;
    57.                    
    58.                        
    59.                 }
    60.                 if (GUI.Button (Rect (Screen.width / 2 - 600,Screen.height / 2 +225,140,20), "Back")) {
    61.                         phase = 0;
    62.                 }
    63. }
    64.                 if(phase == 2) {
    65.                 if (GUI.Button (Rect (Screen.width / 2 - 600,Screen.height / 2 + 200,140,20), "Health Potion")) {
    66.                         turn = 2;
    67.                         phase = 0;
    68.                 }
    69.                 if (GUI.Button (Rect (Screen.width / 2 - 600,Screen.height / 2 +225,140,20), "Back")) {
    70.                         phase = 0;
    71.                 }
    72. }
    73.                 if(phase == 3) {
    74.                 if (GUI.Button (Rect (Screen.width / 2 - 600,Screen.height / 2 + 200,140,20), "Run Away")) {
    75.                         phase = 0;
    76.                                          
    77.                         otherScript.inBattle = false;
    78.                      
    79.                        
    80.                        
    81.                 }
    82.                 if (GUI.Button (Rect (Screen.width / 2 - 600,Screen.height / 2 +225,140,20), "Back")) {
    83.                         phase = 0;
    84.                 }
    85.             }
    86.         }
    87.     }
    88.     GUI.Box(new Rect(10,10,50,20), "" + timer.ToString("0"));
    89.     if (GUI.Button (Rect(50, 0, 200, 30), effectObj[ arrayNo ].name )) {
    90.         effectOn();
    91.         }
    92. }
    93. function Update () {
    94.         if(turn == 2 && enemyTurnActivated == false) {
    95.                 enemyTurnActivated = true;
    96.                 enemyTurn();
    97.                
    98.         }          
    99.        
    100. }
    101.  
    102. function enemyTurn () {  
    103.             yield WaitForSeconds(1);
    104.             enemyMonster.SendMessage("attackEnemy", 0); // this stops the enemy animation from playing
    105.              yield WaitForSeconds(3);
    106.             player.SendMessage("damagePlayer", Random.Range(1,10));   // This sends a message to the player health script                
    107.             yield WaitForSeconds(timer);
    108.             enemyTurnActivated = false;
    109.             turn = 1;
    110. }
    111.  
    112. function effectOn(){
    113.     if(attackEffects != null)Destroy( attackEffects );
    114.     attackEffects = Instantiate(effectObj[ arrayNo ] );
    115. }
    116.  
    117. function enemyAnimations () {
    118.         if (enemyHit == true) {
    119.         enemyMonster.SendMessage("attackEnemy", 1); // This sets the Enemys Animator to true so hit animation plays.
    120.        
    121. }}
    122.  
    Just really wanting to know if they is other ways of doing some of functions differently and also wanting to know if they is a better way of doing the GUI without having to keep writing each line out is they a way of making it so i can add a new button via the inspector? I know my coding is probably not the best but I'm only looking at this as a starting point for my battle system, enemy attacks, and player attack each other so does what I want just not sure if I've took the right approach. Cheers in advance for any help which is given to me.
     
  5. Vanamerax

    Vanamerax

    Joined:
    Jan 12, 2012
    Posts:
    938
    Direct things you could do:

    Move the gameObject.Find.GetComponent to the start method, these are two expensive calls and OnGUI can be called multiple times per frame.

    When you you are checking for all the phases, you could replace all the if statements with a switch statement.

    Other, more radical things you could do if you are on unity 4.6, is to use the new GUI system instead of OnGUI (OnGUI is bad for performance no matter how you use it)
     
  6. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    You should get the 4.6 beta and start learning how to use uGUI. Then you can build your GUI in the scene view.

    Also, I'm seeing a lot of use of sendmessage. Don't use that if you can avoid it.

    Other than that, not really much I'd change.
     
  7. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    I tried using the new GUI when I upgraded and well I'm not actually sure how to use it :/ I got about as far using the canvas and thats about it I'm not sure how to assign things to the gui on the canvas. Hence why I downgraded again :(

    Also about the sendmessage is they another way of sending the variable over to my other script? because I'm guessing that the enemy health and stats are seperate script to the actually battle system?

    I'm not even 100% sure how I'm going to make it so that each enemy has it's own health and when slice is clicked it asks you to choose what target to hit lol but I can come to that once I've sorted a few other things out.

    cheers for the help though guys.
     
  8. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Well, you need to invest time to understand the new system. That's completely normal, and some additional time to figure out the HowTo for coding with the new system.
    In general it's already quite user friendly, in my opinion. Regarding the advantages (not only performance wise) you have with the new system, it's worth the effort and time for sure.

    In order to avoid using SendMessage, you could get references to the components you need to operate with. Once set up, it's much more efficient than permanently using SendMessage.
     
  9. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    ahh the only part I don't understand is the whole how to make the buttons/images etc do what I want it to do.
    Do you know anywhere that has good tutorials and don't drag them out so long ?
    We've kinda driffted away from the whole reason I posted here but if you guys think the new uGUI will help me out then I'll deffoo go with what you guys say just need pointing in the right direction to where I can learn to use it. I'll google in the meantime but if you have links to how you learnt to use it please do provide :) Cheers again !!
     
  10. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    The tutorials section here at Unity has a whole bunch of uGUI tutorials. Yes, getting through them takes a minute, but trust me, when you're done you'll feel dumb for bothering with the legacy gui. It'll save you a lot of time in the long run, and gives you a lot more power.

    You wanted to optimize your code, well, getting rid of that legacy GUI code will optimize it drastically. Don't be afraid to learn to use an axe just because you can cut down a tree with your pocket knife.
     
  11. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    Okay awesome man I looked at one tutorial that unity released as a show off sort of thing from google and it does look pretty simple :) So learning at the moment, I have a question though someone said something about switches? would that still apply if I use the uGUI?
     
    Last edited: Dec 21, 2014
  12. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    I get how to make my buttons do what I want with the uGUI now but I don't understand how my turn thing will work? because if the say button GUI is being displayed,If I hit slice it will just stay there as the script above says if it isn't players turn then don't display GUI so would I just make it so that it turns the uGUI-button off using


    Code (JavaScript):
    1.  
    2.  
    3. this.active = false;
    I know you can do enabled as it keeps popping up but can change that later just wondering if I turn the the GUI button off after players go will my script still work the same way?
     
  13. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    Whoops forgot I could do
    Code (JavaScript):
    1. GameObject.Interactble = false;

    LOL cheers though guys. :)
     
  14. toreau

    toreau

    Joined:
    Feb 8, 2014
    Posts:
    204
    As mentioned by others: do all kinds of "get game objects" inside Start(), and start looking at uGUI; there are several good introductions on unity3d.com and Youtube.

    In addition, you could create all these buttons in Start() as well; at least in C# the GUI.Button returns the button created, so no need to create it every time. Maybe Unity is able to cache this or something, but I wouldn't bet on it and it's nevertheless good practice not to create something more than once.
     
  15. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    I don't get the new uGUI at all. Tried making it so that my health code for my player is displayed but it doesn't work and the ways I've looked at don't work my monodevelop doesn't pick up the coding for the new UI at all and doesn't display what I need it to keeps saying "text" isn't valid so when I put UI.Text like i says it doesn't work either :/ I don't even want to use the new UI for this simple reason now I'm raging because I've built something for the last 3 days non stop coding and now the new Unity has messed my whole project up nothing is working :/ can't even find any decent tutorials on how to do things because the tutorials I read or watch don't give examples and when they do they do it that fast. I'm downgrading again :(
     
  16. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    I don't use C# either as I can't get my head around how it all works. I know little things to do with it but other than that nothing ever works when I try coding using C# so nip that one off lol.
     
  17. darkAbacus247

    darkAbacus247

    Joined:
    Sep 7, 2010
    Posts:
    251
    You may also try using case statements, it could help clean up lines 30-80 a bit, perhaps not much less code but will run a decent deal faster. You may Even wish to combined them with a for loop to automatically grab the desired statement :)
     
    Josenifftodd likes this.
  18. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    The Unity tutorials for uGUI have example projects that show how to do just about everything you need.

    unity3d.com/learn/tutorials/modules/beginner/ui

    Really, if C# doesn't make sense to you, it's because you need to learn the fundamentals of programming. Unity makes it tempting to try to do things you don't understand, but until you understand what you're doing, it'll only work through luck.
     
    Vanamerax and Josenifftodd like this.
  19. Vanamerax

    Vanamerax

    Joined:
    Jan 12, 2012
    Posts:
    938
    Totally agree with that. Learning C# is totally worth it IMO, as UnityScript is trying to hide all kinds of implementation details for you that you probably should know about and who knows how long it remains supported, considering that unity is already trying to fade out Boo usage.
     
  20. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    Started learning it :) seems easy ish.