Class Player() question - Javascript

I am a beginner programmer and have a question in regard to the “Player” class in the example below. The example is from a tutorial that I am doing that is showing how to setup a class, using a character type example. I understand the concept of setting up the class and using it to print to the console, but my question lies within what the class Player actually is in the code.

Question: Is the creator of the tutorial creating a sort of custom class called Player, or is Player a type of class for unity/javascript? So is Player a predefined thing, or is this person simply naming the class like you would a variable? I see that when you use the class in update, you have to type Player() which makes me think that it is predefined, but I can’t find it in the scripting reference.

Thanks!

function Update () {

var valkanyn = Player();
valkanyn.firstName = "Valkanyn";
valkanyn.race = "High Elf"; 
valkanyn.magicType = "Fire";
valkanyn.age = 436;

print(valkanyn.firstName + " is" + valkanyn.age + " years old");

}

class Player
{
	
var firstName;
var race;
var magicType;
var age;
}

I have just recently started getting my head around this stuff. Here’s what I think your code is doing: Every Update a local variable called valkanyn of type Player is being declared. Player is defined at the bottom, as a custom class that has it’s own custom variables. This doesn’t seem to make sense being in an Update.

UnityScript automatically creates a class (based on the file name), so Player is actually a child class of that class.
And you definitely don’t want to be doing that setup in Update - Awake or Start would be better places for it.

That aside, what’s your goal here? Currently, the code creates and initializes a Player object, prints some info, and discards it.
If this component is supposed to hold the player stats, then it should maintain a reference to the Player object (and you should consider whether that should be a separate object or not).

It’s a custom class; you can see it defined in the code you posted (line 13).

You can name a class whatever you want.

You have to use Player because that’s what you defined it as. As paulygons mentioned it shouldn’t be used in Update like that though.

No, in order for the Player class to be a “child” class it would have to extend the script class. The Player class as posted in the code above is completely unrelated to the script class. It’s perfectly fine to create multiple unrelated classes in the same script (maybe not the best way to organize things though).

–Eric

Thanks guys that clears things up a bit.

As far as it being in update, I think it is there because its early in the tutorial and we aren’t using it for actual gameplay yet, just printing to console. I’m sure it will be where it needs to be later on.

Thanks again guys, and thank you Eric for being specific.