Programming Chapter 1

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(It's always JavaScript, never JScript)
Line 3: Line 3:
 
== Make a Statement ==
 
== Make a Statement ==
  
A statement is basically a computer instruction. Semicolons in JScript and C# are for ending a statement, much like when we write sentences we end with a period. And just like you can have multiple sentences on one line, you can have multiple statements. For example:
+
A statement is basically a computer instruction. Semicolons in JavaScript and C# are for ending a statement, much like when we write sentences we end with a period. And just like you can have multiple sentences on one line, you can have multiple statements. For example:
 
<javascript>;;;</javascript>
 
<javascript>;;;</javascript>
 
That's three empty statements and legal code! An empty statement doesn't do anything, which is why it's called empty.
 
That's three empty statements and legal code! An empty statement doesn't do anything, which is why it's called empty.
  
A block allows multiple statements to be written where one statement is allowed, and is the same in JScript and C#, using begin '{' block and '}' end block. Ready for some more do nothing code:
+
A block allows multiple statements to be written where one statement is allowed, and is the same in JavaScript and C#, using begin '{' block and '}' end block. Ready for some more do nothing code:
 
<javascript>{ ;; }</javascript>
 
<javascript>{ ;; }</javascript>
 
That's valid code as well. You can also have a block inside a block (this is called nesting).
 
That's valid code as well. You can also have a block inside a block (this is called nesting).
Line 16: Line 16:
  
 
A comment is a piece of text in code that does nothing:
 
A comment is a piece of text in code that does nothing:
<javascript>// this is a comment in Javascript (JScript) and C#
+
<javascript>// this is a comment in Javascript and C#
 
; // This is a comment after an empty statement</javascript>
 
; // This is a comment after an empty statement</javascript>
 
The '//' tells the compiler to ignore whatever follows it on the same line. There are also begin/end comments:
 
The '//' tells the compiler to ignore whatever follows it on the same line. There are also begin/end comments:
Line 32: Line 32:
  
 
Feel free to try any of these out in Unity by creating a new Javascript or CSharp file and entering this code inside of the 'Update' block. Our custom code starts at the line with '// Empty statements...':
 
Feel free to try any of these out in Unity by creating a new Javascript or CSharp file and entering this code inside of the 'Update' block. Our custom code starts at the line with '// Empty statements...':
==== JScript - Play ====
+
==== JavaScript - Play ====
 
<javascript>function Update () {
 
<javascript>function Update () {
 
     // Empty statements inside a block below
 
     // Empty statements inside a block below
Line 52: Line 52:
 
== What's your Type? ==
 
== What's your Type? ==
  
A data type is a way to tell the compiler what kind of information you want to store and how it should be analyzed. The most common types used in programming are numbers, letters and boolean (true/false). On top of this there are other predefined types in languages as well as user defined types (types that we can create). JScript uses dynamic typing, which means that you don't always have to tell it the type, it will dynamically try to figure out what type something is. C# uses static typing, meaning that you always have to tell it what type you want your data to be. Each has it's up and downsides, and you can find out more [http://en.wikipedia.org/wiki/Type_system here (check out the Dynamic typing section)].
+
A data type is a way to tell the compiler what kind of information you want to store and how it should be analyzed. The most common types used in programming are numbers, letters and boolean (true/false). On top of this there are other predefined types in languages as well as user defined types (types that we can create). JavaScript uses dynamic typing, which means that you don't always have to tell it the type, it will dynamically try to figure out what type something is. C# uses static typing, meaning that you always have to tell it what type you want your data to be. Each has it's up and downsides, and you can find out more [http://en.wikipedia.org/wiki/Type_system here (check out the Dynamic typing section)].
  
Now I'm not going to list all the keywords and types, that's done in the references already, use them. But I'll try to explain enough so that when you look at those sites they will be useful. Here are some common types in JScript and C# (again, look at the references for all of them):
+
Now I'm not going to list all the keywords and types, that's done in the references already, use them. But I'll try to explain enough so that when you look at those sites they will be useful. Here are some common types in JavaScript and C# (again, look at the references for all of them):
 
{| border="1" cellspacing="0" cellpadding="4"
 
{| border="1" cellspacing="0" cellpadding="4"
!JScript
+
!JavaScript
 
!C#
 
!C#
 
!Size
 
!Size
Line 82: Line 82:
 
|}
 
|}
  
OK, now what all is that saying. A boolean type can only store true or false (a 1 or 0 underneath). Why doesn't it have a size? Probably because the language designers decided to allow the implementors (JScript and C# are standards and can be implemented by anyone), to be able to handle the type under the hood however they want for the sake of efficiency. Since this is a concise guide, ahem, I won't go into the gritty details and you don't need to worry about it. Now the string also doesn't have a size, but the reason for that is because it can grow and shrink as needed (for efficiencies sake). A string can store any text we want (must be a [http://en.wikipedia.org/wiki/Unicode Unicode Character], but again a minor detail we don't need to worry about). The last two types are numbers, one for integers (-1, 0, 1, 2, ...), and one for decimals (-1.0, 0.0, 0.2, 1.445, ...). Why not just store everything as a decimal? Again, this is for efficiencies sake. A lot of choices made by languages have to have a balance between efficiency (sometimes caused by the underlying hardware) and usability. But don't sweat it, you don't need to remember all this. Just know there are different tools for the job... or rather different types for the job.
+
OK, now what all is that saying. A boolean type can only store true or false (a 1 or 0 underneath). Why doesn't it have a size? Probably because the language designers decided to allow the implementors (JavaScript and C# are standards and can be implemented by anyone), to be able to handle the type under the hood however they want for the sake of efficiency. Since this is a concise guide, ahem, I won't go into the gritty details and you don't need to worry about it. Now the string also doesn't have a size, but the reason for that is because it can grow and shrink as needed (for efficiencies sake). A string can store any text we want (must be a [http://en.wikipedia.org/wiki/Unicode Unicode Character], but again a minor detail we don't need to worry about). The last two types are numbers, one for integers (-1, 0, 1, 2, ...), and one for decimals (-1.0, 0.0, 0.2, 1.445, ...). Why not just store everything as a decimal? Again, this is for efficiencies sake. A lot of choices made by languages have to have a balance between efficiency (sometimes caused by the underlying hardware) and usability. But don't sweat it, you don't need to remember all this. Just know there are different tools for the job... or rather different types for the job.
  
 
== It's all Variable ==
 
== It's all Variable ==
  
 
A variable is a named location to store data. And much like the definition, the data at a location can be changed. Since the information can change it's good to think a little about how you name a variable. So lets get an example program going. Say we want to store a number, like the number of lives a player has.
 
A variable is a named location to store data. And much like the definition, the data at a location can be changed. Since the information can change it's good to think a little about how you name a variable. So lets get an example program going. Say we want to store a number, like the number of lives a player has.
====JScript - Variable naming====
+
====JavaScript - Variable naming====
 
<javascript>
 
<javascript>
 
// Bad name
 
// Bad name
Line 107: Line 107:
 
</csharp>
 
</csharp>
  
When we declare a variable in both languages, it's called a declaration statement, and declaration statements require a proper ending semicolon ';'. In JScript you declare a variable with the keyword '''var''', but remember JScript does dynamic typing so you don't have to put the type, but you can. To tell JScript what type something is you follow the type with a colon ':', and then put the type ('''int''' in this case). Since C# is statically typed, it doesn't need a variable keyword because you have to tell it a type.
+
When we declare a variable in both languages, it's called a declaration statement, and declaration statements require a proper ending semicolon ';'. In JavaScript you declare a variable with the keyword '''var''', but remember JavaScript does dynamic typing so you don't have to put the type, but you can. To tell JavaScript what type something is you follow the type with a colon ':', and then put the type ('''int''' in this case). Since C# is statically typed, it doesn't need a variable keyword because you have to tell it a type.
  
 
The name 'three' can be bad in this instance because while it may be the value we want to store, this is just the name for programmers to use, not the value. And since later the value can change (thus variable), it doesn't make sense to have a name three storing a value of one when the player has lost two lives!
 
The name 'three' can be bad in this instance because while it may be the value we want to store, this is just the name for programmers to use, not the value. And since later the value can change (thus variable), it doesn't make sense to have a name three storing a value of one when the player has lost two lives!
  
 
Now the above variables are all well and good, but what information are they storing since we didn't tell it what to store? The answer is whatever the language uses for the default. You can find the default storage for types in the language references. Now sometimes the default value isn't what we want, so we may want to initialize the value ourselves by telling the variable what to store. It's usually a good idea to initialize variables.
 
Now the above variables are all well and good, but what information are they storing since we didn't tell it what to store? The answer is whatever the language uses for the default. You can find the default storage for types in the language references. Now sometimes the default value isn't what we want, so we may want to initialize the value ourselves by telling the variable what to store. It's usually a good idea to initialize variables.
====JScript - Variable initializing====
+
====JavaScript - Variable initializing====
 
<javascript>
 
<javascript>
 
// Just use the good name
 
// Just use the good name
Line 123: Line 123:
 
</csharp>
 
</csharp>
  
In those examples we store the integer 3 in our variable. The '=' is an assignment operator, it assigns the value on the right to the variable on the left. A variable in most languages usually has to start with an alpha character or an underscore (there are a few other characters allowed as well that vary by language), can only contain alpha characters, numbers, or underscores, can't contain spaces, and in the case of C# and JScript is case sensitive (languages like BASIC are not). So a variable named 'playerLives' is different from 'PlayerLives'.
+
In those examples we store the integer 3 in our variable. The '=' is an assignment operator, it assigns the value on the right to the variable on the left. A variable in most languages usually has to start with an alpha character or an underscore (there are a few other characters allowed as well that vary by language), can only contain alpha characters, numbers, or underscores, can't contain spaces, and in the case of C# and JavaScript is case sensitive (languages like BASIC are not). So a variable named 'playerLives' is different from 'PlayerLives'.
  
 
== Staying Constant ==
 
== Staying Constant ==
  
 
A constant is a fixed value. Unlike a variable it is constant (doesn't change). The '3' used for the player lives above is a constant. A constant cannot be assigned a value, it's constant so it can't be changed. We've only looked at numbers so far, so lets look at a string.
 
A constant is a fixed value. Unlike a variable it is constant (doesn't change). The '3' used for the player lives above is a constant. A constant cannot be assigned a value, it's constant so it can't be changed. We've only looked at numbers so far, so lets look at a string.
====JScript - String consant====
+
====JavaScript - String consant====
 
<javascript>
 
<javascript>
 
var playerName = "Super Player";
 
var playerName = "Super Player";
Line 141: Line 141:
  
 
Scope is the range that a variable exists in. Once a variable is declared, you can't declare it again in the same scope.
 
Scope is the range that a variable exists in. Once a variable is declared, you can't declare it again in the same scope.
====JScript - Same Scope====
+
====JavaScript - Same Scope====
 
<javascript>
 
<javascript>
 
var playerLives = 3;
 
var playerLives = 3;
Line 165: Line 165:
  
 
Scope is defined in a block, remember those? Also remember that a block can be nested in another block. A nested block inherits all of the variables inside it's parent block, but the parent can't redefine a variable that's declared in a child. And when variables go out of scope they are no longer defined.
 
Scope is defined in a block, remember those? Also remember that a block can be nested in another block. A nested block inherits all of the variables inside it's parent block, but the parent can't redefine a variable that's declared in a child. And when variables go out of scope they are no longer defined.
====JScript - Different Scope ====
+
====JavaScript - Different Scope ====
 
<javascript>
 
<javascript>
 
var playerLives = 3;
 
var playerLives = 3;
Line 233: Line 233:
 
** '/*' and '*/' begin and end a comment.
 
** '/*' and '*/' begin and end a comment.
 
* A '''data type''' tells the compiler what type of information to store.
 
* A '''data type''' tells the compiler what type of information to store.
** JScript uses dynamic typing, meaning data doesn't always have to declare a type.
+
** JavaScript uses dynamic typing, meaning data doesn't always have to declare a type.
 
** C# uses static typing, meaning a data type is always required.
 
** C# uses static typing, meaning a data type is always required.
 
* A '''variable''' is a named location to store data.
 
* A '''variable''' is a named location to store data.
Line 240: Line 240:
 
*** Can only contain alpha characters, numbers, or underscores (again possibly others).
 
*** Can only contain alpha characters, numbers, or underscores (again possibly others).
 
*** Can't contain spaces.
 
*** Can't contain spaces.
*** Is case sensitive for JScript and C#.
+
*** Is case sensitive for JavaScript and C#.
 
* A '''declaration statement''' (declaring a variable) ends with a ';'
 
* A '''declaration statement''' (declaring a variable) ends with a ';'
 
* An '''assignment operator''' '=' assigns a value on the right to a variable on the left.
 
* An '''assignment operator''' '=' assigns a value on the right to a variable on the left.
Line 250: Line 250:
 
In each Unity-X section we're going to put what we've learned into practice. By the end of the tutorials, we should have a fully working game. So what kind of game will we make? Well, since we're just learning programming, we'll just make a simple text battle game. That way it doesn't require a lot of art overhead if you're not an artist. So here we go...
 
In each Unity-X section we're going to put what we've learned into practice. By the end of the tutorials, we should have a fully working game. So what kind of game will we make? Well, since we're just learning programming, we'll just make a simple text battle game. That way it doesn't require a lot of art overhead if you're not an artist. So here we go...
  
The game will be called... Unity-X! So go ahead and create a new Unity project and name it Unity-X. Next create a folder and label it Game Assets. Now create a javascript or c# file in the Game Assets folder, name it Person and type in the following jscript or c# code:
+
The game will be called... Unity-X! So go ahead and create a new Unity project and name it Unity-X. Next create a folder and label it Game Assets. Now create a javascript or c# file in the Game Assets folder, name it Person and type in the following JavaScript or C# code:
====JScript - Person====
+
====JavaScript - Person====
 
<javascript>
 
<javascript>
 
// This is a person that will battle in the game (can be player or enemy)
 
// This is a person that will battle in the game (can be player or enemy)
Line 371: Line 371:
 
</csharp>
 
</csharp>
  
Next we need a script that we can attach to a GUI Text object to display the stats. So create a jscript or c# file, name it StatsText, and write the following code (don't worry about what it does now):
+
Next we need a script that we can attach to a GUI Text object to display the stats. So create a JavaScript or C# file, name it StatsText, and write the following code (don't worry about what it does now):
====JScript - StatsText====
+
====JavaScript - StatsText====
 
<javascript>
 
<javascript>
 
var player : Person;
 
var player : Person;

Revision as of 10:15, 14 June 2007

Author: Lucas Goss

Contents

Make a Statement

A statement is basically a computer instruction. Semicolons in JavaScript and C# are for ending a statement, much like when we write sentences we end with a period. And just like you can have multiple sentences on one line, you can have multiple statements. For example: <javascript>;;;</javascript> That's three empty statements and legal code! An empty statement doesn't do anything, which is why it's called empty.

A block allows multiple statements to be written where one statement is allowed, and is the same in JavaScript and C#, using begin '{' block and '}' end block. Ready for some more do nothing code: <javascript>{ ;; }</javascript> That's valid code as well. You can also have a block inside a block (this is called nesting). <javascript>{ ; { ;; } { {} } }</javascript> Notice that for each starting '{' there is an ending '}' that matches it. This is required or else you will get an error from the compiler.

Comment Inside

A comment is a piece of text in code that does nothing: <javascript>// this is a comment in Javascript and C#

// This is a comment after an empty statement</javascript>

The '//' tells the compiler to ignore whatever follows it on the same line. There are also begin/end comments: <javascript>/*

 This comment extends
 over multiple lines.
  • /

{ /* A begin/end comment on one line } */ } </javascript>

The '/*' is the start of the comment and the '*/' is the end. Everything in between is ignored by the compiler, so it will never execute any code inside of a comment. The second comment is valid even though it has two '}' on one line, because one is inside the comment and the other is not. Since the compiler ignores code in a comment it only sees one '}'.

Free to Play

Feel free to try any of these out in Unity by creating a new Javascript or CSharp file and entering this code inside of the 'Update' block. Our custom code starts at the line with '// Empty statements...':

JavaScript - Play

<javascript>function Update () {

   // Empty statements inside a block below
   { ;; }

}</javascript>

CSharp - Play

<csharp>using UnityEngine; using System.Collections;

public class Play : MonoBehaviour {

   void Update()
   {
       // Empty statements inside a block below
       { ;; }
   }

}</csharp>

What's your Type?

A data type is a way to tell the compiler what kind of information you want to store and how it should be analyzed. The most common types used in programming are numbers, letters and boolean (true/false). On top of this there are other predefined types in languages as well as user defined types (types that we can create). JavaScript uses dynamic typing, which means that you don't always have to tell it the type, it will dynamically try to figure out what type something is. C# uses static typing, meaning that you always have to tell it what type you want your data to be. Each has it's up and downsides, and you can find out more here (check out the Dynamic typing section).

Now I'm not going to list all the keywords and types, that's done in the references already, use them. But I'll try to explain enough so that when you look at those sites they will be useful. Here are some common types in JavaScript and C# (again, look at the references for all of them):

JavaScript C# Size Range
boolean bool N/A true or false
String string N/A 0 to approximately 2 billion Unicode characters. Each character is 16 bits (two bytes).
int int 4 bytes -2,147,483,648 to 2,147,483,647
float float 4 bytes Approximate range is -1038 to 1038 with accuracy of about 7 digits. Can represent numbers as small as 10-44

OK, now what all is that saying. A boolean type can only store true or false (a 1 or 0 underneath). Why doesn't it have a size? Probably because the language designers decided to allow the implementors (JavaScript and C# are standards and can be implemented by anyone), to be able to handle the type under the hood however they want for the sake of efficiency. Since this is a concise guide, ahem, I won't go into the gritty details and you don't need to worry about it. Now the string also doesn't have a size, but the reason for that is because it can grow and shrink as needed (for efficiencies sake). A string can store any text we want (must be a Unicode Character, but again a minor detail we don't need to worry about). The last two types are numbers, one for integers (-1, 0, 1, 2, ...), and one for decimals (-1.0, 0.0, 0.2, 1.445, ...). Why not just store everything as a decimal? Again, this is for efficiencies sake. A lot of choices made by languages have to have a balance between efficiency (sometimes caused by the underlying hardware) and usability. But don't sweat it, you don't need to remember all this. Just know there are different tools for the job... or rather different types for the job.

It's all Variable

A variable is a named location to store data. And much like the definition, the data at a location can be changed. Since the information can change it's good to think a little about how you name a variable. So lets get an example program going. Say we want to store a number, like the number of lives a player has.

JavaScript - Variable naming

<javascript> // Bad name var three;

// Good name var playerLives;

// Good name with type var lives : int; </javascript>

C# - Variable naming

<csharp> // Bad name int three;

// Good name int playerLives; </csharp>

When we declare a variable in both languages, it's called a declaration statement, and declaration statements require a proper ending semicolon ';'. In JavaScript you declare a variable with the keyword var, but remember JavaScript does dynamic typing so you don't have to put the type, but you can. To tell JavaScript what type something is you follow the type with a colon ':', and then put the type (int in this case). Since C# is statically typed, it doesn't need a variable keyword because you have to tell it a type.

The name 'three' can be bad in this instance because while it may be the value we want to store, this is just the name for programmers to use, not the value. And since later the value can change (thus variable), it doesn't make sense to have a name three storing a value of one when the player has lost two lives!

Now the above variables are all well and good, but what information are they storing since we didn't tell it what to store? The answer is whatever the language uses for the default. You can find the default storage for types in the language references. Now sometimes the default value isn't what we want, so we may want to initialize the value ourselves by telling the variable what to store. It's usually a good idea to initialize variables.

JavaScript - Variable initializing

<javascript> // Just use the good name var playerLives = 3; </javascript>

C# - Variable initializing

<csharp> // Just use the good name int playerLives = 3; </csharp>

In those examples we store the integer 3 in our variable. The '=' is an assignment operator, it assigns the value on the right to the variable on the left. A variable in most languages usually has to start with an alpha character or an underscore (there are a few other characters allowed as well that vary by language), can only contain alpha characters, numbers, or underscores, can't contain spaces, and in the case of C# and JavaScript is case sensitive (languages like BASIC are not). So a variable named 'playerLives' is different from 'PlayerLives'.

Staying Constant

A constant is a fixed value. Unlike a variable it is constant (doesn't change). The '3' used for the player lives above is a constant. A constant cannot be assigned a value, it's constant so it can't be changed. We've only looked at numbers so far, so lets look at a string.

JavaScript - String consant

<javascript> var playerName = "Super Player"; </javascript>

C# - String consant

<csharp> string playerName = "Super Player"; </csharp> Here the string "Super Player" is a constant. The variable playerName (a string type), gets assigned the value of "Super Player".

What's the Scope?

Scope is the range that a variable exists in. Once a variable is declared, you can't declare it again in the same scope.

JavaScript - Same Scope

<javascript> var playerLives = 3;

// Remember, this will never get executed since it's in a comment. // But if you took off the comment below you'd get an error because you defined the variable twice. // var playerLives = 2;

// Change the value like this playerLives = 2; </javascript>

C# - Same Scope

<csharp> int playerLives = 3;

// Remember, this will never get executed since it's in a comment. // But if you took off the comment below you'd get an error because you defined the variable twice. // int playerLives = 2;

// Change the value like this playerLives = 2; </csharp>

Scope is defined in a block, remember those? Also remember that a block can be nested in another block. A nested block inherits all of the variables inside it's parent block, but the parent can't redefine a variable that's declared in a child. And when variables go out of scope they are no longer defined.

JavaScript - Different Scope

<javascript> var playerLives = 3;

{

   // This is a nested block so we don't redefine the variable, we just use it
   playerLives = 2;
   // We can define the same name in separate blocks though
   var playerAge = 18;

}

{

   // Same variable name as above, but in a different scope
   var playerAge = 16;

}

{

   // Taking the comment off this will give an error,
   // since variables only exist in the scope they were defined
   // playerAge = 10;

}

// This will also error if uncommented since the variable is not in this scope (undefined) // playerAge = 8;

// And this will error if uncommented because a child has already defined this variable // var playerAge = 5; </javascript>

C# - Different Scope

<csharp> int playerLives = 3;

{

   // This is a nested block so we don't redefine the variable, we just use it
   playerLives = 2;
   // We can define the same name in separate blocks though
   int playerAge = 18;

}

{

   // Same variable name as above, but in a different scope
   int playerAge = 16;

}

{

   // Taking the comment off this will give an error,
   // since variables only exist in the scope they were defined
   // playerAge = 10;

}

// This will also error if uncommented since the variable is not in this scope (undefined) // playerAge = 8;

// And this will error if uncommented because a child has already defined this variable // int playerAge = 5; </csharp>

Recap

These are the things that you should have learned:

  • An empty statement is just a semicolon ';', and does nothing.
  • A block '{ }' allows multiple statements to be written where one is allowed.
  • A comment is ignored by the compiler and never seen.
    • '//' comments everything after it on the same line.
    • '/*' and '*/' begin and end a comment.
  • A data type tells the compiler what type of information to store.
    • JavaScript uses dynamic typing, meaning data doesn't always have to declare a type.
    • C# uses static typing, meaning a data type is always required.
  • A variable is a named location to store data.
    • A variable name:
      • Starts with a alpha character or underscore (possibly others depending on the language).
      • Can only contain alpha characters, numbers, or underscores (again possibly others).
      • Can't contain spaces.
      • Is case sensitive for JavaScript and C#.
  • A declaration statement (declaring a variable) ends with a ';'
  • An assignment operator '=' assigns a value on the right to a variable on the left.
  • A constant is a fixed value.
  • A scope is the range that a variable exists in.
    • The same variable cannot be declared twice in the same scope.

Unity-X

In each Unity-X section we're going to put what we've learned into practice. By the end of the tutorials, we should have a fully working game. So what kind of game will we make? Well, since we're just learning programming, we'll just make a simple text battle game. That way it doesn't require a lot of art overhead if you're not an artist. So here we go...

The game will be called... Unity-X! So go ahead and create a new Unity project and name it Unity-X. Next create a folder and label it Game Assets. Now create a javascript or c# file in the Game Assets folder, name it Person and type in the following JavaScript or C# code:

JavaScript - Person

<javascript> // This is a person that will battle in the game (can be player or enemy)

// fullName - The full name of the person // age - The persons age var fullName : String; var age : int;

// level - A number that declares a players progress (how experienced they are) // maxLevel - The maximum level a player can progress to // experience - Amount of experience a player has (leads to gaining a level) var level : int; var maxLevel : int = 99; var experience : int;

// hitPoints - Amount of life a player has // maxHitPoints - Maximum life the player can have (depending on experience) var hitPoints : int; var maxHitPoints : int;

// strength - The stronger, the harder the attack and stronger the defense // maxStrength - Maximum strength at current level var strength : int; var maxStrength : int;

// stamina - More stamina gives more power to endure and fight fatigue // maxStamina - Maximum stamina at current level var stamina : int; var maxStamina : int;

// quickness - The quicker the reaction, more likely to dodge and faster attack // maxQuickness - Maximum quickness at current level var quickness : int; var maxQuickness : int;

// Gets the players current stats in a string function GetStats() : String { return "Name: " + fullName + "\n" + "Age : " + age.ToString() + "\n" + "Lv  : " + level.ToString() + "/" + maxLevel.ToString() + "\n" + "Exp : " + experience.ToString() + "\n" + "Hp  : " + hitPoints.ToString() + "/" + maxHitPoints.ToString() + "\n" + "Str : " + strength.ToString() + "/" + maxStrength.ToString() + "\n" + "Sta : " + stamina.ToString() + "/" + maxStamina.ToString() + "\n" + "Qck : " + quickness.ToString() + "/" + maxQuickness.ToString() + "\n"; }

function Update () { }

</javascript>

C# - Person

<csharp> using UnityEngine; using System.Collections;

// This is a person that will battle in the game (can be player or enemy) public class Person : MonoBehaviour { // fullName - The full name of the person // age - The persons age public string fullName; public int age;

// level - A number that declares a players progress (how experienced they are) // maxLevel - The maximum level a player can progress to // experience - Amount of experience a player has (leads to gaining a level) public int level; public int maxLevel = 99; public int experience;

// hitPoints - Amount of life a player has // maxHitPoints - Maximum life the player can have (depending on experience) public int hitPoints; public int maxHitPoints;

// strength - The stronger, the harder the attack and stronger the defense // maxStrength - Maximum strength at current level public int strength; public int maxStrength;

// stamina - More stamina gives more power to endure and fight fatigue // maxStamina - Maximum stamina at current level public int stamina; public int maxStamina;

// quickness - The quicker the reaction, more likely to dodge and faster attack // maxQuickness - Maximum quickness at current level public int quickness; public int maxQuickness;

// Use this for initialization void Start () { }

// Gets the players current stats in a string public string GetStats() { return "Name: " + fullName + "\n" + "Age : " + age.ToString() + "\n" + "Lv  : " + level.ToString() + "/" + maxLevel.ToString() + "\n" + "Exp : " + experience.ToString() + "\n" + "Hp  : " + hitPoints.ToString() + "/" + maxHitPoints.ToString() + "\n" + "Str : " + strength.ToString() + "/" + maxStrength.ToString() + "\n" + "Sta : " + stamina.ToString() + "/" + maxStamina.ToString() + "\n" + "Qck : " + quickness.ToString() + "/" + maxQuickness.ToString() + "\n"; }

// Update is called once per frame void Update () { } } </csharp>

Next we need a script that we can attach to a GUI Text object to display the stats. So create a JavaScript or C# file, name it StatsText, and write the following code (don't worry about what it does now):

JavaScript - StatsText

<javascript> var player : Person;

function Update () { guiText.text = player.GetStats(); }

</javascript>

C# - StatsText

<csharp> using UnityEngine; using System.Collections;

public class StatsText : MonoBehaviour { public Person player;

// Use this for initialization void Start () { }

// Update is called once per frame void Update () { guiText.text = player.GetStats(); } }

</csharp>

TODO - Unity steps to setup stats


Programming Index : Previous Chapter : Next Chapter

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox