Voici une liste des différents codes source et syntaxes courante utilisées en GDScript dans Godot engine.
Ce fichier est un canevas qui s'étoffera au fur et à mesure de mes expérimentations sur ce langage .

Références/Sources:

  • Doc Godot: Gdscript
  • Les exemples de code sont basés sur la version 3.0 de Godot et peuvent être obsolètes

Syntaxe de base

Les variables et les types simples

  • exemple

    # full syntax with new gdscript version (static typed from Godot 3.1)
    var variable_name : type = value
    # type is fixed but determine by the value with new typed gdscript
    var variable_name := value
    # type is dynamic (all gdscript versions)
    var variable_name = value
    # Examples
    var my_var # no value assigned, no type either
    my_var = 5 # assigning value, type is int but can be changed
    my_var : String = "Hello world" # my_var is a string and ONLY
    var empty = null
    var my_float := 5.0 # my_var is a float and ONLY
    var bool_true = true or false
    # constants (use same syntax as variables)
    const MY_CONST = 200
    const MAX_PLAYER_SPEED : int = 500
    const MAX_PLAYER_SPEED_MAX := 1000
    # string concatenation
    var full_name = "First" + " " + "Last"
    var full_nameV2 : String = "First2" + " " + "Last2"
    # Containers (list of values)
    var empty_list : Array = []
    var my_array = [1, "cat", false, 18.56, 'whatever']
    var my_dict = { "one": 1, "two": 2, "three": 3, "four": 4 }
    my_dict = {one = 1, two = 2, three = 3, four = 4 }
    my_dict.four = 4
    my_dict["five"] = 5

Les chaînes formatées

  • exemple

    # Define a format string with placeholder '%s'
    var format_string = "We're waiting for %s."
    # Using the '%' operator, the placeholder is replaced with the desired value
    var actual_string = format_string % "Godot"
    # output: "We're waiting for Godot."
    print(actual_string)
    
    # Multiple placeholders
    var format_string = "%s was reluctant to learn %s, but now he enjoys it."
    var actual_string = format_string % ["Estragon", "GDScript"]
    # output: "Estragon was reluctant to learn GDScript, but now he enjoys it."
    print(actual_string)
    
    # Define a format string
    var format_string = "We're waiting for {str}"
    # Using the 'format' method, replace the 'str' placeholder
    var actual_string = format_string.format({"str":"Godot"})
    # output: "We're waiting for Godot"
    print(actual_string)

Les conditions et les boucles

  • exemple

    if user_answer == "B" and my_var == 1:
    # leave the condition
    pass
    elif user_answer == "C" or my_int = 5:
    do_something()
    else:
    do_something_else()
    
    # tests if a value is in an array
    var my_array = [1, "cat", false, 18.56, 'whatever']
    if my_var in my_array:
    do_something()
    else:
    do_something_else()
    
    # test for an empty array
    if not my_array:
    # my_array is empty
    
    for quote in quotes:
    quote.capitalize()
    
    var y = [1,2,3,4]
    for x in y:
    print(x)
    
    for x in range(8):
    print(x)
    
    while x < 10:
    print(x)
    
    # skip the end of a block (on an if or in a loop)
    continue
    # BREAK the block and go to next line (on an if or in a loop)
    break

Les fonctions

  • exemple

    func get_random_item_in(my_list)->void:
    # TODO: get a random number
    item = my_list[0] # get a quote from a list
    print(item) # show the quote in the interpreter
    return "program is over" # returned value

Les objets

  • création d'une classe: un fichier GDscript est considéré comme une classe

  • Enregistrer un script comme une nouvelle classe (permet de créer de nouveaux types)

    extends Node
    # Declare the class name here, add a comma and an optional path to an image to use as an icon
    class_name ScriptName, "res://path/to/optional/icon.svg"
    func _ready():
    var this = ScriptName           # reference to the script
    var cppNode = MyCppNode.new()   # new instance of a class named MyCppNode
    cppNode.queue_free()
  • exemple de classe dérivée

    extend Sprite
    var max_health := 100
    func take_damage (amount:int)->void
      health -= amount
  • instantiation d'un objet

    var object := Object.new()
  • forcer le type d'un objet (casting), utile pour accèder au méthode propres à classe dérivée

    ($AnimationPlayer as AnimationPlayer).play("walk")

Les noeuds (Node)

  • création d'un node

    var s:Sprite
    func _ready()->void:
    s = Sprite.new() # Create a new sprite!
    add_child(s) # Add it as a child of this node.
  • supression plus sécure d'un node

    func _someaction()->void:
     # delete s (object previous defined)
     s.queue_free()
     # delete current node
     queue_free()
  • suppression immédiate d'un node

    func _someaction()->void:
      # delete s (object previous defined)
      s.queue_free()
      # delete current node
      queue_free()

Les scenes

  • instantiation d'une scene existante

    # loading the scene method 1: Will load when the script is instanced.
    # var scene = load("res://myscene.tscn")
    
    # loading the scene method 2 (préféred):Will load when parsing the script.
    var scene = preload("res://myscene.tscn")
    
    # create the scene instance as a "special node" (PackedScene)
    var node : PackedScene = scene.instance()
    
    # add it to the current scene root
    add_child(node)
  • supression plus sécure d'un node

    func _someaction()->void:
    # delete s (object previous defined)
    s.queue_free()
    # delete current node
    queue_free()
  • suppression immédiate d'un node

    func _someaction()->void:
      # delete s (object previous defined)
      s.queue_free()
      # delete current node
      queue_free()

Écran & Images

L'affichage

  • Connaître la taille de la fenêtre

    position = get_viewport_rect().size.y:
  • Changer la taille de la fenêtre

    TODO
  • passage en Plein écran ou mode fenêtré

    TODO

Le GamePlay

Notions utiles

  • Récupérer une référence vers un noeud

    get_node("first/chid/child")
    # $ is a shortcut for get node
    $first/chid/child
  • supprimer un composant / une entité

    queue_free()
  • Créer et émettre un signal

    # code in the signal emitter script (player)
    signal direction_changed
    func _physics_process(delta:float)->void:
    emit_signal('direction_changed', input_direction)
    #...
  • Recevoir et gérer un signal

    # code in the signal receiver script
    player_node = get_node('player')
    player_node.connect("direction_changed", self, '_on_Player_direction_changed')
    #...
    func _on_Player_direction_changed(direction)->void:
    #...
  • exemple d'implémentation d'un évènement sur un bouton

    extends Panel
    func _on_button_pressed()->void:
    get_node("Label").text = "HELLO!"
    func _ready()->void:
    get_node("Button").connect("pressed", self, "_on_button_pressed")
  • helper pour les collisions

    # first method: delta is already applied and a slide movement is also applied on collision
    velocity = input_direction.normalized() * speed
    move_and_slide(velocity)
    # second method: just a basic collision
    var motion = input_direction.normalized() * speed * delta
    var collider = move_and_collide(motion)

Le Delta time

  • le delta time est directement passé à la fonction _Process

    extends Label
    var accu := 0
    func _process(delta:float)->void:
      accu += 1
    var text := str(accu)

Gestion des entrées utilisateur

Le clavier

  • intercepter une touche du clavier

    input_direction := Vector2()
    input_direction.x = int(Input.is_action_pressed("move_right")) - int(Input.is_action_pressed("move_left"))
    input_direction.y = int(Input.is_action_pressed("move_down")) - int(Input.is_action_pressed("move_up"))
  • Déplacer un sprite au clavier

    extends Sprite
    export var SPEED := 100
    func _process(delta:float):
    var direction := Vector2(0, 0)
    if Input.is_key_pressed(KEY_UP):
        direction.y -= 1
    if Input.is_key_pressed(KEY_DOWN):
        direction.y += 1
    if Input.is_key_pressed(KEY_LEFT):
        direction.x -= 1
    if Input.is_key_pressed(KEY_RIGHT):
        direction.x += 1
    direction = direction.normalized() # To make sure diagonal movements will be in the same speed
    direction *= SPEED * delta         # Multiply by speed (pixels per second) and the time passed (seconds)
    translate(direction)               # Move the Sprite by the direction vector

Le joystick

  • intercepter un bouton du joystick
func _input(event):
  if event is InputEventJoypadButton:
    prints("Button:", str(event.button_index))
  • Déplacer un sprite avec un joystick
extends Sprite
const SPEED: = 300
var device_index: = 0
func joy_connect(index: int, connect) -> void:
  # When a joystick is detected, keep the device index in a variable
  if connect:
    device_index = index
func _ready() -> void:
  Input.connect("joy_connection_changed", self, "joy_connect")
func _process(delta: float) -> void:
  var direction: = Vector2(0, 0)
  # Query Input singleton with the device index
  if Input.is_joy_button_pressed(device_index, 12): # UP
    direction.y -= 1
  if Input.is_joy_button_pressed(device_index, 13): # DOWN
    direction.y += 1
  if Input.is_joy_button_pressed(device_index, 14): # LEFT
    direction.x -= 1
  if Input.is_joy_button_pressed(device_index, 15): # RIGHT
    direction.x += 1
  direction = direction.normalized()
  direction *= SPEED*delta
  translate(direction)

La souris

  • Afficher/Masquer le curseur de la souris

    TODO
  • Connaître l'état des boutons de la souris

    func _input(event):
    if event is InputEventMouseButton && event.pressed:
      prints("Button", event.button_index, "is pressed at", str(event.position))
    if event is InputEventMouseMotion:
       prints("Mouse moved to", str(event.position))
    func _process(delta):
    if Input.is_mouse_button_pressed(BUTTON_LEFT):
      prints("Holding left mouse button at", get_tree().get_root().get_mouse_position())
  • Connaître la position de la souris

    x = get_global_mouse_position().x
    y = get_global_mouse_position().y
  • Déplacer un sprite avec la souris

    func _input(event):
    if event is InputEventMouseMotion:
      position = event.position

Tactile (Touchscreen)

  • Connaître l'état du Touchscreen

    func _input(event):
    if event is InputEventScreenTouch && event.pressed:
        prints("Screen touch at", str(event.position))
    if event is InputEventScreenDrag:
        prints("Screen drag at", str(event.position))
  • Déplacer un sprite avec le Touchscreen

    func _input(event):
    if event is InputEventScreenTouch:
        position = event.position

Physique

Gravité & Inertie

  • déplacement selon un objet ayant un angle de rotation (en degré)

    TODO

L'audio

Musique

  • charger et jouer une musique

    TODO

Sons

  • charger et jouer un son

    TODO

Système

Fichiers

  • lire un fichier texte

    TODO

Divers

Gestion de l'application

  • Quitter l'application

    TODO

Article précédent Article suivant