Hi,
But what kind of data does a PlayerController type hold?
I would refer to that as PlayerController data. Your class for example may have multiple members, as @Stephen_Crookes suggests above, if you add a field for the player’s name, as a string then this would become one of the data items it contains. With a class named as PlayerController I would imagine this could cover a number of specific data items that relate to the player, or player interactions within your game.
What do you call types made from the names of other objects?
I’m not quite sure I understand this question. Creating a class defines a type. When you reference these classes with variables, your variables can be of any choice of name you like, but you should consider the relevance of the name.
Can I make a type out of any name?
Yes, you can create a class with a name of your choice, however, you should give some consideration to any clashes in the naming, for example, creating your own class called String
may be a bad idea, as you may find clashes with the System.String
. Although these classes would effectively be in different namespace, e.g. System
and your class being in your project as a default namespace, it is generally best to name your classes based upon what they represent.
How do I know when I have to use the name of an object to create a type?
Again, I’m not entirely sure I’m clear on what you mean here, but I think you are talking about the differences I will try to outline in an example below;
public class LevelManager : MonoBehaviour
{
public void LoadLevel(string name)
{
Application.LoadLevel(name);
}
}
Ok, so in the above example, we have a class called LevelManager, it has a public method called LoadLevel, this method expects to be passed a string which will represent the name of the level we wish to load.
In order to use our LevelManager, we would either expose a public field and drag our LevelManager GameObject from the scene into this field, or perhaps find the LevelManager in the scene (you do this in BlockBreaker).
In our code, we would have something like this;
private LevelManager levelManager;
private void Start(
{
levelManager = GameObject.FindObjectOfType<LevelManager>();
}
So, here, the lower-cased levelManager is our variable of type LevelManager
(our class).
In order to use it’s LoadLevel
method, we would have a statement perhaps like this;
levelManager.LoadLevel("MyFirstLevel");
Note, the lower-cased variable, and how we are passing in the name of our level as a string, as per the method’s definition in our class.
The above outlines an instantiation approach, e.g. we have an instance of our LevelManager
class, in this case, attached to a GameObject in the scene, which we have either passed in by reference, or, found (as per the example above).
An alternative, would be to make our LoadLevel
method static. This would allow us to call the method without first having to instantiate an instance of our class, and I think this may be what you are asking, with regards to the use of the class name (type) as opposed to the variable name.
If we re-write our LevelManager like this;
public class LevelManager : MonoBehaviour
{
public static void LoadLevel(string name)
{
Application.LoadLevel(name);
}
}
Note, the use of the static keyword in method definition now, we can call this method without having to instantiate our class, for example;
LevelManager.LoadLevel("MyFirstLevel");
Because we do not have an instance variable, we use the class name, hence the upper-cased LevelManager, followed by the name of our static method.
In fact, if you look at the Application.LoadLevel
method which we subsequently call, this is doing exactly that.
I’m not 100% certain whether this was the question that you were asking here, but I have a feeling it was, if not, my apologies, but I would need a little more clarification, perhaps you could provide an example etc.