How to make a building system

Godot Version

4.3

Question

I'm making a 2d tower defense, and i can't find a tutorial how to make a building system. Can someone send tutorials how to make it, please?

There are not really much tutorials over a 2D building system, but I can walk you through it.

You can make a new scene for the tower, and when you click somewhere you can instantiate that scene.

Example:

extends Node2D

var towerScene = "res://tower.tscn" # Replace this with your actual tower scene
var mouseButtonPressed = false # Makes sure towers aren't placed every frame the mouse is held.

func _input(ev):
    if Input.is_mouse_button_pressed():
        if !mouseButtonPressed:
            towerScene.instantiate()
            # Set your tower variable here
            # ex. towerScene.type = "Archer"
            get_tree().get_current_scene().add_child(towerScene)
            mouseButtonPressed = true
    if !Input.is_mouse_button_pressed():
        mouseButtonPressed = false
2 Likes

thank you