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

Basic game mechanisms (lives, continue, scene loading, pause...) best practices?

Discussion in 'Scripting' started by wondersonic, Aug 29, 2012.

  1. wondersonic

    wondersonic

    Joined:
    Jan 8, 2012
    Posts:
    62
    Hello,
    I'm looking for your best pointers to implement these basic game mechanisms in an efficient and elegant way:
    - player lives
    - multiplayer (being several on one computer or several over a network, say up to 10 people)
    - game continue
    - game pause
    - scene loading (with all the little tricks to know: static variables...)

    Thanks in advance for your feedback!
    WS
     
  2. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    Documentation and basic programming knowledge is all you need to complete these simple tasks..

    For lives:
    store a var named lives(or something appropriate)
    when a condition is met, remove or add a live.
    if lives >= 0 then game over, or another event..

    multiplayer:
    you will need a server, or use another service, if you cannot program gameplay at all then multiplayer is going to be difficult to sort out..

    game pause:
    set the games timescale to 0

    game continue:
    set the games timescale to 1.

    note:
    setting the timescale is a quick and easy way to pause, but can be problematic at times..

    scene loading:
    use: Application.LoadLevel("Scene Name Here");
     
  3. wondersonic

    wondersonic

    Joined:
    Jan 8, 2012
    Posts:
    62
    Smitchell, thank you for your reply :)

    Ok, assume I'll create one unity scene per each game level. First is it a best practice?
    If so, lives will be a variable that must be passed from scene to scene, right? How do you do that? I imagine static C# variable can do the thing but how does it work? Do you create a monobehavior and attach it to the Camera? Do you create a static class? (does that exist?)

    Now of course, based on some events, I'll increase or decrease this number and act accordingly (game over if lives < 0).

    Now about multiplayer, meaning (several people playing on the very same computer, not LAN/network multiplayer), what kind of structures would you use? a static array of PlayerInfo object? Do you have web links to such tutorials?

    Ok and what about scripts that should continue to work while timescale=0? Any best practices? I would say, any secret scripts?
    Again, something related to scene loading and context passed from one to the other (for continue management).

    Best practices? What about DontDestroyOnLoad? I've of course read the doc, but I wish real life usage and best practices according to your experience :)

    Thanks a lot in advance :)
     
  4. fano_linux

    fano_linux

    Joined:
    Jan 1, 2012
    Posts:
    909
    Don't use timescale = 0 if your unity is greater than 2, use timescale = 0.001;

    I would recomend for pause game that every script has a message receiver that pause it's objects, in other words every object in scene must have a script controller, and when the master controller pause the game, all scripts receive that message and use it's pause method to stop that particular object.
     
  5. wondersonic

    wondersonic

    Joined:
    Jan 8, 2012
    Posts:
    62
    Thanks for the precision. That's exactly the kind of information I'm looking for!
     
  6. fano_linux

    fano_linux

    Joined:
    Jan 1, 2012
    Posts:
    909
    you are wellcome..

    For lives, i think you can do in many ways.
    1 - Store the character status (mp, hp,exp whatever) in files (xml, txt) or database such as sqlite to recover that info when the game loads another scene.
    2 - Don't destroy the var lives that you are using. (but if the game exit by an error, or somenthing else, you will lose that information.
    I think you can do it as you need, i'm presenting not the solution, i'm presenting ways for you to think of what is the best for you.

    For scene loadin, you will need to create a singleton class to controll all the actions of loading scene. Why singleton? Well let's say there's a particular script that kill your char when ther's a collision, so that script can tell your main script to reload the level by singleton, example:

    Code (csharp):
    1.  
    2. class MasterScriptScene
    3. {
    4.    public static MasterScriptScene Instance;
    5.  
    6.    void Awake()
    7.    {
    8.       Instance = this;
    9.    }
    10.  
    11.    public void ReloadLevel(int level)
    12.    {
    13.        //..... reload level logic
    14.     }
    15.  
    16.    public void ReloadCurrentLevel()
    17.    {
    18.        //..... reload level logic with the currentlevel static var (no need parameters)
    19.     }
    20. }
    21.  
    Code (csharp):
    1.  
    2. //Script at a gameobject that kill the player and reload the level
    3.  MasterScriptScene.Instance.ReloadLevel(1);
    4.  
    Just a tip: Use at the MasterScriptScene a int var by static way to hold the current scene, so in this case i'm showing to you you would call like this:

    Code (csharp):
    1.  
    2. MasterScriptScene.Instance.ReloadCurrentLevel();
    3.  
    You should try some ways, sometimes we have fears and keep wondering if it will really work or not. Just start testing it and take your own conclusion,
    there's no right way, for example footsteps audio, you will find many of many ways to do the same thing. So again the best way is the way that fits your project and make you confortable.

    Best of luck
     
    Last edited: Sep 3, 2012
  7. arkon

    arkon

    Joined:
    Jun 27, 2011
    Posts:
    1,122
    For lives, scores and anything I want to survive from scene to scene the method I use is to create a dummy first scene that doest nothing much
    except create a few singletons, and then imidiately load the first scene the player sees. For example a score.cs file that creates a score singleton that has a dontdestroyonload command in it's Start function. Now on each scene I Score.GetValue() Score.AdjValue() etc.

    When I want to store a variable to survive a power down or reset, I store it in PlayerPrefs.
     
  8. Vanamerax

    Vanamerax

    Joined:
    Jan 12, 2012
    Posts:
    938
    It is also worth to take a look at Burgzergarcade's tutorials if you don't have a hard time with basic programming syntax ( yet alot of basics to more advanced programming techniques are used and discussed ) It really helped me understand the programming flow and how to sort things out. He shows you how to setup your player stats, interaction, scene loading, inventory etc (you can also skip things you dont need, but alot of scripts are reused/modified throughout the series)

    here is a link: http://www.youtube.com/user/BurgZergArcade

    I'd recommend to start with the Hack slash tutorial serie part 1
     
  9. wondersonic

    wondersonic

    Joined:
    Jan 8, 2012
    Posts:
    62
    Great tips! Thanks!!!
     
  10. wondersonic

    wondersonic

    Joined:
    Jan 8, 2012
    Posts:
    62
    Thanks for the link, indeed, very interesting videos!