Creating your first script
in the introduction, we assume you have programming foundations. The equivalent C# code has been included in another tab for convenience. See also GDScript reference. See also C# basics page.
Project setup
Please create a new project to start with a clean slate. Your project should contain one picture: the Godot icon, which we often use for prototyping in the community.
We need to create a Sprite node to display it in the game. In the Scene dock, click the Other Node button.
Type “Sprite” in the search bar to filter nodes and double-click on Sprite to create the node.
Your Scene tab should now only have a Sprite node.
icon.png from the FileSystem dock onto the Texture slot.
Note
You can create Sprite nodes automatically by dragging and dropping images on the viewport.
Then, click and drag the icon in the viewport to center it in the game view.
Creating a new script
To create and attach a new script to our node, right-click on Sprite in the scene dock and select “Attach Script”.
The Attach Node Script window appears. It allows you to select the script’s language and file path, among other options.
Change the Template from Default to Empty to start with a clean file. Leave the other options by default and click the Create button to create the script.
Sprite.gd file open and the following line of code:
GDScript C#
extends Sprite
public class Sprite : Godot.Sprite// Declare member variables here. Examples:// private int a = 2;// private string b = "text";// Called when the node enters the scene tree for the first time.public override void _Ready(){}// // Called every frame. 'delta' is the elapsed time since the previous frame.// public override void _Process(float delta)// {//// }
extends keyword defines the class this script inherits or extends. In this case, it’s Sprite, meaning our script will get access to all the properties and functions of the Sprite node, including classes it extends, like Node2D, CanvasItem, and Node.
Note
extends keyword, your class will implicitly extend Reference, which Godot uses to manage your application’s memory.
texture.
Note
By default, the Inspector displays a node’s properties in “Title Case”, with capitalized words separated by a space. In GDScript code, these properties are in “snake_case”, which is lowercase with words separated by an underscore.
You can hover any property’s name in the Inspector to see a description and its identifier in code.
Hello, world!
Our script currently doesn’t do anything. Let’s make it print the text “Hello, world!” to the Output bottom panel to get started. Add the following code to your script: GDScript C#
func _init(): print("Hello, world!")
public Sprite(){ GD.Print("Hello, world!");}
func keyword defines a new function named _init. This is a special name for our class’s constructor. The engine calls _init() on every object or node upon creating it in memory, if you define this function.
Note
print() is necessary for the code to work. If you omit it or don’t indent a line correctly, the editor will highlight it in red and display the following error message: “Indented block expected”.
Output bottom panel that expands. It should display “Hello, world!”.
_init() function, so you’re only left with the line extends Sprite.
Turning around
It’s time to make our node move and rotate. To do so, we’re going to add two member variables to our script: the movement speed in pixels per second and the angular speed in radians per second. GDScript C#
var speed = 400var angular_speed = PI
private int Speed = 400;private float AngularSpeed = Mathf.Pi;
speed and angular_speed properties.
Note
Angles in Godot work in radians by default, but you have built-in functions and properties available if you prefer to calculate angles in degrees instead.
_process() virtual function of the Node class. If you define it in any class that extends the Node class, like Sprite, Godot will call the function every frame and pass it an argument named delta, the time elapsed since the last frame.
Note
Games work by rendering many images per second, each called a frame, and they do so in a loop. We measure the rate at which a game produces images in Frames Per Second (FPS). Most games aim for 60 FPS, although you might find figures like 30 FPS on slower mobile devices or 90 to 240 for virtual reality games.
The engine and game developers do their best to update the game world and render images at a constant time interval, but there are always small variations in frame render times. That’s why the engine provides us with this delta time value, making our motion independent of our framerate.
At the bottom of the script, define the function:
GDScript C#
func _process(delta): rotation += angular_speed * delta
public override void _Process(float delta){ Rotation += AngularSpeed * delta;}
func keyword defines a new function. After it, we have to write the function’s name and arguments it takes in parentheses. A colon ends the definition, and the indented blocks that follow are the function’s content or instructions.
Note
_process(), like _init(), starts with a leading underscore. By convention, Godot’s virtual functions, that is to say, built-in functions you can override to communicate with the engine, start with an underscore.
rotation += angular_speed * delta, increments our sprite’s rotation every frame. Here, rotation is a property inherited from the class Node2D, which Sprite extends. It controls the rotation of our node and works with radians.
Tip
position, rotation, or _process to open the corresponding documentation in a new tab.
Run the scene to see the Godot icon turn in-place.
Moving forward
_process() function, ensuring the new lines are indented the same way as the one before them.
GDScript C#
var velocity = Vector2.UP.rotated(rotation) * speedposition += velocity * delta
var velocity = Vector2.Up.Rotated(Rotation) * Speed;Position += velocity * delta;
var keyword defines a new variable. If you put it at the top of the script, it defines a property of the class. Inside a function, it defines a local variable: it only exists within the function’s scope.
velocity, a 2D vector representing both a direction and a speed. To make the node move forward, we start from the Vector2 class’s constant Vector2.UP, a vector pointing up, and rotate it by calling the Vector2.rotated() method. This expression, Vector2.UP.rotated(rotation), is a vector pointing forward relative to our icon. Multiplied by our speed property, it gives us a velocity we can use to move the node forward.
velocity * delta to the node’s position to move it. The position itself is of type Vector2, a built-in type in Godot representing a 2D vector.
Run the scene to see the Godot head run in circles.
Note
Your first 2D game, you will learn another approach to moving objects while detecting collisions.
Listening to player input, we’ll use player input to control it.
Complete script
Sprite.gd file for reference.
GDScript C#
extends Spritevar speed = 400var angular_speed = PIfunc _process(delta): rotation += angular_speed * delta var velocity = Vector2.UP.rotated(rotation) * speed position += velocity * delta
using Godot;public class Sprite : Godot.Sprite{ private int Speed = 400; private float AngularSpeed = Mathf.Pi; public override void _Process(float delta) { Rotation += AngularSpeed * delta; var velocity = Vector2.Up.Rotated(Rotation) * Speed; Position += velocity * delta; }}
