[Solved] How to record and read the number of times that an option being chose by the player in the game?

Hi:
I’m developing my own text adventure game . In part of my game, I’m going to offer 5 different options for the player to choose, those options will lead the player to 3 states: WIN, WRONG or LOST. Only one option(right option) will lead the player to the WIN state, the other 4 options will lead to WRONG state. The player will only have 3 chances to pick the right option to win. If the wrong options was selected over 3 times, the player will be led to the LOST state.

My question is how can i realize that if the player choose wrong option over 3 times, the state will lead to the LOST state but not the WRONG state. I guess it is about recording and reading the number of times that the WRONG state method being called, but i don’t know how to do.

I use google searched for an answer, and tried to follow the example “enter the wrong password 3 times will lock the account”, but it doesn’t work. Here is my code:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class NewBehaviourScript : MonoBehaviour {
public Text text;
private enum States {StartState,Options, Wrongoption,WrongOption3,CoretOption }
private States mystate;

// Use this for initialization
void Start () {
    mystate = States.StartState;
}

// Update is called once per frame
void Update () {
    if (mystate == States.StartState) { StartState(); }
    if (mystate == States.Options) { Options(); }
    if (mystate == States.Wrongoption) { Wrongoption(); }
    if (mystate == States.WrongOption3) { WrongOption3(); }
    if (mystate == States.CoretOption) { CoretOption(); }
}

void StartState()
{
    text.text = "Let's begin";
    if (Input.GetKeyDown(KeyCode.Space)) { mystate = States.Options; }
}
static int x = 0;

void Options()
{
    
    text.text = "Please select the right option, you have three chances: \n" +
                " 1. 123\n" +
                " 2. 456\n" +
                " 3. 789\n" +
                " 4. 468\n" +
                " 5. 165\n" +
                "Please use keypad number to choose the correct password option";
    if (Input.GetKeyDown(KeyCode.Keypad1)) { mystate = States.Wrongoption; }
    if (Input.GetKeyDown(KeyCode.Keypad2)) { mystate = States.Wrongoption; }
    if (Input.GetKeyDown(KeyCode.Keypad3)) { mystate = States.Wrongoption; }
    if (Input.GetKeyDown(KeyCode.Keypad4)) { mystate = States.CoretOption; }
    if (Input.GetKeyDown(KeyCode.Keypad5)) { mystate = States.Wrongoption; }
    if (x >= 3) { mystate = States.WrongOption3; }
}


int Wrongoption() {
    text.text = "Wrong Option";
                   "Press R to return";
    if (Input.GetKeyDown(KeyCode.R)) { mystate = States.Options; } // jump to WrongOption3 not the Options state;
    x++;
    return x;
    
}
void WrongOption3() { text.text = "You Lost"; }
void CoretOption() { text.text = "You Win!"; }

}

=================================================

In Wrongoption() function, if i press R to return, it directly jump to the WrongOption3 state even it is my first time to choose the wrong option. It didn’t do any counting.

I’m using Unity 5.4.1f1, and Visual Studio Community 2015 V14.0.25123.

I really appreciate if any one can help me to solve this problem. Thank you.

A couple of thoughts spring to mind looking at your code…

First thought is, why the use of the static on the int x variable?

Second thought is, how many times within an Update() call is the keydown being detected and therefore incrementing your variable?

An easy way to determine the second thought above would be to pop a Debug.Log("Wrong Guess Detected " + x + " times"); statement in the Wrongoption() method, before you call return x;.

See what appears in the console - I wonder whether you have perhaps already exceeded the value of 3 before you even get a second chance at entering the code.

Hi Rob:
Thanks for your reply.
About your first thought, i’m newbie at C# programming, i’m not really sure the purpose of using the static on the int x variable, as the answer I found didn’t give an explanation. I guess its original purpose is stay in memory to detect if the method being called, but I found that, in my case , it won’t matter if i put the static keyword on it or not. I should be more careful when i’m using something i don’t know, thanks for your reminding.

About your second thought, yes, it is very stupid mistake i made, even there is “// Update is called once per frame” warning on it . I guess I should not put the increment in the Wrongoption() but the return function body. I got this one: I put the increment within the R keydown method body. the other code remain the same. As follow:

int x = 0;
void Wrongoption() {
text.text = “Wrong Option”;
if (Input.GetKeyDown(KeyCode.R)){ mystate = States.Options; x++; }
if (x == 3) { mystate = States.WrongOption3; }

}

It works just fine.
thanks for your help.

1 Like

Hey @Ben_Wang087,

By using the static modifier on your variable you are stating that the it belongs to the Type rather than the instance of that Type.

There are times when this can be useful, and times where it’s incorrect usage will be problematic.

If you consider a class, lets call it LevelManager, which is instantiated when a game loads, and lets say it is responsible for the loading of levels within a game.

If we store the level number in a static variable, and use this to load the next level, after the player has used up all of their lives and we start again, instead of loading the first level, it takes us back to the level number that was stored within the static variable - because when the instance of the LevelManager was destroyed at the end of the game, the value of the static variable is still held against the class (Type) LevelManager.

If the intention was to make the player start at the beginning again, we have a problem. Equally, you may want to be able to offer the player an option to return to the last level that they played, in which case this could be useful.

The above is not really a concrete example of good design, but just an example of how you could do something using a static variable and how it may, or may not, be useful.


See also;

1 Like

Hi Rob:
Thank you for explaining the static modifier thing and you explain it very clear. I will keep it in mind and i’m sure it will help me in my further studying.

1 Like

You are more than welcome @Ben_Wang087, anything else be sure to post, there’s lots of friendlies here who are always willing to help.

I shall look forward to seeing your take on each of the games on the course :slight_smile:

1 Like

Privacy & Terms