I am using Godot version 4.4.1. And I’m watching a tutorial from version 4.2.1. Now in the tutorial, it doesn’t have the float and void stuff, and I don’t completely understand what it means, and if it’s critical to have it or not have it. If someone is willing to explain what it is in very simple terms (I’ve never coded in my life before) I’d be grateful.
Hi there,
float
is the type of the “delta” parameter. Type is the nature of a variable, like floating point number, string, boolean, etc.
Specifying the type is optional, but can make the code easier to read and more safe: if no type is specified, that means you can use any type of variable, however, that can lead to errors.
For instance, consider such a function:
func test(param):
if param > 10:
print('Hi!')
That function prints “Hi” if the parameter is higher than 10. Easy, right? And what could go wrong? Well, since there is no specified type for param
, you could write that:
test('some string')
And that second line would be wrong as the function will do if 'some string' > 10
, and comparing a string to a number doesn’t work.
So, by specifying the expected type of a parameter, you ensure a more stable behaviour:
func test(param: float):
if param > 10:
print('Hi!')
In the case of the _process
function, delta
is the time that has passed since the last frame, which is a floating point number.
void
is the type of the value returned by the function.
Functions can be used to calculate some value and return it. For instance:
func square(x: int) -> int:
return x * x
That function will take an integer as a parameter, square it, and return the result. When returning a value, the syntax -> type
can be used to specify which type is returned.
But, functions can also be used to simply contain a bunch of instructions, without returning anything. In that case, you don’t have to specify anything, but, you can also specify that the function returns “void”, which means nothing.
I believe that, when writing GDScript code, it’s easier to just remove the -> void
part.
In the case of the _process
function, void
is used as this function does not return anything.
Let me know if that makes things more clear or if you need more explanations.
GDScript can be used for both static and dynamic typing. If you do something like this:
var some_variable : int = 0
Then you are explicitly telling your computer that you’re declaring a variable named some_variable
that is of type int
and has a value of zero. This is static typing.
You could also declare the variable like this:
var some_variable = 0
You’re not explicitly telling the computer what some_variable
’s type is-- you’re effectively telling the computer “figure out what the type is yourself.” This is dynamic typing.
Static typing is more performant, as dynamically-typed variables take up 20 whole bytes. It’s also good for making your code more robust, because with dynamic typing you could theoretically do something like this:
var some_variable = "hi" # holds a string
func _ready()->void:
some_variable = some_variable + 2 # Huh?
The code above will make your computer very unhappy, as some_variable
starts out as a string, but we later try to use it as an integer. If you were to try and run this code, your project would probably come to a screeching halt. Had we properly defined some_variable
to be of type string
, the editor would have highlighted the offending line of code for us, with an appropriate message saying what we did wrong.
Dynamic typing can have its uses. For instance, you could make an array like this:
var some_array = [1, "efrfergeg", 122]
Just like variables have types, you can also declare the type of a function’s return value. Example:
func add(some_value : int, some_other_value : int)->int:
return some_value + some_other_value
It is now absolutely clear what the function should return (an integer.)
If a function never returns a value, you can make this clear by declaring the return type to be void
.
Short Answer: You don’t need it.
You’ve gotten some good long answers, and it’s helpful to know in the future, but yeah just keep chugging along and don’t worry about it.
func _process(delta):
pass
. . . is exactly the same as . . .
func _process(delta: float) -> void:
pass
. . . for your purposes. (You can worry about the differences later.)
Enjoy learning how to program! It’s a lot of fun!