Basic Syntax

Operators

Arithmetic

  • Addition: a + b
  • Subtraction: a - b
  • Multiplication: a * b
  • Division: a / b
  • Modulus: a % b
  • Power: a ** b

Comparison

  • Equal: a == b
  • Not equal: a != b
  • Less than: a < b
  • Less than or equal: a <= b
  • Greater than: a > b
  • Greater than or equal: a >= b

Logical

  • And: a and b or a && b
  • Or: a or b or a || b
  • Not: not a or !a

Bitwise

  • Bitwise AND: a & b
  • Bitwise OR: a | b
  • Bitwise XOR: a ^ b
  • Bitwise NOT: ~a
  • Left shift: a << b
  • Right shift: a >> b

Assignment

  • Assign: a = b
  • Add and assign: a += b
  • Subtract and assign: a -= b
  • Multiply and assign: a *= b
  • Divide and assign: a /= b
  • Modulus and assign: a %= b
  • Power and assign: a **= b
  • Left shift and assign: a <<= b
  • Right shift and assign: a >>= b
  • Bitwise AND and assign: a &= b
  • Bitwise OR and assign: a |= b
  • Bitwise XOR and assign: a ^= b

Constants

  • Declare a constant: const MY_CONSTANT = 100

Static Variables

Static variables belong to a class rather than an instance of the class. To define a static variable, use the static keyword:

  • Declare a static variable: static var my_static_variable = 30
  • Change the value of a static variable: my_static_variable = 40
  • Since statics belong to the class, you can also use MyClass.my_static_variable = 40

Arrays and Dictionaries

GDScript also provides built-in data structures like arrays and dictionaries:

  • Array: Ordered list of elements
  • Dictionary: Key-value pairs

Variables

In GDScript, variables can be declared using the var keyword.

  • Declare a variable: var my_variable = 10
  • Change the value of a variable: my_variable = 20
  • Declare a variable with a specific type: var my_int: int = 5

Here are some common data types in GDScript:

  • int: Integer numbers
  • float: Floating-point numbers
  • bool: Boolean values (true or false)
  • String: Text strings

Indentation

GDScript uses indentation to define code blocks, just like Python:

func _init():
    pass

Basic Output

To print output in GDScript, use the print() function:

print("Hello, Godot!")

Comments

Single-line comments:

# This is a single-line comment

Multi-line comments

"""
This is a multiline string, not a comment!
And thus it will be parsed by interpreter...
"""

# Now this
# is a multiline comments
# Interpreter will not read this
Comments