Home Blog Gaming

GDScript Variables and Dynamic Typing

Godot Tutorials > GDScript Variables + Dynamic Typing

GDScript Variables

Variables are used to represent an unknown value. The value of a variable will be determined by your game code. For a more indepth explantion of variables check out PHP Variables, Constants, and Datatypes.

A variables's datatype defines the nature of the data stored within. The main datatypes featured in GDScript are:

int: This is an integer. Integers are whole numbers and they can be possitive or negative.

float: A float is a decimal. If you want fractional amounts to be included in the value, you will use the float datatype.

bool: A bool or boolean variable has a value of true or false.

string: A string's value is equal to text. When declaring a variable, string values are always put inside quotation marks.

Video: Variables, Dynamic Typing and Indentation Blocks

Now let's look at some GDScript code examples. If you are new to computer programming the following examples will make the concepts much more clear.

Mouse Hover over the blue text in the description to highlight the code.

  # Integer Variable
  var number = 666
  # Float Variable
  var num = 6.66
  # Boolean Variable
  var winner = true
  # String Variable
  var king = "Hatnix"

➼ Integer - The variable is named number and its value is equal to 666
➼ Float - The variable is named num and its value is 6.66
➼ Boolen - The variable is named winner and its value is true.
➼ String - The variable is named king and its value is Hatnix. Notice Hatnix is put in quotes.

Dynamic Typing

The examples above are called variants. GDScript is a scripting language based on Python and just like Python, GDScript uses dynamic typing. Typing a variable means when you declare the variable you also declare its datatype. Dynamic typing means you can either type or not type your variables. Variables that are not typed, such as the examples above, are called variants.

Variable Typing

There are two different syntaxes for typing your variables:
(1) Explicit
(2) Inferred

Now let's look at the code

  # String Variable: Typed Explicit
  var queen: String = "Gänseblümchen"
  # String Variable: Typed Inferred
  var flower := "Daisy"

➼ Explict Typing - The variable queen explictly states that the variable is a string.
➼ Inferred Typing - The variable flower does not explicitly say it is a string, but it is still typed. The data typed is infered by the data type of Daisy

In the examples above, the values of queen and flower must always be strings. Otherwise, your code will return errors. This is the advantage of typing. If you have a bug in your logic that sets a variable equal to the wrong datatype you will get an error message prompting you to fix your logic.

Integers and Floats

The advantage of variable typing is most clearly seen with float vs integer. Let's say you have a variable called score which holds a player's current score. You will probably want score's value to be an integer. It is fairly easy to accidently formulate a scoring function that returns a float. Typing your score variable will pervent this kind of error.

  var score: int = 0

Varients sometimes return unexpected results.


Declare the following variables in GDScript:
  1. A string variable that holds a player's name.
  2. A float variable that holds the distance a player drives.
  3. An integer containing a player's current level
  4. A variable that counts the number of living enemies.
  5. A variable that indicates whether or not a level is completed.

Want Commander Candy to Grade your Homework?


Twitch: daisychaincosplay Live Coding!!!
Learn PHP on Linux with Commander Candy
Coding Commanders YouTube
Coding Commanders Twitter
Learn HTML on Linux with Commander Candy
murks - Linux Game Dev
How to Install Dragon Age Orgins on Linux by Hatnix
Linux Gaming Blog Introduction
Sexy Linux T-shirt
Coding Commanders Twitter
Kingdoms and Castles Linux Review by Hatnix
Previous Lesson | Next Lesson