Can var stand for an array? (Unity 2D course)

I have a question about the Unity 2D course on the lecture with the title: Manage Next States.
I get confused at minute 2:20 when the instructor writes down the variable of type var on line 27. So far I understood that the variable var was a type of variable that you can use when you don’t want to specify the exact variable, so it’s there just to be used if you want to. In fact, the instructor says that var can be replaced with State. I don’t get why it’s possible to use var instead of State.
If state.GetNextState (in the AdventureGame.cs script) means that we are calling the public method GetNextStates from the State.cs script, which returns the array nextStates, how is it possible that a variable can now store an entire array of the current state?

It would be like writing down this:
the variable called nextStates has the array (from the other script) of the state we are currently in, assigned to it.
Now I’m asking myself: how is it possible that we’re taking the array from one script and putting it into a variable which has the same name as the array in the other script?

Using the array of type State would then make more sense because you would be storing an array into an “empty” array with the same name (nextStates).
Is var maybe being converted to whatever type it is equal to?

As you probably can tell I feel a bit frustrated now. So far I think I understood everything well, except some minor things that are still a bit confusing.

I think I’m asking myself these question because I misunderstood something.
I hope I was clear enough to get an answer I can work with.
If I wasn’t clear it would be enough if you could just explain to me what the lines 25-28 actually mean.
Thanks in advance!

1 Like

I was asking myself the exact same thing.
And this is how I believe it works (I may very well be wrong as I’m new to coding):

The states.cs file class is a Scriptable object, which is I believe, why you can access the array from your AdventureGame.cs file.
This as I understand it is because a scriptable object is treated as an object but cant be added to the scene like other objects in Unity like a 3D model or a 2D image for example. (See the links I pasted at the end).
And the variable state doesn’t store the whole array if i understand this correctly, it gets assigned “Startstate” in AdventureGame.cs and then the value of the state variable gets updated in the ManageStates function to the value of “NextStates”.

I hope this is correct and that it helps.
maybe someone more experienced can let me know if my explanation is correct or not.


Thank you for your answer!
I read a few things on the pages you recommended to me, but I don’t think that’s the place where I will find the answers I’m looking for.

At this point, I’m really getting confused.
Maybe I should have started talking about the

“minor things that are still a bit confusing”

before asking for an explanation of my question.

I’ll try to explain my doubts:

Because we named our script State.cs our custom class (a class we created and named as we want) is called State (in the State.cs file), right?

public class State : ScriptableObject

In fact, when I hover over State with my mouse it says: class State
Now comes the thing I don’t understand:
What does State[] nextStates; mean?
Is State[] a variable, an array or a class? When I hover over it, it says again class State.
From what I understood State[] is an array because there are square brackets after State. The problem is that I thought that the syntax to declare an array was like that:

anyTypeOfVariable[] theNameOfYourArray;

State is NOT a variable from what I know so far, so the syntax would be wrong and there should be some problems in the code. Even when I hover over State[] it says: class State, as if it would be possible to use not only variables but also entire classes as an array type…
But why does it work?

When I’m in the AdventureGame.cs script and I hover over State it says again: class State, as if we called the class from the State.cs script. Is that true? Did we call the class and changed the name to startingState?
Right after the line with [SerializeField] State startingState; there is another line which I therefore not understand: State state;

When all this has become clear to me, I’d have a chance to figure out why we’re using var instead of State[], which, as I said, is something I do not understand.

Forgive me if everything I’m saying is wrong, I never touched a programming language before.

As you said, we created a class named State. In C#, when creating a class, it automatically becomes a type as well. When we declare a variable, we need to declare it with a type.

[] indicates that we are dealing with an array. It’s the C# syntax. Somebody a couple of decades ago decided that this is how we declare an array.

State[] nextStates means: We declare a variable named nextStates. That variable can hold a reference to an array object with State elements in the array.

State[] a variable, an array or a class

It’s never a class because we are dealing with objects created from classes. The Array class/object is one of the oldest built-in types in C#. State[] is neither an object nor a class, it’s a type. A type just tells the computer of what type the object is. An object is a collection of data in memory. Regard it as some wrapper or category or something like that because in the memory, you just have millions of 0s and 1s that don’t have an inherent meaning.

Did we call the class and changed the name to startingState ?

Fortunately, it’s less complicated. :slight_smile:

When declaring a variable, we need to give it a name. Whether the name is startingState or blahblah does not matter as long as the name is unambigous.

The compiler replaces the var by the actual type. I, personally, don’t use var because prefer an explicite type declaration. It makes code easier to read for me, and I don’t have to guess when reading more complex code. It’s a matter of personal preference, though. Other programmers prefer var.

Thank you a lot!

So, if I’m not mistaken, as soon as you create a class the name of the class can be used as a type of variable.

A question: does the class need to be public, so that I can access the variable type from another script, like in our case?

Anyway, in this case, I think the variable of type State can store ScriptableObjects, right? What else can it store?
And thus, because State is a type of variable, we can use that type of variable and make it an array by adding the square brackets to it. And as long as you are using a type of variable and adding square brackets to it, you can call that an array, right?

In C#, when creating a class, it automatically becomes a type as well.

But, does that mean that I could use the variable type AdventureGame in my script? What would that type of variable store or hold?

Next Question:

The compiler replaces the var by the actual type.

How does the compiler know what the actual type is?

var nextStates = state.GetNextStates(); 

My guess is that, in this case, State[] is the only possible type of array you can put behind nextStates, because you’re essentially assigning the State[] array named nextStates (from the State.cs script) of the variable of type State named state (the state you’re currently in) to an array which has to be of the same type, therefore of type State[].
So I think you can’t assign a type of variable or array to a different type of variable or array, right?

These examples wouldn’t work then:

string nameOfMyStringVariable = "John";
int nameOfMyIntVariable = nameOfMyStringVariable;

This wouldn’t work because you can only assign an int to an int and not an int to a string type.

int[] nameOfMyIntArray;
string nameOfMyStringVariable = nameOfMyIntArray;

This wouldn’t work because you cannot assign a variable to an array. In our case, we assigned an array to an array of a variable (if I understood correctly what the . means)

int[] nameOfMyIntArray;
string[] nameOfMyStringArray = nameOfMyIntArray;

That wouldn’t work either, because you can’t assign a string[] to an int[].

Besides all that, I wasn’t that far from the truth when I said:

Is var maybe being converted to whatever type it is equal to?

or when I asked:

Can var stand for an array?

The last question for this reply:

var nextStates = state.GetNextStates();
is nextStates just the name of the array of type var or are we calling nextStates from the State.cs script?
What I mean exactly: in the State.cs file we have an array of type State[] named nextStates. In the AdventureGame.cs script we have an array of type var (or more explicit: State[]) also named nextStates.
Are they the same or would it be possible to change the name of one of them?

They have the same structure, but I think that, as we are not somehow calling nextStates from the State.cs script, the two are independent of each other. They just share the same name, without having a connection between scripts.

Please tell me if everything I said was wrong so that I can be more confident while absorbing all the information on the course.

That’s correct. :slight_smile:

Good question. Private classes would not make sense as they wouldn’t exist in any scope and could not be accessed. The only exception are nested classes (= classes defined in another class). Also see this discussion.

AdventureGame is a reference type. The default type of a reference type is null. For further information on reference types and value types, please see here. It’s sufficient to learn the value types. Everything else is a reference type.

How does the compiler know what the actual type is?

Because we assign a value to the variable. var n; would be invalid because without any context, the compiler cannot know what to do with the variable. var n = 6; would create a variable of type int.

GetNextStates() returns an object of type State[], thus the compiler knows that var is actually State[].

string nameOfMyStringVariable = "John";
int nameOfMyIntVariable = nameOfMyStringVariable;

This won’t work because the types cannot be implicitely converted. There are types that you can convert implicitly, for example:

int n = 1;
float f = n;

You could explicitely convert with certain methods such as ToInt32().

string nameOfMyStringVariable = "John";
int nameOfMyIntVariable = nameOfMyStringVariable.ToInt32();

This won’t work, though, as “John” is not a number.

However, this would work:

string s = "9999";
int n = s.ToInt32();

For further information, please see here. You’ll have to look the many different built-in methods up that you could use to convert one type to another.

Objects of type AdventureGame and objects of type State exist in two different scopes. This means that you are able to declare a variable with the same name in both classes. Each class would have its own nextStates variable. This makes sense because there are hundreds of thousands C# classes out there. We certainly don’t want to spend time finding names that haven’t been used yet since C# was created.

Each object has got its own variable. If you create two State objects, you would get two different story string variables.

By the way, if you are interested in a free C# course, I can recommend Bob Tabor’s.

Thank you for that information, as soon as I have time I will follow this course.

So, if I decided to declare AdventureGame by typing:
AndventureGame theNameOfMyDataType;, wouldn’t have a value, since AdventureGame is a class and classes are reference types, which means that, when only declared and not initialized, they store the default value null, which basically is a placeholder to be filled (as soon as you assign a value to theNameOfMyDataType) with an address to the RAM where the actual value is stored.
That’s how I understood it, at least.

I asked myself; why do we need reference types? Isn’t it less memory consuming if we just store the value itself? I’m not sure if what I’m about to say is wrong or correct. After thinking a while I came to a conclusion: since strings, classes and arrays can contain a lot of information it might be more practical to save their value in the heap (I think it’s called so) so that you’re only dealing with an address (which is maybe easier to handle for a computer) when you use them repeatedly in your code.

Coming back to my question: if I wanted to assign a value to theNameOfMyDataType, what value could it be? A number? A string? None of them? Something with the same Data Type?

AndventureGame theNameOfMyDataType;
theNameOfMyDataType = ??;

What can I exactly do with theNameOfMyDataType? How can it be used in a practical way?

Something else:

What is exactly an object? Doing some research I found out that an object is an instance of a class, but in the examples, it is always introduced by the modifier keyword new, which isn’t the case in our AdventureGame.cs or State.cs scripts. We haven’t yet introduced that keyword.

The variable does have a value: null, which means “no reference”.

And what is “the value” in your opinion?

This article might be interesting.

if I wanted to assign a value to theNameOfMyDataType , what value could it be? A number? A string? None of them? Something with the same Data Type?

The value is either null or a reference (“link”) to an object of type AndventureGame because you declared the variable with the AndventureGame type.

What is exactly an object?

It’s a defined collection of data. An integer in memory is also an object. In C#, everything is an object. We almost always deal with objects, almost never with the memory itself. Also see here. There is a lot of great information on the internet which you could find within seconds.

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

Privacy & Terms