Coding notes - Day one
Here are the notes about coding
- Computers don't know what to do, until we tell them. These commands could be simple and like a step by step guide or a recipe.
- Commands could also be a little more complicated with conditions and logic. Like only used if we are hungry.
- To tell a computer what to do...
- We need to understand three elements of a script. Variables
- Variables. When programming, we need to deal with lots of data. We store this data inside variables to be called on at a later stage.
- There are four main variables. Strings, Int. Float, and Bool.
- Strings. Can contain letters, numbers and other characters. Can not be used for calculations. Used for names, text, passwords, etc.
- Int. Short for Integer, used for storing numbers with no decimal place. No dots, not 1.1. Used for Mario styled lives like 3 to 0.
- Float. Used for storing numbers that can have decimals. Has dots, can be 1.1. Used for RPG style health where it can be between 100% and 0%.
- Bool. Short for Boolean. It has one of two possible values. True or False.
- Functions
- Basically a set of code that runs at certain times depending on what programming language you are using. Any commands we put inside Start will never happen again.
- Common functions, Start(), runs at the start of a script. Update(), runs on each frame refresh. FixedUpdate(), same as Update but has a fixed frame rate refresh.
- Methods
- Commands can be wrapped into a set of instructions called methods. We can reuse this every time we want it.
- Synatx
- The specific way we write the language a computer understands. Similar to English, we know the end with the (.) The computer knows a line of code has ended is by the (;)
- It is important to understand the programming languages syntax as a simple out of place (;) can stop your whole 1000 lines of code from running.
- E.g Example of C# for variable private string playerName = "Joe";
- Notes. Strings have quotes around their values (""). Private must have a lower case (p). Write the same (n with N and p with p). Must end with (;).
- E.g's
- public float playerHealth = 100;
- public bool isJumping - false;
- private sprite heart1In;
- private sprite heart1out;
- Difference between public and private. Public can be accessed within any variable in the entire script. Private is by the script the variable is in.
- E.g's
- void Start(){
- }
- void Update(){
- }
- void FixedUpdate(){
- }
- This shows the start and the end of the code/function. Put what you want within the space
- E.g's
- void Inputs(){
- }
- private void Inputs(){
- }
- Can have private or public
- Now we have the method, we need to call to it. It is waiting to be referenced. Usually done within one of the main functions. Start() Update() or FixedUpdate().
- E.g's
- Update(){
- Inputs();
- }
- Extra bits of C#
- (==) symbol. We can use this to check if a variable equals something. E.g, If(health ==0)
- (If) statements. Use if statements to check the value of something before we run a piece of code
- E.g If(health == 0){
- GameOver();
- }
- If the health is not 0 that it will not run
- (If else) statements. Like If but has additional conditions and code if the first part is not true
- E.g If(velocity.y == 0){
- OnGround = true;
- }else{
- OnGround = false;
- }
- If the first part of the if statement is meet, the other half will not run
- (&&) symbol. We use this if we are trying to check more than one variables before running some code
- E.g If(health == 0 && score == 100)
- (||) symbol. Means 'or'. We use this to check if one variable or another is true. Only one needs to be true to work
- E.g If(health == o || score == 100)
- In most cases writing bool variable can be shortened.
Comments
Post a Comment