Saturday 30 August 2014

[JavaScript] GUI Buttons

JavaScript Tutorial

Today we are going to be looking at GUI Buttons.

What is a GUI Button?
A GUI Button is a button that appears on the screen when you put a position and text box in, don't forget to include the size.

Where does a GUI button have to be?
GUI buttons need to be inside function OnGUI() for it to work.

An example of a GUI button:
GUI.Button (Rect (0, 0, 262, 20), "I am a button!");
This a GUI button that appears in the top left of the screen that sais "I am a button!"

Variables with GUI Buttons:
This is also a very good way of using buttons, let's say we want to display an amount of cash to buy something with our button, it will appear.
var price : float = 10f;

function OnGUI() {

    GUI.Button (Rect (0, 0, 262, 20), "Buy me for $" + price);
}

Now it should appear as "Buy me for $10!" in a button, but you notice you cannot click on it. We will get into that next.

Making buttons do things:
To make buttons do things, we need to add an if() { to the button, here's an example:
if(GUI.Button (Rect (0, 0, 262, 20), "Buy me for $" + price)) {
  // place stuff here
}

Now if we click on it, nothing will happen, let's change it to something where it loads a level:

if(GUI.Button (Rect (0, 0, 262, 20), "Buy me for $" + price)) {
  Application.LoadLevel("LEVELNAMEHERE"); /* loads the level names "LEVELNAMEHERE" */
}

Now if you have a level called LEVELNAMEHERE on your game, it will load that level.

That's it for this java tutorial, bye!

[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!