Godot Tips and Tricks
Share on

Godot Tips and Tricks

Tags

Oh hi there 👋 dont miss out by subscribing.

Hey what’s up Friends, This time around I simply want to shower you with Tips and Tricks for the Godot Game Engine. Enjoy.

Contact me if you have some Tips and Tricks you are missing from this page.

Run Multiple Instances

Useful when testing Multiplayer Games, where one instance acts as the Server and the other as a Client.

Color Picker in Code Editor

You can get a Color Picker for Color Objects by Right Clicking it.

Set Custom Asset Library Sources

Got to Editor Settings > Asset Library > Available URLs.

"Speed up" your Code Editor

When Indexing your Code Files Godot will wait for 2 Seconds after you made the last change. You can lower this time in Editor Settings > Text Editor > Completion > Idle Parse Delay.

Custom Doc Comments (##)

If you did not know, Godot will create custom Documentation for your Script Files

Button Groups

You can add Buttons to groups so that only one of them can be pressed at a time, which radio button behavior.

var buttonGroup = ButtonGroup.new()

...

button.toggle_mode = true
button.button_group = buttonGroup

Can also be added via the Properties Panel.

Button Shortcuts

You can add Shortcuts to Buttons either via the Properties Panel,

or through code.

var shortcut = Shortcut.new()
var key = InputEventKey.new()
key.set_keycode(KEY_V)
shortcut.events = [ key ]
button.shortcut = shortcut

Rect2

The Rect2 Class is very useful for 2D Area Code, for example you could use this class to find out if a point is within an Area.

var rect = Rect2(0, 0, 100, 100)

print(rect.has_point(50, 50)) # Yes
print(rect.has_point(-10, -10)) # No

Stepify Numbers

You probably know clamp but there is also snapped which takes in a number and a snapping amount and it will snap it to that range.

var number = snapped(55, 20)

# Will be 60

Setting SpinBox value without signal.

The SpinBox class will emit the value_changed signal even if the Value was changed via code, you can avoid this by calling set_value_no_signal on the SpinBox.

Class Names

You can create globally available classes/types by using the class_name keyword within your scripts.

These Scripts will be available everywhere by their name, keep in mind that this does not load scenes but scripts.

Dragging Dropping Node References

You can quickly drag and drop node references into the code editor to get the Path of the Node, By holding CTRL it will even create an onready variable for you.

Adding Submenus

If you want add a submenu you will have as you probably already thought to create another PopupMenu and assign it to another Menu.

var alignMenu = PopupMenu.new()
alignMenu.name = 'align'
add_child(alignMenu)

...

add_submenu_item('Align Option', 'align', 2)

It is important to add a name to the submenu as this info will be used internally by the Engine.

Global Scripts / Singletons

You can add Scripts as Singletons so they’re globally available.

Do this by going into Settings > Autoload

Access them by their given name.

Anti Aliasing Settings

is_instance_valid()

Check if a reference to a node has been freed with is_instance_valid(node)

Adding Shortcuts to Menus

Adding shortcuts to Menus involves just like with buttons first creating a shortcut and then setting it to a certain item in the menu.

add_item('Toggle', 0)
set_item_shortcut(0, toggle_shortcut)

Typed Arrays

You can type arrays.

var node_array: Array[Node] = []

Moving Children

You can move child nodes within their parent with move_child. To find the index (position) of a child within a parent use get_index on the child. This for example useful when dealing with Split Container where you want to flip the order of panels.

Typing Signal Arguments

You can add types to signal arguments.

signal on_object_added(node: Node2D)
signal on_object_removed(node: Node2D)
signal on_object_renamed(node: Node2D)

Pausing and Not Pausing

You can use get_tree().paused to pause and continue your game. This will make it so the _process functions wont be called.

For more fine grained control on how Nodes behave we can set their Process Mode.

Shortcuts

Everything is better with Shortcuts -> Godot Keyboard Shortcuts (maximmaeder.com)

Commonly Needed Functions

Root Element

get_tree().root.get_child(-1)

Viewport Size

get_viewport().get_visible_rect().size

Active Viewport Control Element

get_viewport()

Current Mouse Position

get_viewport().get_mouse_position()

More Helpers

For more Helpers you take a look at my Godot Plugin.

Other Articles

Other Posts you may like ...

Highlighted Dummy Code Editor with HTML CSS JavaScript (vue.js)

Highlighted Dummy Code Editor with HTML CSS JavaScript (vue.js)

Creating Menus with Godot 4

Creating Menus with Godot 4

Autocomplete Input with JavaScript/Typescript

Autocomplete Input with JavaScript/Typescript

To Top
© 2022 - 2024 Maxim Mäder
Enthusiast Shui Theme Version 44