Need help understanding local vs member variables

So, I’m following along and have attached my code below. It works fine, and I’m getting the hang of the terminal hacker ideas and concepts, but I am a little confused by the use of variables with functions as parameters.

For instance:

void RunMainMenu(string input)

This confuses me a bit and I just need a different explanation of what is going on here. The way I see it, we refactored by creating this function “RunMainMenu” and having “(string input)” states that the function will run and specifically check the string “input” and pass that value along… is that correct?

Any and all help is appreciated!

Code is below

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Hacker : MonoBehaviour
{
// Game state

int level;
string levelName;
enum Screen {MainMenu, Password, Screen}
Screen currentScreen = Screen.MainMenu;

// Start is called before the first frame update

void Start()
{
    ShowMainMenu();
}

void ShowMainMenu()
{
    Terminal.ClearScreen();
    Terminal.WriteLine("Press one of the following number keys to hack:");
    Terminal.WriteLine("1 - Baby Mode");
    Terminal.WriteLine("2 - Man Mode");
    Terminal.WriteLine("3 - Sage Mode");
}

// this only decides how to handle user input, not actually do it
void OnUserInput(string input)
{
    if (input == "menu") // we can always go straight to the main menu
    {
        ShowMainMenu();
        currentScreen = Screen.MainMenu;
    }
    else if (currentScreen == Screen.MainMenu)
    {
        RunMainMenu(input);
    }

}

void RunMainMenu(string input)
{
    if (input == "1")
    {
        level = 1;
        levelName = ", \"easy mode\"";
        StartGame();
    }
    else if (input == "2")
    {
        level = 2;
        levelName = ", \"med mode\"";
        StartGame();
    }
    else
    {
        Terminal.WriteLine("Please choose a valid option");
        currentScreen = Screen.MainMenu;

    }
}

void StartGame()
{
    Terminal.WriteLine("You have selected level "+ level + levelName);
    currentScreen = Screen.Password;
}


// Update is called once per frame

void Update()
{
   
}

}

Hi @Ian95,

Welcome to our community! :slight_smile:

void RunMainMenu(string input) is called a method signature. The method name is RunMainMenu. Obviously. The variable inside the parentheses is called parameter. It exists within the scope of the method code block.

The parameter does not pass anything on. It contains the received data when the method was called.

The method gets called here: RunMainMenu(input);
And the value of input gets passed on as an argument.

In my opinion, it is a bit difficult to see what’s going on here because the names are the same. Here is a simplified version:

string username = "Rick"; // instance member/variable

void Start()
{
    string username2 = "Ben"; // local variable

   DisplayName(username); // this.username where 'this' refers to the instance
   DisplayName(username2);
    
}

void DisplayName (string name)
{
    Terminal.WriteLine(name);
}

That’s all there is to know. Don’t get confused by the many different names. :slight_smile:

Did this help?


See also:

1 Like

Hi Nina, thanks! That is helpful for sure. I think having everything named “input” has been confusing me, but your example helped me to understand the difference and the uses for these methods and their parameters.

I ran that code and can see that both names are output to the terminal. Makes sense.

Thank you for the prompt response!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms