Godot Engine: GDScript Cheatsheet#
Basics#
Comments
# this is a comment
"""
This is
a multi-line
comment
"""Variables
# structure : var [name] : [type] = [value]
var this_is_a_variable : int = 10Functions
func this_is_a_function() -> void:
print("example")Conditionals / If-else
if true == false:
print("first if")
elif false == true:
print("Else if")
else:
print("else")Built In Basic Node Functions#
Call a Init Method (Similar to Start() in Unity)
func _ready():
print("Method Ready Called")Call a game loop Method (Similar to Update() with Time.deltaTime in Unity)
func _process(delta):
print("Method Process with Delta Called")Call a game Physics loop Method (In Unity is like FixedUpdate() with Time.fixedDeltaTime )
func _physics_process(delta):
print("Method Physics Process with Delta Called")Public and Private Nomenclature#
Apply an _ (underline) for private methods, and none for public methods
# Private
var _my_private_variable : int = 10
func _my_private_function():
pass
# Public
var my_public_variable : int = 10
func my_public_function():
passScene Management#
Change Scene
get_tree().change_scene_to_file('res://'+nextScene+'.tscn')Reload Scene
get_tree().reload_current_scene()Quit Game
get_tree().quit()Arrays#
Initialize Arrays
var _local_variable: Array[String] = []Resize Arrays
var _local_variable: Array[String] = []
_local_variable.resize(10) # now it has length = 10Node Management#
Show and hide object
# hide Node
selected_node.hide()
# show Node
selected_node.show()Engine Interface Management#
@export calls in the editor a reference to a variable (Similar to [SerializeField] in Unity)
@export var animation_player_path : NodePathSet a custom script icon
# Must have an image for the icon and call it this way
@icon("res://Theme/Icons/vibration.png")
class_name VibrationArea2D extends NodeSignals#
Add signal to an script
# structure : signal [signal_name]
signal timeoutCall an existing signal to be emmited
# structure : [node].[signal_name].connect([function_to_be_called])
timer.timeout.connect(_on_timer_timeout)
func _on_timer_timeout() -> void:
pass