For more information on functions, read my Python post and come back here. Today, we’ll be highlighting key differences between Python and GDScript functions. But first, let’s start with the similarities.

The Similarities

Both Python and GDScript functions can have types in their definitions, similar to the variables from our last post. They also both allow you to accept as many parameters as you like. These are the staple of functions in any language. There are also a few important differences that are key to writing proper GDScript.

Key Difference

The main difference between functions written in Python vs GDScript are the func keyword and no first-class functions. Let’s get into it starting with the func keyword.

Func Keyword

In GDScript, unlike Python, the func keyword replaces def to define a function. That’s all there is to it. But as I mentioned, there are no first-class functions.

First-Class Functions

What does first-class function mean? A first-class function means that you can pass around a function as data in your program or script. There is support for this in Python, but in Godot, this is not allowed for performance reasons; in most cases, you won’t need this feature. You can, however, use a function reference at run time like so:

# Call a function by name in one step.
my_node.call("my_function", args)

# Store a function reference.
var my_func = funcref(my_node, "my_function")
# Call stored function reference.
my_func.call_func(args)
From the Godot documentation.

In this example, they use the special funcref keyword, which allows you to mimic first-class function behavior. Next thing is, you have to choose the arguments that you will pass into the function along with using either In most cases, you will not need this capability as it’s not as flexible as you might like it to be.

With that said, I hope highlighting these key differences helps you and brings you closer to making your dream game! Stay tuned as next time’ we’ll start talking about classes in Godot on multiple levels.

%d bloggers like this: