Search Unity

How to pause the game properly?

Discussion in 'Project Tiny' started by IriySoft, Mar 11, 2019.

  1. IriySoft

    IriySoft

    Joined:
    Dec 21, 2016
    Posts:
    6
    What is the correct way to pause the game made with Unity Tiny that is a Box2D physics game with several colliding objects and such?
     
    osamansr2o1oo and Kiori like this.
  2. Kiori

    Kiori

    Joined:
    Jun 25, 2014
    Posts:
    159
    That's a good question that i'd also like to know, and I'm willing to bet Unity doesn't have a solution for that.
    In fact how do you pause ECS in any game?
    Another related question is, what about pausing the game when it' running in another tab or in the bg(mobile), or when the user alt+tabs, etc.

    In normal html5 games i've made you can do things like have an 'active' boolean, and on the update loop only run all of the systems if active === true. And you can set the tabs(etc.) I mention above with things like blur, focus events.

    So technically if you can simply stop all the systems, everything stops.
    But I'm not really sure how to implement that here, if anyone has a solution I'm interested also.
     
  3. Rupture13

    Rupture13

    Joined:
    Apr 12, 2016
    Posts:
    78
    Having a pause boolean in a system works indeed. And for pausing I would suggest setting every system to pause except for maybe rendering and audio systems. However, I don't think you can override the current Unity Tiny systems to add a pause boolean. That being said, I think you can make dirty workarounds for the systems you can't technically pause.
     
  4. Zoelovezle

    Zoelovezle

    Joined:
    Aug 7, 2015
    Posts:
    54
    I created a static variable isPaused. Every Component/Behaviour update function checks if the game is paused. Thus stopping the execution of further code. I modify these variables when the player wants to pause the game or when the window focus is changed.
     
  5. Rupture13

    Rupture13

    Joined:
    Apr 12, 2016
    Posts:
    78
    @Zoelovezle that only works for your own written components/behaviours/systems right? You couldn't actually intercept something like the code execution for the Tiny built-in physics, right?
     
    Zoelovezle likes this.
  6. IriySoft

    IriySoft

    Joined:
    Dec 21, 2016
    Posts:
    6
    Yeah, pausing the physics is the most important part of my question. Tried to stop scheduler and then unpause from JS, but it doesn't work well. Normal Unity has Time.timeScale that you can set to 0 that effectively pauses physics. Tiny doesn't have it... Hope it's addressed in further updates.
     
  7. Zoelovezle

    Zoelovezle

    Joined:
    Aug 7, 2015
    Posts:
    54
    It's possible. There's a quick hack :p I am working on that ;) . I will update within a few hours.
     
  8. Zoelovezle

    Zoelovezle

    Joined:
    Aug 7, 2015
    Posts:
    54
    After some digging into exported files, I found this line of code which start's the game
    Code (CSharp):
    1. try { ut.Runtime.Service.run(this.world); } catch (e) { if (e !== 'SimulateInfiniteLoop') throw e; }
    I tried initially by referencing the world and destroying it by ut.Runtime.Service.destroy() and re-run when you want to unpause. For some reason even if this worked, this caused all the update functions to run twice a frame.

    Finally, after more digging, I found pause, isPaused, and resume methods and many more from the scheduler.
    Code (CSharp):
    1. this.world.scheduler()
    ;)
    This way you can just stop all the scheduler by scheduler.pause() and scheduler.resume() :p
     
    Rupture13 likes this.
  9. Maras

    Maras

    Joined:
    Dec 11, 2012
    Posts:
    44
    Does Tiny already fully support Box2D or you used some workaround for that? Sorry about off topic
     
  10. Rupture13

    Rupture13

    Joined:
    Apr 12, 2016
    Posts:
    78
    @Maras
    Depends on what you mean by "fully support", but yeah there are RigidBody2D and RectCollider2D components that work well.
     
  11. Kiori

    Kiori

    Joined:
    Jun 25, 2014
    Posts:
    159
    Did you test if this works, with a physics system active, like the op said?
     
    Zoelovezle likes this.
  12. Zoelovezle

    Zoelovezle

    Joined:
    Aug 7, 2015
    Posts:
    54
    it does :)
     
    Kiori likes this.