Tower Placing in a Tower Defense Game

Godot Version

Godot 4.2.2

Question

I can’t get the UI to work when I want to place a tower in my game, I wanted it to be able to fill in where my mouse is with a tower to show exactly where the tower is and I am having trouble with this specific line of code, I was following a tutorial but it was from an old version of Godot, and the code that they put in doesn’t work. Here it is:

extends Node2D

var map_node

var build_mode = false
var build_valid = false
var build_location
var build_type

func initiate_build_mode(tower_type):
build_type = tower_type + “tier_1”
build_mode = true
get_node(“UI”).set_tower_preview(build_type, get_global_mouse_position())
func _ready():
map_node = get_node(“Map1”) ## Turn this into variable based on selected map

for i in get_tree().get_nodes_in_group("build_buttons"):
	i.connect("pressed", self, initiate_build_mode, [i.get_name()])

func _unhandled_input(event):
pass

func update_tower_preview():
pass

func cancel_build_mode():
pass

func verify_and_build():
pass

In Godot 4 they updated the way you connect signals so instead of
i.connect("pressed", self, initiate_build_mode, [i.get_name()])
it’ll be
i.pressed.connect(initiate_build_mode.bind(i.get_name()))

I think everything else is the same but let me know if that works.

It solved that problem but now it seems that there is an issue projecting the tower on the mouse, as soon as I click the tower button it breaks the game and it doesn’t respond. This should be the section that is responsible for projecting the image but it might be wrong.

func initiate_build_mode(tower_type):
build_type = tower_type + “_tier_1”
build_mode = true
get_node(“UI”).set_tower_preview(build_type, get_global_mouse_position())

Did you get an error message or did it just freeze? Not too sure what the issue is