A Brief note on Variables — The fundamental Building Blocks in Programming
Before we get our player cube to move via user input we need to discuss a fundamental tenet of computer programming and that is the illustrious Variable.
So what is a variable? The simplest way to define them are as boxes that can hold different types of information.
Each box can hold certain types of information (Data type), the ones we will cover today are: string, float, int and bool. Let me run over what each of them are.
String — Can be any combination of characters and symbols as long as they are within quotation marks. eg. “I am really #$%#$%@ mad about my poor score of 67%.”.
Float — A float can be any number with a decimal (yup to 7 digits) If you need more precision than that then you would use a Double which allows up to 15 digits. eg. 456.6798
Int — An Int or integer is any number either positive or negative eg. 156784 or -156784 .
Bool — A bool is either a true or false value.
When declaring a variable they must have three elements with an optional fourth. Those are:
Public or Private variable. A public variable will be visible and thus usable by other scripts in your code (Global) whereas a private variable will only be usable within the class that they are declared in.
Data Type are the String, Bool, Int and Float that we covered above. Once a variable has be declared a certain type it cannot be changed to another type.
Name You have to name your variable something unique and meaningful to the purpose and context of what it will be used for. If you are creating a variable that stores a users age then you should name it something like _playerAge. The _ at the beginning of the variable is not required but is considered to be good coding hygiene so that your variables stand out and makes your code more readable by yourself and other developers if you are working collaboratively.
Value This is optional. If you need a variable to start with an initial value then you can put it in otherwise each datatype will have a default and the whole purpose of a variable is to …vary it will change based on how you manipulate them in your code.
There you have it. a quick and dirty primer on variables! Until next time. 🤙