Don't understand what is being explained in the refactoring lesson

Hey guys! Pretty new here.

So I understand the concept of “refactoring”, but within that lesson I am being told that I need to make it so when I make an input in the terminal on the main menu, it takes me to the password screen and thereafter any input should not register as it would if I were still on the main menu.

I get the problem and end goal, but I don’t understand what we’re doing within the code to achieve that. Just to clarify, I think my lack of basic understanding of variables, functions, etc, is holding me back from understanding what is happening. I do have a general idea, but I guess I’m having a hard time holding on to how this all works.

Ex. I don’t fully understand yet why this “void OnUserInput(string input)” is layed out that way. Why is some of it in brackets? How do I know when it’s not supposed to have brackets?

Hi Matthew,

Welcome to our community. :slight_smile:

In C#, the structure for a method signature goes like this:

accessModifier returnType MethodName (parameters)

By default, all C# methods without an access modifier are private. void OnUserInput(string input) was not declared with an (explicite) access modifier, so it is private. private means that this method can be accessed within the same object/instance only. However, Unity had got its own rules, so don’t worry about this.

void is the return type. In our case, it indicates that the method returns nothing. The return; instruction is optional for void methods. Here are two examples:

void SayHello (string name)
    print("Hello, " + name + "!");
    return; // is optional

public int GetSum (int a, int b)
    return (a + b);

void OnUserInput (string userInput)
   print("The player typed: " + userInput);

The method name must be unambiguous. According to the C# coding convention, the preferred pattern for names is “DoSomething”.

Parameters in methods are optional. You can define them to make the method receive data. They act as local variables inside the method code block. The names for the parameters must be unambiguous. (I renamed the parameter of the OnUserInput method on purpose. You can test that in your code.)
When the method gets called, the expected data must be passed on. In the case of OnUserInput, it is a string. We do not call OnUserInput in our own script/object but Ben’s and Rick’s “secret” object does that during runtime when the player typed something.

If programmers followed the official C# coding convention, the On prefix indicates that the method was called from outside when a certain event happens. In the case of OnUserInput, the event is: The player typed something.

And that’s basically everything there is to know about method signatures in C#.

Did this clear it up for you?

See also:

  • Microsoft C# Programming Guide: Methods

Thanks Nina! I’ll definitely keep all this in mind. It’s definitely a lot to get used to, but I guess there’s nothing wrong with constantly looking it up if I keep forgetting haha.

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

Privacy & Terms