Saturday, 30 August 2014

[C#/CSharp] Variables

C#/CSharp Tutorial

What is a var?
A variable is something that defines things. You must try to use as least as possible unless you want your code to be massive and super confusing.

Types of variables
  • boolean (true/false)
  • int (numbers without decimals)
  • float (numbers with decimals) -- this should be one that you use a little more.
  • String (text)

There are way more variables - but this is just a few examples of the ones we use.

public vartype name = number;


  • public = (Optional) This is so you can change it in the object that you added the script to, eg. The check button for boolean appears on the object under the script.
  • vartype = The variable type. This is very required, see "Types of variables" adobe.
  • name = The name of the variable
  • number = (Optional) We mainly use ints and floats for a number.

Example:

public float maxHealth = 100f;
public float currentHealth = 100f;

It's best to make these public so we can change them in the object (as in you can type what number you want)


Where are some places we use vars?
We use vars in many places in C#, such as:

function VARIABLENAME { // VARIABLENAME = the variable name
if(VARIABLENAME <= 0) { /* if the VARIABLENAME is under or equal to (<=) 0 */
   /* a bunch of code we can use if variable name is under or equal to 0 example: */
   Application.LoadLevel("Respawn"); /* this makes the level named "respawn" load when your health is under 0 (death) */
}


We also use variables in other places, such as:

if(currentHealth <= 0) { // if your current health is under 0
currentHealth = 0; // set current health to 0
}


Here is also a good place that most of you are gonna use if you have a float / int such as levelling up, stamina, mana, health etc. I use it with health.

if(currentHealth >= maxHealth) { /* if current health is above or equal to (>=) max health */
   currentHealth = maxHealth; /* make current health your max health so it doesn't go above your max health. (default 100) */
}


That's it for this small tutorial, bye!

No comments:

Post a Comment