GD Script: Getting Started
Introduction
- GD Script is an object-oriented, imperative, and gradually typed programming language, similar to Python.
- Specifically designed for Godot.
- This tutorial covers concepts from variables and conditionals to inheritance and signals.
- The video is divided into clearly named sections that serve as a reference manual.
Creating a Test Scene
- Creating a node and renaming it to "Main".
- Save as scene and add a script.
- The
ready() function is used as the game start point.
- Use
print() to display messages in the console.
- Indentation and case sensitivity are important.
Modifying Nodes Through Script
- Add a "Label" node and modify its properties like
text and modulate.
- Drag and drop nodes into the
ready() function to create references.
- Change colors using
modulate.
Handling Input
- Create input actions in the project settings.
input(event) function to handle inputs.
- Use
event.is_action_pressed() and event.is_action_released() to detect keys pressed and released.
Variables and Operations
- Define variables with
var and initialize values.
- Arithmetic operations:
+=, -=, *=, /=, etc.
- Use
if and else control structures to handle game logic.
Code Comments
- Use
# to add comments and explain parts of the code.
- Can also be used to temporarily disable parts of the code.
Advanced Variables
- Scope of variables: global vs. local.
- Data types:
bool, int, float, string, and type casting.
- Optional static typing for stronger type safety.
- Use
export to expose variables in the inspector.
- Constants: Use
const to define immutable values.
Functions
- Definition: Use
func to define reusable functions.
- Parameters and Returns: Functions can take arguments and return values using
return.
- Static typing is also applicable to parameters and return values.
Random Numbers
- Functions like
randf() and rand_range() to get random numbers.
- Use for probabilities and assigning different random values.
Arrays and Loops
- Arrays: Creating and managing lists of elements with indices.
- For and While Loops: Code repetition using
for and while.
- Use
break and continue in loops for flow control.
Dictionaries
- Concept of key-value pairs for storing relational data.
- Accessing and modifying values using keys.
- Nesting dictionaries and arrays within dictionaries.
Enums and Match Statements
- Use enums to define states or labels in the game.
- Match Statements to execute different code blocks based on enum values.
Advanced Node Modification
onready to ensure a node exists before referencing it.
- Use
export to reference nodes from the inspector.
- Type checking using
is keyword.
Signals
- Signals: Messages that nodes can send and listen to for events.
- Connecting and disconnecting signals from the interface and through code.
- Passing parameters through signals.
Getters and Setters
- Getters and Setters: Control how a variable is accessed and modified.
- Clamping values and emitting signals in setters.
Classes and Instances
- Classes: Templates for creating objects with common properties and methods.
- Instances: Specific versions of a class with unique values.
- Use
extends to inherit functionalities from other classes.
Inner Classes and Composition
- Inner classes to group related variables and functions within another class.
- Composition: Sometimes preferred over inheritance for code structure.
Best Practices
- Call Down, Signal Up: Recommended practice for communication between nodes.
- Code Style: Follow style guides to keep code readable and maintainable.
This is a comprehensive summary of the GD Script tutorial covered in the video. Be sure to review each section and practice the examples for a deeper understanding.