Wednesday, April 13, 2011

Learn C sharp Gaming Chapter 7


Basics of Structures and Classes
In the olden days of computer programming, programming languages were quite simple,
and you could create only a limited number of variables. This, obviously, made programs
very limited and quite ugly, to boot. For example, you would be creating programs like the
following in an older language:
int SpaceshipArmor;
int SpaceshipPower;
int SpaceshipFuel;
int EnemyArmor;
int EnemyPower;
int EnemyFuel;
As you can imagine, this gets to be a tangled mess rather quickly, and makes it very difficult
to manage your code.

Using classes and structures makes your life easier by encapsulating data into an easy-to
use packet of data.
Creating Classes and Structures
Basically, the idea behind classes and structures is to create your very own kind of object
using pieces of what was already in the language. A structure is essentially a data type that
can hold other pieces of data inside of it, allowing you to build your own types of data; it
is sort of like a building block. For example, here’s a structure describing a simple spaceship
object in C#:
struct Spaceship
{
public int fuel;
public int armor;
public int power;
}

note
The keyword public tells the compiler that any function anywhere can access the data inside of a
structure. You don’t have to worry about this right now; I’ll go more into depth on this later on in
the chapter. If the word public is left out, then the computer assumes you don’t want things outside
of the class accessing it.

note
To create a class, simply replace the struct keyword with class in the previous example.
Now, inside of your programs you can create your very own spaceship variables:
Spaceship player;
Spaceship enemy;
player.fuel = 100;
enemy.fuel = 100;

Differences between Structures and Classes
In C#, there are a few fundamental differences between classes and structures. Structures
are meant to be lightweight constructions, meaning they’re usually very simple and don’t
have a lot of complex features in them. Structures are also usually smaller than classes, so
C# will always create structures as value types (meaning they will always be created on the
stack).


Classes, unlike structures, are always reference types, and thus are always created on the
heap, rather than on the stack. Classes have many features that that structures do not have,
but instead of throwing them all at you now, I’ll explain these features as we come across
them.

Putting Functions in Your Classes and Structures
Classes and structures not only have the ability to store data, but they can perform certain
operations as well, if you give them the ability to do so. For example, you might want to
make it easy to quickly reset all of the data in a spaceship to 100; without a function, this
would look like the following:
player.fuel = 100;
player.armor = 100;
player.power = 100;
Obviously, this isn’t something that you want to be doing all over the place, so why not
put it inside a function instead, thus making the Spaceship class look like this (the new part
is in bold):
struct Spaceship
{
public int fuel;
public int armor;
public int power;
public void Recharge()
{
fuel = 100;
armor = 100;
power = 100;
}
}
Now you can just call the Recharge function on a spaceship whenever you want to have all
of its variables recharged:
player.Recharge();

Return Values
Functions not only perform tasks, but they can return values, as well. For example, say you
have a spaceship; you know how much fuel and power it has, but you’re not really sure
how much longer the power supplies will last. To calculate this, you make up a formula—

let’s say you get two hours of time from each power unit; in order to find out how much
time you have left on your current power level, you would do something like this:
int hoursleft = player.power * 2;
Well, that’s one way to solve the problem, but it isn’t really a great solution. Later on in the
game, you may decide that each power unit supplies three hours instead of two. To make
this change, you’d have to go through all of your code and find all the places where you
used 2 and change them to 3. Not fun.
So make this process into a function!
int HoursofPowerLeft()
{
return power * 2;
}
Ta-da! The int in front of the function name tells the compiler what type is being returned
from the function, and you use the return keyword to return a value. If you don’t want to
return anything, then just use void, as you saw previously.
You should note that the return statement causes the function to exit immediately. If you
look at the following code, you’ll see that some of it will never execute:
int Function()
{
return 0;
int x = 10; // this never executes
}
note
You should note that the C# compiler is smart enough to realize that the code won’t execute, and
it yells at you for writing code like that.
You’re also allowed to have multiple return statements in your code:
int Function()
{
if( something )
return 0;
return 1;
}
In this code, if something is true (assuming it exists, of course), then 0 is returned; otherwise,
1 is returned.


Parameters
You’re also allowed to give a function some parameters to work with. My example of
calculating the amount of time left with your available power level is a very simple calculation
that doesn’t use any parameters, but I can change this and make it more flexible.
What if the power drain on a spaceship depends on some external factor, like how much
radiation is in the system (bear with me—I’m just making this up)? Let’s say that the lower
the radiation level in the system, the less power drain there is. So, if you rewrite the previous
function with this in mind, you get:
int HoursofPowerLeft( int radiationlevel )
{
return (power * 2) / radiationlevel;
}
If the radiation level was 1 (I’m using completely fictitious data measurements here; just
pretend it makes sense) and your power is 100, then the number of hours left is 200. If the
radiation level is 2, then you have 100 hours left; and if it’s 3, you have 66 hours left.
You would call the function like this:
int hoursleft = player.HoursofPowerLeft( 1 );


Multiple Parameters
There will be times when you’re going to want to pass in more than one parameter, and
C# allows you to do that:
int Function1( int parameter1, float parameter2, double parameter3 )
Value Parameters versus Reference Parameters
This is where things can get a little tricky. Let’s say you have a class with two functions that
looks like this:
class MyClass
{
public void Function1( int parameter )
{
parameter = 10;
}
public void Function2()
{
int x = 0;
Function1( x );
// what is x?
}
}
So what is x after this code completes? Is it 0 or is it 10? The answer is 0 because you passed
x in by-value. This means that the computer took the value of x, copied it, and placed it into
a new variable named parameter; now, when parameter is changed, nothing happens to x.
So how do you make it pass by reference? Just do two things. First, change the declaration
of Function1:
public void Function1( ref int parameter )

Second, change the function call to look like this:
Function1( ref x );
Now you’ll pass a reference to x, and the value of x will be changed.
You should note that classes are always passed by reference. For example:
public void Function1( Int32 parameter )
In this function, any Int32 (a fictional class that I just made up) you pass into it will always
be passed by reference, not by value

No comments:

Post a Comment

Your comment is pending for approval

AngularJS Basics - Part 1

                                                                  AngularJS What is AngularJS ·          Framework by googl...