3
\$\begingroup\$

I want to know how to make something happen only once like a tutorial in a game which appears only when you first start your game and then when your game got saved to a further point it never appears again even when you close your game and start again.

I want to give players some amount of gold when they first got their hands on my game ... but I do not want to give them that again and again whenever they restart the game ...

So I want to know how to make something happen only once in a game application..

I hope you get my point ... thanks ..

\$\endgroup\$
  • 3
    \$\begingroup\$ I'd use PlayerPrefs. If the entry is blank, or false, do the tutorial, then set it to true. If true, skip it. This also lets the player re-do the tutorial if they'd like by resetting that value (can provide an in-game gui control for it). \$\endgroup\$ – Draco18s Jan 25 '16 at 17:44
2
\$\begingroup\$

You'll want to create some form of persistent data storage. Persistent data, if you're not familiar, is data that can be saved/stored on the hard drive for use in-between scenes, after the game/computer has been shut down, etc. In short, you're writing it to a file of some kind. More info on persistent data can be found here.

As Draco18s mentioned in the comments, your best bet is to store some kind of OfferTutorial variable in persistent data. By default, you'll have this set as OfferTutorial = true.

Once the user has completed the tutorial and earned the reward, you would switch this variable to OfferTutorial = false.

On load, simply check this variable to determine if you want to offer the tutorial to the user.

\$\endgroup\$
1
\$\begingroup\$

It is my normal practice in my every game. First I'd like you to add a small script named Utils.cs made by me that I do use in my every game. No such requirement but easy to handle these kind of stuff. Main benifit of this script is you can use bool in PlayerPrefs through int.

Utils.cs

using UnityEngine;
using System.Collections;

public class Utils : MonoBehaviour
{

    public static void SetInt (string key, int value)
    {
        PlayerPrefs.SetInt (key, value);
    }

    public static int GetInt (string key)
    {
        return PlayerPrefs.GetInt (key);
    }

    public static void SetString (string key, string value)
    {
        PlayerPrefs.SetString (key, value);
    }

    public static string GetString (string key)
    {
        return PlayerPrefs.GetString (key);
    }

    public static void SetBool (string key, bool value)
    {
        if (value)
            PlayerPrefs.SetInt (key, 1);
        else
            PlayerPrefs.SetInt (key, 0);
    }

    public static bool GetBool (string key)
    {
        int val = PlayerPrefs.GetInt (key);
        if (val == 1)
            return true;
        return false;
    }
}

Now in any script that runs only once per game launch (script in splash screen for example), you can handle very first launch like,

SplashScreen script

void Start()
{
    if (!Utils.GetBool ("isAppLaunchedFirstTime")) {
            Utils.SetBool ("isAppLaunchedFirstTime", true);
            //TODO: Set initial coins
            //TODO: Set sound flag to true
            //TODO: Set tutorial flag to true
            //TODO: Whatever you want to do only once
        }
}
\$\endgroup\$
  • \$\begingroup\$ thats a very handy and scalable approach!! \$\endgroup\$ – Yash Kaushik Jan 29 '16 at 21:16
  • \$\begingroup\$ Minor point: Utils.SetBool ("isAppLaunchedFirstTime", true) should be the last thing you do on the special handling of the first launch, not the first. I'd even introduce a separate Utils.GetBool ("needsToRunTutorial") which is only modified when the tutorial is completed or aborted by the player. Unexpected crashes do happen, and no matter how robust our program is, unrelated 3rd party applications can take our application down with them. \$\endgroup\$ – Peter - Unban Robert Harvey Nov 8 '16 at 7:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.