Programming Chapter 2
Revision as of 21:21, 15 June 2007
Author: Lucas Goss
Are we Functional?
A function is a group of instructions that perform a specific task. If you completed Example-1, we had two functions in our script: Update and Start.
JScript - Function
<javascript> // function FunctionName(parameters) : type
// This function takes as input two numbers and adds them, returning the value function Add(number1 : int, number2 : int) : int {
return number1 + number2;
}
// This function has no parameters and returns nothing function Start() {
// Call a function using it's name and parameter list var sum = Add(3, 4);
} </javascript>
C# - Function
<csharp> // modifiers type FunctionName(parameters)
// This function takes as input two numbers and adds them, returning the value int Add(int number1, int number2) {
return number1 + number2;
}
// This function has no parameters and returns nothing void Start() {
// Call a function using it's name and parameter list int sum = Add(3, 4);
} </csharp> Start and Update), but hopefully you can see the usefulness as in the examples above. When you call a function it must have the same number of parameters as the function declaration. The "+" sign is an operator... but first lets try our hand at arrays.
An object (class) defines the characteristics of some thing. In .NET for example, every class is a child of the object class. The object class (another keyword) has some basic characteristics that are defined for everything. And class is the keyword to define an object in JScript and C#. Note that variables are sometimes called fields or attributes within a class, and functions may be called methods or behaviors in a class. Lets take a look at our Example-1 project that we created last chapter. Be sure to read the new comments and code added to the Start() method...
JScript - Object
<javascript> // This is an variable (field, attribute) within the HelloWorld class var message : String = "Hello World!";
// This is a method within the HelloWorld class function var function Update() { // guiText is a GUIText object that uses the . operator to access the member variable "text" guiText.text = message; } </javascript>
C# - Object
<csharp> using UnityEngine; using System.Collections;
// This is a HelloWorld class, that is public and a child of (inherits) MonoBehavior public class HelloWorld : MonoBehaviour { // This is an variable (field, attribute) within the HelloWorld class public string message = "Hello World!";
// This is a method within the HelloWorld class void int void Update () { // guiText is a GUIText object that uses the . operator to access the member variable "text" guiText.text = message; } } </csharp> In C# we had to declare class, JScript automatically creates a class if it's not defined (you can use the keyword class in JScript as well). The HelloWorld class inherits from MonoBehavior, meaning it now has all the variables and methods that are in MonoBehavior. Everything inside of the begin and end blocks is part of the HelloWorld object. Also since just about everything is an object you can cast any type to an object (using the cast operator). Well you can read more about Object Oriented Programming here, but for now we'll just cover one more thing.
Scope Modifiers
One last thing before we finish up. Scope modifiers are a way to tell what "scope" a variable, method, class, etc. has. An object has full access to everything inside, however non-public items cannot be used from the outside. Using the Example-1 project again, change the message type to private and save...
JScript - Scope Modifier
<javascript> private var message : String; </javascript>
C# - Scope Modifier
<csharp> private string message; </csharp> Now look in the inspector... message is gone! private only allows access from inside the class. Another type protected allows access only to the class and child classes. And public allows anyone access. So for example, our HelloWorld class inherits from MonoBehavior. Any private variable or method in MonoBehavior cannot be used by our class, but our class can use protected and public variables and methods. The "Log" method in the "Debug" object must be a public method, because if it was private or protected we couldn't use it as it is not declared in our class and our class does not inherit from it.
Recap
Things we learned:
- Functions are a group of instructions that take parameters and return a type
-: .
- An Object is defined by the keyword class and contains methods, variables, and other defining characteristics
- Almost everything is of type object (and can be cast to an object)
- Scope Modifiers
- private is only accessible to the class defining it
- protected is only accessible to the class defining it and classes that inherit from it
- public is accessible to everything
Example-2
TODO
Programming Index : Previous Chapter : Next Chapter