Home » #gamedev » Variables, Functions, and Conditionals Made Easy

Variables, Functions, and Conditionals Made Easy

Programming is powerful. It gets its power from variables, functions, and conditionals. A variable is storage for dynamic information. A function is one or more instructions that perform a specific task. A conditional expression decides what should be done in the event that specific information meets particular criteria such as being true or being a particular number.

A variable is often likened to a container in which information is stored and from which it is retrieved. As its name implies, that information is variable; that is, it can change as the program runs. In a video game a character’s health is often represented by a number from 0-100. This health number would most likely be stored as an integer variable named, you guessed it, “health.” The value of “health” will change throughout the game depending on, among other things, how many shovels the character is hit with.

A function is comprised of one or more instructions. It’s a re-usable mini-program of sorts. A function’s instructions might involve subtracting the number 20 from a character’s “health” variable every time it got hit with a shovel. More likely, instead of 20, the function would subtract the value of the variable “damage” from the variable “health.” That way the variable “damage” could be updated according to the kind of shovel the player is hit with. A coal shovel would do more damage than a snow shovel which, in turn, would do more damage than one of those little plastic sand-castle shovels

A conditional determines what will happen as the result of new information. For example, a conditional might continuously check to see what a player’s “health” is, and, if it’s 0 (or less), decide to play a death animation and end the game. Otherwise, it would continue to check until and unless the character meets its grizzly demise at the hands of a shovel-wielding maniac. Video games are pretty violent these days…

Functions and conditionals are and do whatever the programmer tells them to be and do. Functions can be custom-made or sourced ready-made from libraries. Variables, however, come in specific types. Here are some important variable types;

  • Integer: use this type when dealing with whole numbers

  • Float: use this type when dealing with numbers that have decimal places

  • Vectors 2 and 3: use these types when dealing with two or three numbers at a time, for example, when dealing with 2D and 3D coordinates

  • Boolean: use this when storing a true or false value

  • String: use this when you want to store text (note that “15” stored as a string is the word 15 spelled with numbers and not the number 15)

There are other variables as well, some of which are specific to certain programming environments, but learning these is a good start. Understanding the concepts behind variables, functions, and conditionals is the foundation for a strong programming education!

I hope you found this content useful. If so, please like and subscribe and consider contributing to
my brand new Patreon
so that I can continue to produce great #gamedev and #game art content while battling cancer!

brand new live streaming!
featuring #gameplay, #gamedev, and soon, live #gamedev lessons!

Watch live video from nickelcitypixels on www.twitch.tv

Be sure to subscribe to the
nickelcitypixels youtube channel
so that you don’t miss out! I hope to get to 500 subscribers by Oct. 5th, 2018 (exactly 1 year after my diagnosis)!

Ditto the blog newsletter!

I'm active on twitter, username
@NickelCityPixel.

And finally, I've recently launched
openforcommissions.com.
It is a central site for game developers to commission artists, and for artists to get paid. Currently it is invite only, but you can increase your chances to participate by linking your portfolio in the blog post comments!

Thanks as always for your support!

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*