Godot Tips

In this read, I'll list down the Godot tips I have learned over time.

Godot Tips

Executing scripts in the editor

Below points to run the script in the editor are for Godot 4

  • make a new script that inherits' from EditorScript

  • add @tool keyword at the top of the script.

  • add func _run() magic function, this would be the entry point for the script.

  • after writing the code, save the script and to execute the script in editor press ctrl+shift+x

Python decorators in Godot 4.

To have (somewhat) the same functionality as python decorators in godot let's first understand what python decorators do on the ground level.
let there be a function parent_fun that accepts at least 1 argument which would be the reference of another (child) function be that be child_fun other than this argument more parameters could be passed, (we'll talk about that after the basics)
here is how function definition in python would look like

def parent_fun(child_fun): # child_fun is a reference of another function

inside parent_fun whatever needs to be done before or after executing(calling) the function can be done for example here we'll just print "Parent function" and then we call the child_fun.

def child_one():
    print("I am a child")

def parent_fun(child_fun):
    print("Parent function")
    child_fun()

parent_fun(child_fun) # this way of writing is more understandable to me

# to use @ symbol when using decorators orto make it more pythonists

def return_parent(fun):
    def parent_fun():
        print("Parent function")
        child_fun()
    return parent_fun

@return_parent
def child_two():
    print("I am a child")

child_two()

# both will print same output

Note: parent_fun(child_fun) is the way we'll be using in Godot 4

With this, we get the basics of python decorators now let's see how we can use this in Godot 4 and we'll go more in-depth in Godot
Here is the same code in Godot 4

Now, let's say you want to pass some arguments in child_fun that can be done as well we can use callv function to pass arguments in an array.

Note: callv accepts an array of all the arguments.

Note: name property in line 15 is passed in an array

I generally use the above technique more as a context manager of python than a decorator.