Can someone explain this to me? Player myPlayer = new Player();
Player myPlayer = new Player();
So, there is another script (and class) named Player, which I think the script is trying to access, but I don't understand this line of code. I'm starting to learn Intermediate Gameplay Scripting, and I got confused with this. Please help!
Here's the video I'm learning from: https://www.youtube.com/watch?v=BYwCXJ-J9dA (it's an Unity Official Tutorial)
Also, I think myPlayer is the name given to the variable we're creating, but I don't understand "= new Player()"
Answer by Landern · Nov 14, 2016 at 06:35 PM
Classes in c# are an implementation(also called a type) that can be instantiated(created in memory) to represent something.
When
Player myPlayer = new Player();
is invoked in code, it is creating a new object called "myPlayer" which all the fields/properties/methods that exist. Right before the semi-colon at the end of the line is the parameters to pass to the constructor of the class when being created. By default there is a parameterless constructor, if no logic needs to take place you don't need to define it in the class, the below is the same as c# will create a default when building out the code. You will also notice that constucters don't have return types or marked as void(they return nothing).
public class Player
{
private string m_name; // field, a backing field for the property called Name
public Player()
{
// empty constructor
}
public string Name
{
get { return m_name; } // returns the private m_name field .
set { m_name = value } // sets the m_name to the special keyword value(the right hand of a equal sign)
}
}
// this class while doesn't explictly have a constructor does still have a constructor.
public class Player
{
private string m_name; // field, a backing field for the property called Name
public string Name
{
get { return m_name; } // returns the private m_name field .
set { m_name = value } // sets the m_name to the special keyword value(the right hand of a equal sign)
}
}
constructors can call constructors before so you can combine logic
public class Player
{
private string m_name; // field, a backing field for the property called Name
private int m_player_id; // field for a player id
public Player()
{
m_player_id = 1928;
}
// if this constructor was used it would allow you to pass in a player name and still call the default parameterless constructor which sets the m_player_id to 1928.
public Player(string playerName)
: this()
{
m_name = playerName;
}
public string Name
{
get { return m_name; } // returns the private m_name field .
set { m_name = value } // sets the m_name to the special keyword value(the right hand of a equal sign)
}
}
So at the end of the day, what you're asking about is just the creation of an object that either has login in it, or doesn't, but once instantiated the other members of the class(field/properties/etc) are available for you to manipulate.
You should have a class somewhere called Player which in the tutorial they do create.
Well explained! Didn't know you could combine constructors within the same class, aside from inheritance tricks, awesome! :)