Download presentation
Presentation is loading. Please wait.
1
CIS125GA Week 4 Logical Beginnings
2
Contents: Slides Mathematical Operators Logical Operators Nesting Examples Functions Snippets: Calling(); Random.Range Set.Active
3
Mathematic Operators & Expressions
An EXPRESSION is a Mathematical Operation. For asking a Method Math Questions, We simply use math symbols and abbreviations, such as: / Division * Multiplication + Addition - Subtraction % Modulus And ask it in the same way we would write a Math Problem: Move Bunny Forward the distance of : ((4*8)/2)
4
Shorthand for Mathematical Operations
There is Shorthand too! ++ Increment by 1 -- Decrement by one Timer++ is the same as writing Timer = Timer + 1 Combined Operators like += = *= /= %= Perform a calculation THEN reassign the new number to the variable. X +=Y is the same as writing X = X+Y HP -= Damage is the same as writing HP = HP - Damage
5
Relational Operators We can ask a question about how 2 things in our world relate to each other. These answer as True or False, known as RETURNING BOOLEANS! We use symbols for this as well. > Greater than < Less Than >= Greater than OR equal to <= Less than OR equal to && And || Or But because the = sign is used to assign a value, like in KillerBunny.Fur = White we can’t use an “ = “ to show equality. Instead we have: == (It’s really really Equal to!) != (OMG! It’s not Equal!) ! (Not!)
6
Compare and swap like types
Variables have to be the same type to compare. Variables of the same type can also be swapped out / assigned for each other. Valid: (HitPointInt = ManaPointInt) Valid: (GameObjectPlayer = GameObjectPotato) BAD: ( GameObjectPlayer = HitPointInt) You can check if a string is the same as another string (string == string) But not if a GameObject is a String BAD: (GameObject == String) However, the NAME of a Gameobject is a String, you can compare that! Valid: (GameObject.name == string) Valid: ( Int >= Int) Valid: ( Bool != True)
7
Nesting We can also NEST code by putting a piece within a piece within a piece within a dream within a dream within another block of code. (You will do this a lot!) (BTW, this animation looks terrible) Note how Alice staggers the code & uses colored regions to help you see the nesting and control structures Example:
8
Nesting With Syntax Pay Special Attention to the Brackets. They MUST bookend each IF’s Instruction set. (Disclaimer : I made up this code to make it easier to read, but still get the point across..) Void update () { If (Lancelot is in the forest) If (The Grail Shaped Beacon is on) Do these Instructions; } Else
9
Example Time! #1 Nesting The nested IF goes in the instructions of the condition that has to be satisfied first. IF ( Weather != Raining) { Run outside in Undies; } Not raining doesn’t mean sunny. It could be snowing. It could be hailing. It could be the apocalypse. All bad Undie-running weather. NEST AN IF to check a second condition. IF ( Weather != Snowing) You can nest as many times as you want.
10
Example Time! #2 We can also do this with an else.. And pants..
If ( WeatherYucky == True) { if (Weather == Rain) Bring Umbrella; } else // Meaning the weather IS yucky, but is not rain Put on Pants; Else // Meaning the weather is NOT yucky Run outside in Undies;
11
Example Time! #3 If you have a dice & Coin available, play along!
// Int DiceRoll creates a disposable variable especially for this block of instrcutions, and assigns a random number between 1-7 to it. For Random.Range, the minimum number is Possible in the roll, but it will only go up to the number before the maximum. //Flip Coin If (coinFlip == Heads) { // If condition is satisfied, do these instructions: //Roll Dice Int DiceRoll = Random.Range (1,7); If ( DiceRoll > 3) Erika Wins!; } Else // Meaning the dice rolled 3 or less) You Win!; else //meaning coinFlip == Tails You Escape!;
12
Example Time! #4 Math and assign!
The combination operators -=, +=, /= and *= are really handy shorthand in gaming situations. MyHitPoints -= FireBallDamage; The code takes MyHitPoints int MyHitPoints = 25; Subtracts FireBall Damage int FireBallDamage = 5; And reassigns the number to the variable MyHitPoints. Now int MyHitPoints = 20; This can be done with any mathematical operators * / %
13
Functions Methods / Functions: A named section of a program that performs a specific task. All Methods are functions. Methods are specifically functions that are owned or associated with an object, so in object oriented programming (OOP), all functions are methods.
14
Making Functions function
Return type- Functions need to know what kind of data you want to get back from them (Final product). If you want nothing back, you put void as a null data type. Name- You need to name your functions. Name them logically to save pain and suffering later. () Container- Sometimes we PASS information to a function. We need to put in a container even if we have nothing to give it right now. {Curly Braces}- Tell us where the instructions begin and end, so we know what instructions are owned by what code. void BestFunctionEver () { // Brackets Bookend the instructions Do these Instructions; } Unity has built in functions with names that are Reserved, like Start, Update, FixedUpdate etc. Built in functions work in specific ways.
15
Calling(); You cannot make a function inside another function. If you need a functions instructions to run inside another function, you call it. Call it by name, add a container() (in case you want to give it some information) and add a SemiColon; at the end, because it’s an instruction. This code runs; Then we call and THAT code runs; BestFunctionEver(); Then we continue here, where we left off;
16
Random.Range (Min, Max);
Random.Range picks a randomized number (Think of it as a dice roll!) It must be assigned to either an INT or FLOAT variable. It needs information to run: Minimum number in range Maximum number in range When using a FLOAT, maximum is inclusive When using an INT, maximum is exclusive, meaning the roll can go UP to that number, but not include that number. MyHP = Random.Range (1, 21);
17
****.SetActive(bool);
Sometimes, you just want things to disappear. In Unity, you can toggle if something is ACTIVE in a scene or not. .SetActive requires a Boolean. Erika.SetActive(false);
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.