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.
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).
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?
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.
Really? A millisecond? http://pastebin.com/hPZ8KFxa Code (csharp): Simple 'if (boolean)' speed test: 49997853 100,000,000 iterations in 576ms 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
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.
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).
What I do is make a manager class with a static variable isPaused, and check it in every script. Code (csharp): function Update () { if (Manager.isPaused) return; Logic (); }
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?
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.
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.
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!)
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?
refer to my post above. Enemy.js Player.js Control.js control could be something like this. Code (csharp): static var isPaused = false; function Update(){ if(isPaused){ //do the code that should only be done if the game is paused. }} and Player.js might look like this, for example. Code (csharp): var player : Transform; function Update(){ if(!isPaused){ //Move around scripts, weapon firing scripts, anything that should happen when the game is -not- paused. }} 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): Control.isPaused = true; to unpause, just set to false.