Tuesday, 17 September 2019

Programming 17/9 - 27/9

joe.simmonds@sit.ac.nz - contact email.

Intro to Programming

Script has 3 elements: Variables, Functions and Methods.

Variables

  • Programming deals with a lot of data
  • Data could be numbers, words or true/false
  • ie. string playername - "Joe"
    • Strings are variables that can contain letters, numbers and other characters
      • Can not be used for calculations
      • Used for names, text, passwords
    • Int
      • short for integer
      • variable for storing numbers with no decimal place eg whole numbers.
      • used for Mario style lives e.g 0 to 3
    • Float
      • Variable used for storing numbers that can have a decimal place
      • Used for RPG style health where it can be between 0 and 100% eg 95.24%
    • Bool
      • Short for Boolean
      • a variable that has one of two possible values
      • True or False
Functions
  • Sets of code that runs at certain times depending on what language you are using
  • Common functions are
  • Start() which runs at start of script
  • Update() runs on each frame refresh (fastest script can run)
  • FixedUpdate() runs on each fixed rate frame refresh. (think movement speed)
Methods
  • Commands can be wrapped into a set of instructions called methods
  • e.g Step 1: Take Bread
  • Step 2: put in toaster
  • Step 3: turn on toaster
  • Can be wrapped into a method called MakeToast
  • This saves us having to re-type the same steps over and over
Syntax
  • The specific way we write the code
  • Similar to english language using punctuation
  • Computer knows a line of code is complete with a semi colon (;)
  • A simple out of place ; can stop all your code from running so it is important to understand the programming language syntax
  • C# variable syntax example: private string playerName - "Joe";
  • It is important that strings have " " around their values. Thats how we know its a string
  • Private must be lower case p
  • Lower case = methods, Upper case - variables
  • The string must be written how it will be referenced later in the script
  • Public variable can be accessed by any script in your project, private can only be accessed by the script the variable is written in.
  • More examples of variable syntax
    • public float playerHealth - 100;
    • public bool isJumping - false;
    • private sprite heart1In;
    • private sprite heart1Out;
  • Function syntax
    • 3 main functions, Start(), Update() and FixedUpdate()
    • written as void start(){    code goes here        }
    • void means this script wont return a result
  • Method syntax
  • void inputs(){    player input related code goes here       }
  • this can also be private or public
  • once methods are written, we have to call it in our script. this is usually done within one of the main functions
  • inside of our function, we type the name we called our method followed by () and a ;
  • e.g Update(){          Inputs();         }
Extra bits of syntax
  •  = symbol tells the variable what it is
  • == is checking whether the variable equals something e.g If(health == 0)
  • if statements check the value of something before we run a piece of code.
  • !jumping can replace jumping = false
if(health == 0){
             
If else statements - similar to an if statement but has additional conditions and codes if the first part isnt true.

if(velocity.y == 0){
             OnGround = true;
}else{
             OnGround = false;
}

If the first line is met, the second part wont run.

&& symbol is used if we are trying to check more than one variable before running some code.
e.g if(health == 0 && score == 100) both variables must be true to run the code.

|| symbol means "or". This is used to check if one variable or another is correct before running the code.
e.g if(health == 0 || score == 100) means code will run if health is 0 or if score is 100.

Writing variables quicker
  • in most cases bool variables can be shortened 
    • e.g 
Putting into practice


19/9

Collectables and Globals

Collectables have two functions, score and economy.
Collectables for score is simple, the more u need, they harder they get. Incorrect use of collectables can break a game economy. Too easy to get, and the game becomes too easy. This makes the game boring too quick.
Doom(1993) has one of the best approaches to collectables. A lot of the level puzzles are directly related to shortage of ammo and being able to complete the level. Alternatively like in Gears of War 5(2019) u get the big gun right away but with very little ammo so the player has to be picky about when they use it.
Same with health packs, too many means theres no fear of dying. You can restrict how many the player can hold, or how many per level.

All UI text, buttons sliders etc must be on a canvas.

Text in Unity
Must be on a canvas system to display. Text Mesh Pro system or standard unity text ui system but the standard system is pixellated and low quality. Must import using TMP Essentials.

Buttons can be custom import, or can use the built in unity ones.
Two ways of dealing with imported buttons, standard button sprite or 9 slice sprite. 9 slice sprite splits the button into 9 sections, with the 4 corner sections not being modified when resizing to keep the resolution.

Unity projects are made of scenes, each scene is a level, or a main menu, settings menu, intro splash screen. Scenes can be linked using a scene manager but first need to go through Build Settings and make sure the scenes we want included and what order they are, are showing in the build screen. SceneManager must be included in our script to be able to use it.
using UnityEngine.SceneManagement;

Any script in the build must include this at the very top of the script. Within the script all you need to do is add the code
SceneManager.LoadScene(1);
or
SceneManager.LoadScene("MainScene");

We can use either the name(string) or the index(number)


No comments:

Post a Comment

BSA702 14/7

 arrays and lists Quick and Easy Galaxy painting  great tutorial I found when I was looking for a background for my pitch tomorrow. I want t...