Welcome to our comprehensive guide on the Marker2D class in Godot 4. Marker2D is a versatile tool that every game developer should understand and utilize. It is an engaging, valuable, and accessible class that can enhance your game development experience.
What is Marker2D?
Marker2D is a class in Godot 4 that acts as a generic 2D position hint for editing. Just like a plain Node2D, it displays as a cross in the 2D editor at all times. It’s like having a GPS marker in your game editor, guiding you to the exact location of your game elements.
What is Marker2D used for?
The Marker2D class is primarily used to visually mark positions within the 2D editor. These markers can be used to guide the placement of game objects, similar to how you would mark locations on a map when planning a journey.
Why should you learn Marker2D?
Understanding and using the Marker2D class can greatly improve your efficiency and effectiveness as a game developer. It’s a simple tool, but its usefulness cannot be overstated. It’s akin to knowing the exact locations of all the power-ups in a game level – it gives you a clear advantage.
Developer API
For a more detailed look at the Marker2D class, including its properties and methods, we recommend visiting the latest developer API.
Example Project
You can download the project files here.
Fortunately, using the Marker2D node is fairly straightforward. Let us take a look at a simple example where a Marker2D node could be used to help with object placement at runtime.
Consider this example, say we wanted to have a few spawn points to spawn objects from. These could represent any entity in game such as bullets, arrows, enemies, etc. Let us start with creating a scene called ‘Bullet’ with the root node being a Sprite2D.
We will be using the godot icon for this example, drag a texture from the FileSystem into the texture
property of the Sprite2D node.
In the inspector, set the scale to 0.25 so that the bullet appears smaller.
Lets create a simple script that moves the bullet to the right each frame in local space. Create a script called bullet.gd and attach it to the Sprite2D node.
We do this by:
- Making a
speed
variable to control how fast the movement will be. - Incrementing the current local
position
byVector2(speed, 0)
each frame.- Note that you will have to multiply delta to this calculation to make it framerate independent.
extends Sprite2D var speed = 10 func _process(delta: float) -> void: position = position + Vector2.RIGHT * speed #you can also write this as #position += Vector2.RIGHT * speed
Creating the spawner
Now that we have a bullet, lets create a ‘Spawner’ scene with the root node being a Marker2D
and add a script called spawner.gd
to it to spawn our bullets.
Add a Timer node to it as a child. If you do not know how a timer node works, visit this page where we discuss in detail how the Timer node works. Lets set the timer’s autostart
property to true so that we don’t have to start it manually.
In the spawner.gd
script, let us instantiate the bullet scene every 1 second
- Get a reference to the scene.
- Connect to the Timer’s
timeout
signal. - On timeout,
- Instantiate the scene.
PackedScene.instantiate()
returns aNode
reference, we will need to store it in a variable. - Adding the instance to the scene tree as a child of the Marker2D.
- Instantiate the scene.
extends Node2D @onready var timer: Timer = $Timer var bullet = preload("res://bullet.tscn") func _ready() -> void: timer.timeout.connect(_on_timer_timeout) func _on_timer_timeout(): var bullet_instance = bullet.instantiate() add_child(bullet_instance)
Seeing the spawner in action
Now that we’ve set up all the code required for spawning bullets, lets instantiate the spawner scene into our main scene. Create a new scene called ‘Main’ and with the SceneTree highlighted, click on the “Instantiate Child Scene” button or use the keyboard shortcut Ctrl + Shift + A
and add the spawner scene.
We will now be able to see the advantage of using a Marker2D node for the spawner, select the newly instantiated Spawner scene, in the inspector, set the Gizmo Extents
property to a larger value such as 30. You will now be able to easily visualize the spawn points in the editor! You can even rotate the node to see the gizmo react. This is very helpful since it allows us to visualize the orientation of the node too.
The Marker2D gizmos are only visible in the editor, and are invisible during play. Run the Main scene to see the spawner in action! You will see that the spawner spawns a bullet every one second, and the bullets move forward on their own!
You can set the rotation
of the spawner to have the bullets move in a different direction. This works because we’re incrementing the bullet’s local position.
Why not just use a Node2D? It does the same thing!
Thats true, you could do this with a Node2D and it would act the exact same way, however, the advantage with using a Marker2D is that you can change the gizmo extents to easily visualize the spawn positions. If you zoom out in the editor, it gets difficult to see Node2Ds and differentiate them from other nodes in the scene, but a marker has gizmos that help with visualizing their positions at any range!
Full script code
You can download the Godot project here. The project was developed and tested in version 4.2.
bullet.gd
extends Sprite2D var speed = 10 func _process(delta: float) -> void: position = position + Vector2.RIGHT * speed #//you can also write this as #//position += Vector2.RIGHT * speed
spawner.gd
extends Node2D @onready var timer: Timer = $Timer var bullet = preload("res://bullet.tscn") func _ready() -> void: timer.timeout.connect(_on_timer_timeout) func _on_timer_timeout(): var bullet_instance = bullet.instantiate() add_child(bullet_instance)
Where to Go Next?
Ready to take the next step in your game development journey? We at Zenva offer a range of beginner to professional courses in programming, game development, and AI. With over 250 supported courses available, you can learn coding, create games, and earn certificates at your own pace.
One of our standout offerings is the Godot Game Development Mini-Degree. This self-paced, comprehensive learning pathway is designed for aspiring game developers of all skill levels. With a focus on the free, open-source Godot 4 engine, this program covers a wide range of game development topics. From 2D and 3D game creation to complex gameplay mechanics across various genres, this mini-degree offers a straightforward and engaging learning process.
For a broader collection, check out our Godot courses. Continue your learning journey with us at Zenva – where beginners can become professionals.
Did you come across any errors in this tutorial? Please let us know by completing this form and we’ll look into it!

FINAL DAYS: Unlock coding courses in Unity, Godot, Unreal, Python and more.