How to pause your game?

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

  1. a1s2d3f4

    a1s2d3f4

    New Member

    Joined:
    Mar 20, 2011
    Messages:
    413
    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

    New Member

    Joined:
    Jan 13, 2011
    Messages:
    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

    New Member

    Joined:
    Mar 20, 2011
    Messages:
    413
    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

    New Member

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

    Akinon93

    New Member

    Joined:
    Jan 13, 2011
    Messages:
    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

    Member

    Joined:
    Sep 19, 2010
    Messages:
    3,791
    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

    New Member

    Joined:
    Jan 13, 2011
    Messages:
    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

    New Member

    Joined:
    Mar 20, 2011
    Messages:
    413
    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

    Member

    Joined:
    Dec 31, 2010
    Messages:
    2,600
    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

    New Member

    Joined:
    Mar 20, 2011
    Messages:
    413
    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

    New Member

    Joined:
    Jan 13, 2011
    Messages:
    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

    New Member

    Joined:
    Nov 13, 2010
    Messages:
    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

    Member

    Joined:
    Sep 19, 2010
    Messages:
    3,791
    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

    New Member

    Joined:
    Mar 20, 2011
    Messages:
    413
    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

    New Member

    Joined:
    Jan 13, 2011
    Messages:
    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. //do the code that should only be done if the game is paused.
    6. }}
    7.  
    and Player.js might look like this, for example.

    Code (csharp):
    1.  
    2.  
    3. function Update(){
    4. if(!isPaused){
    5. //Move around scripts, weapon firing scripts, anything that should happen when the game is -not- paused.
    6. }}
    7.  
    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

    New Member

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