GD Script Overview

Jul 28, 2024

GD Script Overview

Introduction

  • GD Script is a high-level, object-oriented, imperative programming language with Python-like syntax.
  • Designed specifically for the Godot Engine (GDAU).
  • In this video, we'll learn about key concepts, from variables to signals, tailored for beginners.
  • Focus areas include:
    • Variables
    • Conditionals
    • Inheritance
    • Dictionaries
    • Signals

Video Structure

  • The video is an overview, not an exhaustive tutorial.
  • Beginners are encouraged to pause and experiment as they go along.
  • Each section is clearly named for future reference.
  • Seasoned programmers will also find this overview useful while transitioning to GD Script.

Getting Started with GD Script

  • An introductory suggestion is to begin with a GDAU tutorial for complete beginners.
  • The video is sponsored by Code Crafters for software skill enhancements.

Hello World Example

  1. Creating a Scene:
    • Create a new node (Main), and save as a scene.
  2. Adding a Script:
    • Script name should be main.gd.
  3. Defining Functions:
    • ready(): Executed when the node enters the scene tree.
    • Replace pass with print("Hello World").
  4. Running the Game:
    • Run the scene and observe messages in the console.

Key GD Script Syntax

  • Statements end in new lines. No semicolons needed.
  • Indentation is crucial for defining code structure (similar to Python).
  • Case-Sensitivity: e.g., Print is different from print.

Displaying Text in Game

  • Create a Label Node to display text.
  • Accessing Node Properties:
    • Drag and drop the label into the script to create a reference.
    • Set label.text = "Hello World" to show it in-game.

Handling Input

  • Configure Input Actions:
    • Add an action in Project Settings (e.g., Space bar).
  • Create an input(event) function to handle actions.
    • Change colors based on pressed actions (e.g., turn label red).
    • Use if event.is_action_pressed("my_action") to check the input.

Variables

  • Variables in GD Script are simple containers for data.
  • Declare using var variable_name = value.
  • Example: var health = 100.

Conditionals

  • Use if statements to control logic flow.
  • Example:
    if health <= 0:  
        print("You died.")  
    else:  
        print("You are healthy.")  
    

Comments

  • Use # to write comments in code.
  • Example: # This is a comment

Functions

  • Functions are reusable blocks of code defined with func function_name():.
  • Can have input parameters and return values.

Classes and Instances

  • GD Script uses object-oriented principles.
  • Classes act as blueprints for objects in your game.
  • Example:
    class_name Character  
    

Signals

  • Signals are used to notify other nodes when something happens.
  • Connect signals in the Godot editor for decoupled architecture.

Conclusion

  • This overview should help you understand the basics of GD Script.
  • Further resources can be found in the official Godot documentation.