I don't know enough to even ask the right questions!

I feel like in this and in the previous lecture I’ve started to get lost. So much so that I’m not even sure of the questions I should be asking. At this point if I were ever to try to recreate this text adventure on my own without these videos I would be completely lost. By this point in the course should I feel like I should be able to recreate what we’ve gone over or is it okay to feel like I have no foundation? Is this leading up to a big “aha!” moment? Why are we creating a formless game object to attach code to? Why can’t we attach our text code to our text box that we originally put all the placeholder gibberish in? Am I supposed to remember all the steps that it took to get there? I’ve literally spent hours going over 10 minute lectures with several pages of notes and I still feel like I’m way behind. What am I missing? Don’t mean this to sound all whiny but I am a bit frustrated… Any help would be great.

1 Like

I don’t know enough to even ask the right questions!

Baby steps :pray: Will_Apple, baby steps. You will get there eventually bro. Besides this course, I suggest you also look around the Unity Docs as @Rick_Davidson suggested in the previous lectures, and the Unity Community for related content. Just to read through or reference inspiration. (Am sure given this question, you are the kind who would do that too.)

I have just finished this Organise State Files lesson, so am estimatedly at the same stage as you are here. And here is my take from it, and from the Unity Docs.


Why are we creating a formless game object to attach code to?

From the Docs, GameObjects “are the fundamental objects in Unity that represent characters, props and scenery. They do not accomplish much in themselves but they act as containers for Components, which implement the real functionality.”

  • Coming from a web background, allow me to compare an empty GameObject to a div, which you can use as a wrapper to nicely organize the items, within a form. Say you want all Text Items in the form to have a specific styling, you could add a style to the container div and reference the text styles from that. However someone else might choose to add the styling classes to each individual Text element and it’s totally fine. Same thing here, you could attach this code to some Text element within the scene and it could work. I might be wrong but I have just tried it and it works! But Having the GameObject sit at the root of the scene, with a proper descriptive name I think is a lot cleaner than adding it to a text element, as the project structure gets larger.

  • Besides since the empty game object starts out simple, without additional properties like Shaders and Renderers etc (Unless if you choose to add them later), it is simply there to hold a reference to our main script in the scene. Keeping it simple, and easy to find.

Why can’t we attach our text code to our text box that we originally put all the placeholder gibberish in?

See, the StoryText with the gibberish is just one dynamic point in this text. What if we wanted to make the title, and maybe some other text items dynamic? Could we keep adding the scripts to each? It would be bloated very fast. Plus the Sates we create make it easy to later find and update your text instead having to dig into the hierarchy and inspector to find what placeholder text you put where and which! It separates concerns like that such that our textual content is not coupled to the game objects. Besides other reasons I am also yet and about to find out later in the course :yum:.

For example in my case, I have modified the Choice to separate UI Text Elements, and added an additional Title for each choice the user makes, having about 5 Text fields dynamically updated on each choice. As you will note the Subtitle Text, Story Text and Option 1 Text, Option 2 Text, Option 3 Text are the dynamic fields in Game ChapterOne script component whose content is updated by the State ScriptableObjects we created separately. Below is the screenshot showing how they are linked.


Text101: Linking the Text objects to script on Game object

Place holder text is as follows…

  • Subtitle Text = intro
  • Story Text = Instructions here...
  • Option 1 Text = 1
  • Option 2 Text = 2
  • Option 3 Text = 3

And am no expert at Unity or C#. I just learnt about ScriptableObjects in this course, a few days ago, so I know it can be done.

State

The Script that we use to create instances of Game States, which will hold our organised text content separately.


using UnityEngine;

[CreateAssetMenu(menuName = "State")]

public class State : ScriptableObject {
    [SerializeField] string storyTitleText;
    [TextArea(10, 14)] [SerializeField] string storyText;
    [SerializeField] string[] possibleOptions;

    [SerializeField] State[] nextStates;

    public string GetStateStory() {
        return storyText;
    }

    public string GetStateTitle() {
        return storyTitleText;
    }

    public string[] GetPossibleOptions() {
        return possibleOptions;
    }

    public State[] GetNextStates() {
        return nextStates;
    }
}

ChapterOne,

Another script I attached to the Game GameObject. @Rick_Davidson named it
AdventureGame in the lecture.


using UnityEngine;
using UnityEngine.UI;

public class ChapterOne : MonoBehaviour {
    [SerializeField] State initialState;
    [SerializeField] Text missionStory;
    [SerializeField] Text missionTitle;
    [SerializeField] Text[] possibleOptions;

    State state;

    void InitializeGame() {
        state = initialState;
        missionStory.text = state.GetStateStory();
        missionTitle.text = state.GetStateTitle();
    }

    void UpdateState() {
        missionStory.text = state.GetStateStory();
        missionTitle.text = state.GetStateTitle();
        string[] optionsInState = state.GetPossibleOptions();

        for (int option = 0; option < possibleOptions.Length; option++) {
            Debug.Log(option);
            Debug.Log(possibleOptions[option].text);
            string optionText;

            if (option < optionsInState.Length) {
                optionText = optionsInState[option];
            } else {
                optionText = "";
            }
            possibleOptions[option].text = optionText;
        }
    }

    void Start() {
        InitializeGame();
    }

    void Update() {
        ManageState();
    }

    private void ManageState() {
        if (Input.GetKeyDown(KeyCode.Alpha1) || Input.GetKeyDown(KeyCode.Keypad1)) {
            state = state.GetNextStates()[0];
        } else if (Input.GetKeyDown(KeyCode.Alpha2) || Input.GetKeyDown(KeyCode.Keypad2)) {
            state = state.GetNextStates()[1];
        } else if (Input.GetKeyDown(KeyCode.Alpha3) || Input.GetKeyDown(KeyCode.Keypad3)) {
            state = state.GetNextStates()[2];
        } else if (Input.GetKeyDown(KeyCode.X) || Input.GetKeyDown(KeyCode.Escape)) {
            Debug.Log("Yooo just quit the game, the coward you are! #Scoff");
            state = initialState;
        }

        // ToDo: Add conditional to only call this on KeyPress.
        UpdateState();
    }
}

So with those scripts… it allows me to create states like the ones in the screenshot below. Note the Text elements and Array of the possible options. Revisit this Lecture and the Text101 Instructor Hangout #1.


Here is the first state loaded into the game, when I click play.


#PS Will attach the project for you later when I get the time if you need further help.
Hope this helps you man. Keep READING.

2 Likes

Privacy & Terms