Search Unity

Time.timescale

Discussion in 'Scripting' started by Falin, Mar 29, 2011.

  1. Falin

    Falin

    Joined:
    Sep 29, 2009
    Posts:
    242
    Code (csharp):
    1. [B]var TTexture : Texture;
    2. var Tutorial = false;[/B]
    3. static var health : int = 100;
    4. static var max_health : int = 100;
    5. static var level: int = 0;
    6. static var exp: int = 0;
    7. static var endgame = 0;
    8. static var max_exp : int = 4;
    9. static var Gold : int = 0;
    10. var buttonSkin : GUISkin;
    11. var levelget : int;
    12. [B]var tutorial_1 = 0;[/B]
    13. function OnGUI ()
    14. {
    15. GUI.skin = buttonSkin;
    16.  
    17.     // Make a label that uses the "box" GUIStyle.
    18.     GUI.Label (Rect (0,0,200,20), "Health:" + health+ "/" +max_health , "box");
    19.     GUI.Label (Rect (0,20,200,20), "Checkpoint:" +level , "box");
    20.     GUI.Label (Rect (0,40,200,20), "Orbs:"+ exp +"/"+ max_exp , "box");
    21.  
    22.  
    23.      if( endgame == 1 )
    24.   {
    25.     GUI.Label (Rect( 350,300, 100,30 ),"Menu",  "box" );
    26.     if( GUI.Button( Rect( 350,330, 135,45 ), "Resume" ) )
    27.     {
    28.       endgame = 0;
    29.       Time.timeScale = 1;
    30.     }
    31.     if( GUI.Button( Rect( 350,375, 135,45 ), "Try again" ) )
    32.     {
    33.  health  = 100;
    34.  max_health = 100;
    35.  level = 0;
    36.  exp  = 0;
    37.  max_exp = 4;
    38.  nextlevel.checkpoint = 0;
    39.       endgame = 0;
    40.       Time.timeScale = 1;
    41.       Application.LoadLevel(levelget);
    42.      
    43.    
    44.     }
    45.     if( GUI.Button( Rect( 350,420, 135,45 ), "Quit" ) )
    46.     {
    47.  health  = 100;
    48.  max_health = 100;
    49.  level = 0;
    50.  exp  = 0;
    51.  max_exp = 4;
    52.  nextlevel.checkpoint = 0;
    53.       endgame = 0;
    54.       Time.timeScale = 1;
    55.       Application.LoadLevel("Menu");
    56.     }
    57.  
    58.   }
    59.   if( endgame == 2 )
    60.   {
    61.     GUI.Label (Rect( 350,300, 100,30 ),"Menu",  "box" );
    62.     if( GUI.Button( Rect( 350,330, 135,45 ), "Try again" ) )
    63.     {
    64.  health  = 100;
    65.  max_health = 100;
    66.  level = 0;
    67.  exp  = 0;
    68.  max_exp = 4;
    69.  nextlevel.checkpoint = 0;
    70.       endgame = 0;
    71.       Time.timeScale = 1;
    72.       Application.LoadLevel(levelget);
    73.     }
    74.     if( GUI.Button( Rect( 350,375, 135,45 ), "Quit" ) )
    75.     {
    76.  health  = 100;
    77.  max_health = 100;
    78.  level = 0;
    79.  exp  = 0;
    80.  max_exp = 4;
    81.  nextlevel.checkpoint = 0;
    82.       endgame = 0;
    83.       Time.timeScale = 1;
    84.       Application.LoadLevel("Menu");
    85.     }
    86.  
    87.   }
    88.   [B]   if(tutorial_1 == 1){
    89.     GUI.Box(Rect( 20,100,530,430),GUIContent ("Tutorial"));
    90.  if (GUI.Button(Rect (25,120,500,400),TTexture)){
    91.  Screen.lockCursor = true;
    92.     Time.timeScale = 1;
    93.     Tutorial = false;
    94.     tutorial_1 = 0;
    95.  }
    96.  }[/B]
    97.  
    98.     }
    99.  
    100. function Update () {
    101. [B]if(Tutorial == true){
    102.     tutorial_1 = 1;
    103.     Screen.lockCursor = false;
    104.     Time.timeScale = 0;
    105.     }[/B]
    106.     if(exp > max_exp){
    107.         exp = 0;
    108.     }
    109.     if( health <= 0)
    110.     {
    111.         Screen.lockCursor = false;
    112.         Time.timeScale = 0;
    113.         endgame = 2;
    114.     }
    115.     else if( health == max_health)
    116.     {
    117.         health = max_health;
    118.     }
    119.     if(Input.GetKey("q"))
    120.     {
    121.         endgame = 1;
    122.         Screen.lockCursor = false;
    123.         Time.timeScale = 0;
    124.        
    125.     }
    126.    
    127.     PlayerPrefs.SetInt("Gold",Gold);
    128. }
    So I added the higlighted things in the script but when the gui comes up the game isn't paused(When I press 'q' the game doesn't too). But when I remove it from the script the things work and when I press 'q' it pauses the game.
    I don't see what I did wrong.
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    heh, looks like a mess of speghetti code to me... Tabbing would help a bit so it was a bit hard to read.

    I suggest going a simpler route though. Let me give you a few good terms. GameState and GameController..

    GameState: The state at which your game is. Is it paused or running, is the player dead and we are on a certain screen. Using GameStates makes things smoother and alot cleaner. You dont have to have variables or numbers which represent states which you are at.

    GameController: A single object that transfers from one scene to another and controls the general aspects of your game. This is very useful for keeping track of lives, gui controls and lots of stuff. The biggest trick is to make sure you only have one of these in your scene.

    I threw this code out, I didnt test it so much as make sure it didnt have really dumb syntax errors. Use it as a guide to get your game setup and running and it will go from level to level and never die. Even if you restart, and try to create a new one, it will persist even through that.

    Code (csharp):
    1.  
    2. // GameController.js
    3.  
    4. enum GameState{
    5.     showStart,
    6.     running,
    7.     showPaused,
    8.     showEnd,
    9.     showTutorial,
    10.     levelCompleted
    11. }
    12.  
    13. var gameState : GameState=GameState.showStart;
    14. private var gameStateTemp : GameState=GameState.showStart;
    15.  
    16.  
    17. var Tutorial = false;
    18. static var health : int = 100;
    19. static var max_health : int = 100;
    20. static var level: int = 0;
    21. static var exp: int = 0;
    22. static var endgame = 0;
    23. static var max_exp : int = 4;
    24. static var Gold : int = 0;
    25. var buttonSkin : GUISkin;
    26. var levelget : int=0;
    27.  
    28. private var isPaused=true;
    29. private var showPaused=true;
    30. private var showTutorial=false;
    31. private var tutorialPage=1;
    32. var TTexture : Texture[];// an array of images that you cycle through in the tutorial page.
    33.  
    34. private var endGame=false;
    35. private var startGame=true;
    36.  
    37. function Start(){
    38.     DontDestroyOnLoad(this); // this object should never be destroyed
    39.    
    40.     // if we find a duplicate object in the scene destroy this one.
    41.     if(GameObject.Find("ExistingGameController")){
    42.         Destroy(this);
    43.     }
    44.    
    45.     // IMPORTANT: rename the object so that it is the only object of it's type in the scene.
    46.     this.name="ExistingGameController";
    47. }
    48.  
    49. function StartGame(){
    50.     health  = 100;
    51.     max_health = 100;
    52.     level = 0;
    53.     exp  = 0;
    54.     max_exp = 4;
    55.     //nextlevel.checkpoint = 0;
    56.     Time.timeScale = 1;
    57.     isPaused=false;
    58.     gameState=GameState.running;
    59.     Application.LoadLevel(levelget);
    60. }
    61.  
    62. function QuitGame(){
    63.     //PlayerPrefs.SetInt("Gold",Gold);
    64.     Application.Quit();
    65. }
    66.  
    67. function OnGUI ()
    68. {
    69.     GUI.skin = buttonSkin;
    70.  
    71.     if(gameState!=GameState.showStart){
    72.         GUI.Label (Rect (0,0,200,20), "Health:" + health+ "/" +max_health);
    73.         GUI.Label (Rect (0,20,200,20), "Checkpoint:" +level);
    74.         GUI.Label (Rect (0,40,200,20), "Orbs:"+ exp +"/"+ max_exp);
    75.     }
    76.  
    77.     if(gameState==GameState.showTutorial){
    78.         GUI.Box(Rect( 20,100,530,430),GUIContent ("Tutorial"));
    79.         if (GUI.Button(Rect (25,120,500,400),TTexture[tutorialPage])){
    80.             tutorialPage=(tutorialPage + 1) % TTexture.length;
    81.         }
    82.         if( GUI.Button( Rect( 350,330, 135,45 ), "Continue" ) ){
    83.             gameState=gameStateTemp;// return to the last game state
    84.         }
    85.     }
    86.  
    87.     if(gameState==GameState.showStart){
    88.         GUI.Label (Rect( 350,300, 100,30 ),"Menu");
    89.         if( GUI.Button( Rect( 350,330, 135,45 ), "Start" ) ){
    90.             StartGame();
    91.         }
    92.         if( GUI.Button( Rect( 350,330, 135,45 ), "Tutorial" ) ){
    93.             gameStateTemp=gameState;
    94.             gameState=GameState.showTutorial;
    95.         }
    96.     }
    97.    
    98.     if(gameState==GameState.showPaused){
    99.         GUI.Label (Rect( 350,300, 100,30 ),"Menu");
    100.         if( GUI.Button( Rect( 350,330, 135,45 ), "Continue" ) ){
    101.             gameState=GameState.running;
    102.         }
    103.         if( GUI.Button( Rect( 350,330, 135,45 ), "Tutorial" ) ){
    104.             gameStateTemp=gameState;
    105.             gameState=GameState.showTutorial;
    106.         }
    107.     }
    108.    
    109.     if(gameState==GameState.showEnd){
    110.         GUI.Label (Rect( 350,300, 100,30 ),"You are dead!");
    111.         if( GUI.Button( Rect( 350,375, 135,45 ), "Try again" ) ){
    112.             gameState=GameState.showStart;
    113.         }
    114.         if( GUI.Button( Rect( 350,330, 135,45 ), "Tutorial" ) ){
    115.             gameStateTemp=gameState;
    116.             gameState=GameState.showTutorial;
    117.         }
    118.         if( GUI.Button( Rect( 350,420, 135,45 ), "Quit" ) ){
    119.             QuitGame();
    120.         }
    121.     }
    122.    
    123.     if(gameState==GameState.levelCompleted  levelget < Application.levelCount - 1){
    124.         GUI.Label (Rect( 350,300, 100,30 ),"You won! ... This round");
    125.         if( GUI.Button( Rect( 350,375, 135,45 ), "Next Round?" ) ){
    126.             levelget++;
    127.             Application.LoadLevel(levelget);
    128.             gameState=GameState.showStart;
    129.         }
    130.         if( GUI.Button( Rect( 350,420, 135,45 ), "Quit" ) ){
    131.             QuitGame();
    132.         }
    133.     }
    134.    
    135.     if(gameState==GameState.levelCompleted  levelget == Application.levelCount - 1){
    136.         GUI.Label (Rect( 350,300, 100,30 ),"You've Won Everything. I need to make a harder game.");
    137.         if( GUI.Button( Rect( 350,375, 135,45 ), "Try again" ) ){
    138.             levelget=0;
    139.             Application.LoadLevel(levelget);
    140.             return;
    141.         }
    142.         if( GUI.Button( Rect( 350,420, 135,45 ), "Quit" ) ){
    143.             QuitGame();
    144.         }
    145.     }
    146. }
    147.  
    148. function Update () {
    149.     if(health <= 0  gameState!=gameState.showEnd) gameState=gameState.showEnd;
    150.    
    151.     if(gameState!=GameState.running){
    152.         Time.timeScale=0.0;
    153.         return;
    154.     }
    155.    
    156.     if(Input.GetKeyDown("p")){
    157.         gameState=gameState.showPaused;
    158.         return;
    159.     }
    160.     if(Input.GetKeyDown("h")){
    161.         tutorialPage=1;
    162.         gameStateTemp=gameState;
    163.         gameState=GameState.showTutorial;
    164.         return;
    165.     }
    166.    
    167.     Time.timeScale=1.0;
    168.    
    169.    
    170.    
    171. }
    172.  
    NOTE: Something in your game has to trigger GameState.levelCompleted this can be done like so:

    Code (csharp):
    1.  
    2. GameObject.Find("ExistingGameController").GetComponent("GameController").gameState=GameState.levelCompleted;
    3.  
     
  3. Falin

    Falin

    Joined:
    Sep 29, 2009
    Posts:
    242
    Thanks for your helpful answer.
    I'm going to try implenting this tommorow in my game.

    Yeah, I know I need to learn myself to make understandable scripts