User Tools

Site Tools


advanced_unity_programming_concepts

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

advanced_unity_programming_concepts [2023/02/07 12:30] – created nkassaiadvanced_unity_programming_concepts [2023/02/21 09:13] (current) nkassai
Line 11: Line 11:
  
 In Session #3 of the Unity crash course, we will cover the following: In Session #3 of the Unity crash course, we will cover the following:
 +  * Functions
   * Classes   * Classes
   * Scope Access Modifiers   * Scope Access Modifiers
Line 23: Line 24:
 Please make sure to finish the last session titled "Learning the Language of Unity" before starting this session! The previous session gave you a crash course in basic C# programming and we wrote a few scripts together to demonstrate those concepts. If you have already completed the previous session, you may proceed. Please make sure to finish the last session titled "Learning the Language of Unity" before starting this session! The previous session gave you a crash course in basic C# programming and we wrote a few scripts together to demonstrate those concepts. If you have already completed the previous session, you may proceed.
  
 +===== Functions =====
 +
 +Throughout this tutorial, we have utilized **Debug.Log()** to print out information to the terminal. Log() is known as a **function**, which is pre-defined code designed to be reused and sometimes **return** a value. Additionally, **Start()** and **Update()** are also functions which we have been using throughout this tutorial. However, Start and Update do not have any definitions, rather, they are called by Unity at specific intervals (Start is called once the scene starts, and Update is called every frame). We get to define them with our own code! 
 +
 +Let's say we wanted to write a script that would print out the value of two integers, and then their sum. Let's create a new script called functions and write this code in Start. 
 +
 +{{ :function_intro.png?910 }}
 +
 +Now, let's save this script, remove the Loops component from the Cube, and add our Functions script to it. Then, play the Unity scene!
 +
 +{{ :functions_active_1.png?910 }}
 +
 +Now, this script works out no problem, but what if we wanted to repeat this process again in Start? Well, we would have to basically copy and paste the code again to get the following result:
 +
 +{{ :no_function_problem.png?910 }}
 +
 +While this will still work, the code is starting to look a little messy. What if we wanted to repeat this process 10 times? 20? 40? Well, our code would start becoming hundreds of lines long, even though the same 10 lines are repeating over and over again! Let's place the repeated block of code in a function. As I've mentioned previously, functions are used to help reduce code redundancy, but they also have the additional ability to **return** a value.  Whenever you declare a function, you also have to declare it's return type:
 +
 +**dataType FunctionName(parameters){**
 +
 +reusable code
 +
 +**}**
 +
 +For this purpose, we will use the return type of **void** which just means that we do not want this function to return a value. I'll explain this more as we write more functions, but for now, write the following block of code above start:
 +
 +{{ :functions_active_2.png?910 }}
 +
 +In our function, we have defined two parameters, two integers, a and b. We then have the same block of code as before. Now, if we want to be able to use this function in our code, write the name of the function in Start, pass in the **correct** parameters and save. **Note:** When I say correct parameters, that means the number of parameters and datatypes must match the function! If you pass in three integers, or an integer and a string, our script won't be able to find what function we are talking about.
 +
 +Now, go ahead and run our Unity scene to see that we get the same output as before. The difference now, is that if you wanted to run that block of code again, all you have to do is write one line instead of the whole block! 
 +
 +Let's change the return type of the function to be an int instead of a void. Once we do so, we get an error saying that this function needs to return something! Instead of printing out the value to the console, let's just return the value of sum.
 +
 +{{ :functions_active_3.png?910 }}
 +
 +If we were to run this code now, we wouldn't see what sum equals to! However, since this function returns a value of int, we can actually assign the function to an integer variable!
 ===== Classes ===== ===== Classes =====
  
advanced_unity_programming_concepts.txt · Last modified: 2023/02/21 09:13 by nkassai