Confusing lecture

Insanely confusing, watched 2nd part of video 4 times, still don’t understand 80% of it. Feel like a monkey typing text and listening to some alien explanations that are supposed to make it clearer, but don’t. Went from 5 mph in all lectures before this to mach 3.

3 Likes

Hi Seventh_Nexus, welcome to the community :slight_smile:

There is a theory that, given a room full of monkeys and typewriters, given enough time, they would be able to produce the entire works of Shakespear - if that could be true, then I’m very confident you’ll be able to grasp whichever part of this lecture you found more challenging.

What was the specific thing, or things, which you were having trouble with - lets see if we can help :slight_smile:

2 Likes

[CreateAssetMenu(menuName = “State”)]

means let me create objects in editor named “State”. Ok.

public class State : ScriptableObject {

So private means the class in only accessible within the class? Says something like this in the video. How does that make any sense? It’s only accessible within itself?

And does public mean it’s accessible to other classes in the script, or other scripts as well? Unclear form the explanation.

SciptableObject - means that it does not have to be attached to game object to execute? Ok, so what? Why is that relevant? Don’t understand.

public string GetStateStory()

So my script (State.cs) that is not attached anywhere, upon execution, will run a method, that will “return” “string” type data (because it’s not void)… back to… where the method is called from?

Where is the method called from? Nowhere yet? Will it be called from somewhere in the future?

return storytext;

So is this referencing what specific string it will return? Not clear.

[SerializeField] State startingState;

Ok so this means: let me attach “state” type objects in editor. startingState just an arbitrary name?

Wait what. Wasn’t StartingState the name of the object that I created before? Is this related / relevant? Confusion.

“We need a way to continuously know what state were in”
What, why? Don’t we only have 1 state right now? Who is “we”? Me? I know what state is displayed on screen. Does it mean some script needs to know what state were in? Which script? Why does it need to know?

“We’ll create a variable of type “state””

What???

So what’s “state”?

Is it my script state.cs?
Is it a variable type in Csharp?
Is it a game object?

“…and it will be named state”

:D:D:D:D

I feel like the video is making fun of me.

So we have objects in editor named state, we have State type variable, that is named… yes, state. And there is also a script, named State. Oh, and let’s not forget the Starting State object we already created, that is attached to… The Staring State property of Adventure Game script.

“We are deriving the (state) type from the class”

What? What’s a type? Is it the same like an int, string, etc? Confused.

And you can derive types from classes? How does that work?

This is how it feels at 8 minute mark going through this video.

Maybe tomorrow after good nights sleep, after watching the video another 10 times in slow motion, making some flow charts, commenting // every word in every script to explain to myself what it does my tiny brain can start to grasp this absolute confusion.

3 Likes

I like that you feel lost but can still break the problem down into specific questions. You’re going to do just fine at this.

Let’s start here, because it’s easy. The information above is for public and private methods, not classes. When working with Unity, all classes are public. Always.

This means your classes can be seen by all other classes and by Unity itself.

3 Likes

Thanks! Rewatching video again, was just about to edit this out of my question.

1 Like

Hi,

means let me create objects in editor named “State”. Ok.

It’s just the name of the menu item associated with the class you are creating,

public class State : ScriptableObject {

So private means the class in only accessible within the class? Says something like this in the video. How does that make any sense? It’s only accessible within itself?

And does public mean it’s accessible to other classes in the script, or other scripts as well? Unclear form the explanation.

Classes, variables, properties and methods all have what are referred to as Access Modifiers, there are several, but the ones you are typically seeing at this stage of the course are public and private.

In C# the default access modifier is private, if an access modifier isn’t specified it will use the default. You tend to spot this when you look at how the variables are being created in the lectures, you’ll see some where the access modifier isn’t specified, as such, it will be _private.

In the example you’ve given, the class definition is actually public, not private, e.g.

public class State : ScriptableObject

Classes will always be public or internal, unless perhaps you have a nested class, but this is outside of the scope of the course at this time (so don’t worry about it). If they weren’t, nothing else would be able to see/use them.

The member variables you add to a class also use access modifiers, here’s an example;

public class Example : MonoBehaviour
{
    public string name;
    private int ageInYears;
    int ageInDays;               
}

In the above, there are three member variables, they are members of the class Example. name is a public variable, as such, other classes will be able to access this variable directly through an instance of the class, this can be dangerous. As an example, if I were to create an instance of this class and set the name variable to be “Fred”, any other class can access that instance of the class and change the name to something else, like “Rob” for example, without any checks/balances.

The ageInYears variable has a private access modifier, nothing outside of this class will see this member variable, any other members of this class, such as properties or methods will have access to this member variable.

The ageInDays variable is also private, whilst the private access modifier hasn’t been specified, it inherits the default, so again, only other members within this class would be able to access it. Externally, neither ageInYears or ageInDays would be visible.

SciptableObject - means that it does not have to be attached to game object to execute? Ok, so what? Why is that relevant? Don’t understand.

ScriptableObjects are effectively data containers. They can be saved as assets which allows us to instantiate them easily within Unity. In the case of Text101, this allows you to create a series of states, parts of your story, in advance and save them as assets, loading in the relevant asset as and when needed. They all use the same template, the State class to define them.

Normally, for a script to execute it has to be attached to a GameObject in the scene, this is one of the differences with a scriptable object.

public string GetStateStory()

So my script (State.cs) that is not attached anywhere, upon execution, will run a method, that will “return” “string” type data (because it’s not void)… back to… where the method is called from?

Where is the method called from? Nowhere yet? Will it be called from somewhere in the future?

States.cs is not attached to a GameObject in Text101, correct. You do however use it within AdventureGame.cs, you declare a variable named startingState from memory, which is of type State. Within the Inspector you drag in your first State, this instantiates the State asset you have dragged in and creates the reference.

Your class doesn’t just “run a method, that will return string data”, it has to be called. You do this from within AdventureGame.cs in the Start method, you’ll note this line of code;

textComponent.text = state.GetStateStory();

In the above, you have a reference to the UI Text GameObject named textComponent, you access its text property and set it equal to the returned string from the GetStateStory. GetStateStory is a method within the State class and is available via the state variable (and instance of the State class).

return storytext;

So is this referencing what specific string it will return? Not clear.

In this example, storytext is your member variable within the State class, the method GetStateStory has access to this member variable. The text which is returned will be whatever the storytext variable is storing.

[SerializeField] State startingState;

Ok so this means: let me attach “state” type objects in editor. startingState just an arbitrary name?

Not quite. The [SerializeField] attribute exposes fields (member variables) which are otherwise not visible within the editor. You’ll note that there is no access modifier, as such it defaults to being private, a private field would not be visible within the editor. [SerializeField] enables you to retain the field as private, so it isn’t accessible by other classes, but at the same time expose it in the editor so that you can access it directly.

startingState is the variable’s name, you could call this something else of your choosing. Perhaps initialState for example.

Wait what. Wasn’t StartingState the name of the object that I created before? Is this related / relevant? Confusion.

It might have been yes, in the same way that my name is “Rob”, there are many other objects in this world called “Rob”. The difference though is that you have an asset named “StartingState” and a variable within a class name “startingState” - these are different things, and both could be renamed as you wish.

We need a way to continuously know what state were in”
What, why? Don’t we only have 1 state right now? Who is “we”? Me? I know what state is displayed on screen. Does it mean some script needs to know what state were in? Which script? Why does it need to know?

I appreciate you are frustrated so perhaps consider this as “Our game needs a way to know which state we are in”.

In the game, this would effectively be the AdventureGame.cs script as it is the one which is running the game for all intents and purposes.

“We’ll create a variable of type “state””

What???

So what’s “state”?

Is it my script state.cs?
Is it a variable type in Csharp?
Is it a game object?

“…and it will be named state”

When you create a class you are definning a data type, often referred to as a type. Thus, your class State is a type.

In this line we declare a variable;

private State state;

We declare the access modifier (“private”), then the type (“State”) and finally we give the variable a name, in this case “state”. The type is more relevant than the name, you could call the variable something else like “storyPart” for example, but where-ever possible make sure that the names are meaningful;

private State storyPart;

Hope the above helps, if there is still anything you are not clear on let me know. :slight_smile:


See also;

3 Likes

Normally, after we create a class, you can create instances of a class to work with in the game. For example, if you have an Airplane class with a “speed” variable, you can create several instances of this class to make several airplanes, each with their own speed value.

There are several different ways of creating an instance. For example, if you drag an Airplane object into the heirarchy, it will create an instance of that airplane when the scene loads. There are also ways to instantiate things through code while the game is running.

What the ScriptableObject code (along with CreateAssetMenu) is it lets us create instances of something ahead of time and just store it as a resource in our assets area.

When we do this to State, we can then create many different states. Each one has its own data in its own variables, which we can now edit through the inspector to have states ready before the game even starts. Since the states are ready and the instances are already created, we can start putting them in variables, such as currentState.

All variables hold information, but each variable must be declared with the type of information it holds. If I declare an “int” variable, then only integers can go in there. I can’t put my name in there, for example. The type of information must match the type of variable.

Classes are also types. Variables of a type matching a class hold a reference to a class or a specific instance of that class. You can think of this like a name.

If I say “I love my Chester. I take him out and wash him every day.” that statement makes some sense if we know Chester is a beloved pet. But if we are under the impression that Chester is the name of this person’s boss, the statement seems pretty confusing and/or disturbing.

So it’s important to declare what kind of variable Chester is. In this casr, Chester is an Airplane. C# recognises that we have a public Airplane class in Airplane.cs and makes that type available for our use.

So now that we know Chester is an Airplane, we can put the statement in better context but we still don’t know which airplane it is. We will need to find a specific instance of Airplane and assign it to our Chester variable.

Using ScriptableObject lets us create an instance ahead of time and [SerializeField] lets us assign it to a variable from the inspector. This lets us set it up and edit all its information before the game even starts.

This is perhaps more useful for States because we only have so many states in the game and we won’t need to create more while playing. Also, because states are simple scripts that just hold some text and don’t need multiple game components (such as position data, images, sound, physics, etc).

3 Likes

So I think the reason this part is confusing is because the string hasn’t been initialized with any value yet. That is, you declared a string variable called storyText and you have a method that returns its information, but there’s no information assigned to storyText anywhere in your code.

Since storyText has the “[SerializeField]” attribute, you will be able to edit the value of storyText within Unity for each individual instance.

This is actually the awesome part. Once we have all this code in place, you’ll be able to go in and edit all this text later independently without touching the code anymore. This will let you focus on writing once the coding us done and will be especially helpful if you want to team up with someone who doesn’t code.

It is this value edited from Unity later on that will be returned whenever the GetStoryText() public method is run for that particular instances.

3 Likes

Yes, it will return a string value in place of the method call. So it’s like substituting “GetStoryText()” with that string.

It’s not going to be called from State.cs, but since GetStoryText() is a public method, it can be called from any other script anywhere in your code.

It’s eventually going to be called from AdventureGame.cs, which will access the GetStoryText() method for a particular instance so that it returns that instance’s storyText to AdventureGame.

AdventureGame is going to take this returned string value and assign it to a textComponent so that it shows on the screen to the player.

edit:
((Apparently there’s a limit to how many replies I can make. Hopefully you’ve gotten all the help you need, but if you have more questions, feel free to make individual topics for them.))

3 Likes

It’s part of the forum’s spam prevention, you can just edit a previous reply instead of creating multiple-consecutive ones. If you were doing so to provide some degree of separation between the relevant parts, consider using a horizontal rule, or, like your Vector Math topic, headings/subheadings etc. :slight_smile:

Ok, I went through the video few more times, looked more closely at the code and read your answers . Thanks a lot, the majority of it is much more clear now. :bowing_man:

Probably the last part that I am confused about is:

textComponent.text = state.GetStateStory();

textComponent.text - this is clear to me.

GetStateStory(); - is a method, all clear.

“state” is a variable, of type State (same type as the method mentioned above).

Why is the method referenced in such an awkward way, through a variable?

Was the “State state” variable created just for the purpose of referencing the “state” type “GetStateStory” method?

1 Like

textComponent is the variable that you defined at the top of your class, you have exposed it using [SerializeField] so that it can be seen within the editor. You then dragged the UI Text GameObject into this exposed field to create a reference.

.text is a property of the UI Text GameObject, it will be the text which is displayed to the screen.

So, in layman’s terms, “Here’s the UI Text object in the scene I want to update with the story text”.

Why is the method referenced in such an awkward way, through a variable?

Sorry, which method are you referring to now? the GetStateStory method? Assuming so, by awkward, do you mean because you have to call it like this;

state.GetStateStory();

If so, it’s because it is a member of the State class and you are accessing it via the instance of that class, your state variable.

Was the “State state” variable created just for the purpose of referencing the “state” type “GetStateStory” method?

Yes. Unless a method is static it needs an instance of the class in order to access it. In this case, you created an instance of the State class and reference it via the state variable.

Hope this helps and I’m glad you’ve managed to move further forward with the course :slight_smile:

1 Like

Ok, now the lesson is 100% clear!
Thanks again Rob and Anthony :slight_smile:

1 Like

You’re very welcome :slight_smile:

this is the first real heavy lecture in terms of the size of information and tweaks needed to create and manage a scriptableobject and yet its not organized as it should be … for someone new to understand the lecture it will be super confusing besides we need also to know how to do the right thing in the correct order and the reasons why we crated every term and what its functionality … im just saying you did a very nice lecture but it needed to me more organized for the new comers to understand easily what is this all about …and thanks for your efforts Sir

Hi,

It might be worth raising this on the Q&A where one of the student instructors can raise it for one of the instructors attention. If a number of people are having issues with this lecture then perhaps the instructor(s) will be willing to made an update/additional lecture to further explain.

It is unlikely that the instructors will see such things if only posted on the forum.

Hope this helps :slight_smile:

I totally agree with you. I watched this lecture a number of times and it still didn’t make much sense to me.

This thread did clarify a few things for me.

Thanks!

-Amanda

Privacy & Terms