Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to pause your game?

Discussion in 'Scripting' started by a1s2d3f4, Aug 12, 2011.

  1. a1s2d3f4

    a1s2d3f4

    Joined:
    Mar 20, 2011
    Posts:
    415
    Hi, I was wondering if there was a way to pause you game that does NOT involve setting the time scale to zero? I would imagine that it has to be possible since you can pause in the editor.
     
  2. Akinon93

    Akinon93

    Joined:
    Jan 13, 2011
    Posts:
    185
    it's easy really. say I have these scripts.

    Player.js
    MainControl.js
    Enemy.js

    they should speak for themselves, player is the player, enemy is the enemy, and maincontrol is the control of all things that don't relate to other scripts, useful for global variables and such.

    in maincontrol, set a variable isPaused, and make it global.

    then in the other scripts, in the Update functions, make an if statement to check if the isPaused variable in MainControl.js is set to true or false, if it's set to false then run the script, if not then the script wont run (for example, Player.js, the player wouldn't be able to move his character or anything).
     
  3. a1s2d3f4

    a1s2d3f4

    Joined:
    Mar 20, 2011
    Posts:
    415
    I did think about doing that, but I have so many scripts running that it doesn't quite seem efficient. Is there really no way to just write one script that pauses all scripts and animations?
     
  4. diddykonga

    diddykonga

    Joined:
    Jun 9, 2011
    Posts:
    151
    You could edit the timeScale and set it to 0, which would stop the excecution of most things.
     
  5. Akinon93

    Akinon93

    Joined:
    Jan 13, 2011
    Posts:
    185
    he said he didn't want to use timescale.

    that's the only thing I can think of asdf1234. and how many scripts do you have? I highly doubt adding it to your scripts would cause a performance delay or inefficiency, especially considering it can read through the if statement in like a millisecond or less maybe even. if it's set to true, it won't run anything in the if statement, so it completely skips running every line of code in the statement (in this case, your character control, enemy control, etc), if set to false it will run all that just as it normally would.
     
  6. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
    Really? A millisecond?

    http://pastebin.com/hPZ8KFxa

    Code (csharp):
    1. Simple 'if (boolean)' speed test: 49997853
    2. 100,000,000 iterations in 576ms
    3. 173,611 its/ms
    Remembering of course that I'm also:
    • Looping.
    • Reading from a large array.
    • Increasing a counter.

    All in under 6ns.

    So yeah, only off by a few orders of magnitude :p
     
    Last edited: Aug 12, 2011
  7. Akinon93

    Akinon93

    Joined:
    Jan 13, 2011
    Posts:
    185
    lol well I've never looked it up but I knew that computers can read a single if statement really fast, fast enough that it shouldn't make much of a difference.
     
  8. a1s2d3f4

    a1s2d3f4

    Joined:
    Mar 20, 2011
    Posts:
    415
    Ok so i found out that OnApplicationPause() is what causes the game to pause in the editor or if "Run In Background" is not checked on a standalone/webplayer. So, the problem here is: #1Can you call this ingame? #2 if so, is there a way to incorporate a pause menu even if this is on (which is doubtful).
     
  9. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    What I do is make a manager class with a static variable isPaused, and check it in every script.
    Code (csharp):
    1.  
    2. function Update () {
    3.  
    4.     if (Manager.isPaused)
    5.         return;
    6.  
    7.     Logic ();
    8.  
    9. }
    10.  
     
  10. a1s2d3f4

    a1s2d3f4

    Joined:
    Mar 20, 2011
    Posts:
    415
    I'm confused on what this script does. I do know a good amount of javascript but im a bit confused with what you put there. Do i have to reference that in all scripts, or does that one take care of everything?
     
  11. Akinon93

    Akinon93

    Joined:
    Jan 13, 2011
    Posts:
    185
    I think he said what I said before. make a static variable isPaused, and check if it's true or not in all your scripts before running them in the Update function.
     
  12. zine92

    zine92

    Joined:
    Nov 13, 2010
    Posts:
    1,347
    Basically you will need a state manager to set the state of the game, pause/playing or other running states and these are global/static variables because you only 1 instance of these variables So if somewhere in your script, something calls your state manager tell his the game is paused the other scripts will have to also check with state manager if game is running or in a pause state.

    Thats what the script Dman just posted. Basically you add that codes to every of your object in your game to pause running if game is paused.
     
  13. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
    Of course, that's 100% correct.

    The mistake was thinking that 1ms was a small amount of time, in real time software like games 1ms is significant (assuming 30FPS, you've only got ~ 30 of 'em to work with each frame!)
     
  14. a1s2d3f4

    a1s2d3f4

    Joined:
    Mar 20, 2011
    Posts:
    415
    So how can I enable/disable a script from inside the script i'm enabling/disabling? Wouldn't it not be able to re-enable itself since its been turned off? or do i have to make another script to control the script i'm turning on and off?
     
  15. Akinon93

    Akinon93

    Joined:
    Jan 13, 2011
    Posts:
    185
    refer to my post above.

    Enemy.js
    Player.js
    Control.js

    control could be something like this.

    Code (csharp):
    1.  
    2. static var isPaused = false;
    3.  
    4. function Update(){
    5. if(isPaused){
    6. //do the code that should only be done if the game is paused.
    7. }}
    8.  
    and Player.js might look like this, for example.

    Code (csharp):
    1.  
    2. var player : Transform;
    3.  
    4. function Update(){
    5. if(!isPaused){
    6. //Move around scripts, weapon firing scripts, anything that should happen when the game is -not- paused.
    7. }}
    8.  
    the enemy script would look the same as the player script.

    if you want to pause the game, you'd just do this.

    Code (csharp):
    1.  
    2. Control.isPaused = true;
    3.  
    to unpause, just set to false.
     
  16. a1s2d3f4

    a1s2d3f4

    Joined:
    Mar 20, 2011
    Posts:
    415
    Ok, thank you everybody I'll try adding a control script for pausing that i reference in the scripts :)