Home » Godot Referencing Function

Godot Referencing Function

by Online Tutorials Library

Referencing Functions

They cannot be stored in variables, passed as an argument to another service, or be returned from other functions. This is for performance reasons.

To reference a function by its name at runtime, (e.g., to store it in a variable, or pass it to another function as an argument) one must use the call or funcref helpers:

Remember the default functions like _init, and most notifications such as _enter_tree, _exit_tree, _process, _physics_process, etc. are called in all base classes automatically. So, there is only a need to call the function explicitly when overloading them in some way.

Static functions

A function is always declared static. When a function is static, it is access to the instance member variables or self. This is used to make libraries of helper functions;

Statements and control flow

They are standard and can be assignments, function calls, control flow structures, etc. (see below); as a statement, the separator is entirely optional.

If/else/elif

Simple conditions are created by using the if/else/elif syntax. The parenthesis around conditions are allowed, but not required. Given the nature of the tab-based indentation, elif can be used instead of else/if to maintain a level of indentation.

Short statements can be written on the same line as the condition:

Sometimes we might want to assign a different initial based on a Boolean expression. In this case, ternary-if expressions come in handy:

while

Loops are created by using while syntax. Loops can be broken using break or continued using continue:

while [expression]:

    statement(s)

for

To iterate through a range, like an array or table, a for loop is used. When iterating over an array, the array element h stored in the loop variable. When iterating a dictionary, the index has b is stored in the loop variable.

Match

A match statement is used to branch the execution of a program. It is equivalent to the switch statement found in many other languages but offers some additional features.

Syntax:

Switch statements:

  1. Replace the switch with match
  2. Remove case
  3. Remove any breaks. If we don’t want to break by default, we can use continue for a fall through.
  4. Change default to a single underscore.

Control flow:

The pattern is matched from top to bottom. If a pattern matches, the execution continues below the match statement.

If we want to have a fall through, we can use continue to stop execution in the current block and check the ones below it.

There are six pattern types:

  • Constant pattern:
    Constant primitives are numbers and strings
  • Variable pattern
    It matches the contents of a variable/enum
  • Wildcard pattern
    This pattern matches everything. It is written as a single underscore.
    It is used as the equivalent of the default in a switch statement in other languages.
  • Binding pattern
    A binding pattern introduces a new variable. Like the wildcard pattern, it matches everything -and also gives the value a name. It is useful in array and dictionaries.
  • Array pattern
    Every single element in the array pattern is a pattern itself so we can nest them. The length of the array is tested; it has to be the same size as the pattern; otherwise, it cannot match.
    Open-ended array: An array is bigger than the pattern by making the last subpattern.
    Every sub pattern is separated by a comma.
  • Dictionary pattern
    It works the same as the array pattern. Every key has a constant pattern. The size of the dictionaries is tested; first, it has the same size as the pattern; otherwise, the pattern does not match.
    Open-ended dictionaries: A dictionaries can be more significant than the pattern by making the previous subpattern…
    Every subpattern has to be comma-separated.
    If we do not specify a value, then only the existence of the key is checked.
    A value pattern is separated from a key pattern with a:

Multi patterns:

We can also specify multiple patterns separated by a comma. These patterns are not allowed to have any bindings in them.


You may also like