Button is not triggering the desired funtion

Godot Version

Godot v4.3

Question

I’m currently tinkering with a procedurally generated isometric terrain. I want the button to generate new terrain when pressed. However, nothing is happening. This is the code what I have so far:

extends Node2D

@export var noise_height_text : NoiseTexture2D
var noise = FastNoiseLite.new()

var width : int = 50
var height : int = 100

@onready var tile_map = $TileMap
@onready var Generate_World_Button = $Control/VBoxContainer/Generate_World

#Stores the terrain tiles into variables
var source_id = 0
var water_atlas = Vector2i(0, 11)
var land_atlas = Vector2i(1, 11)
var sand_atlas = Vector2i(3, 11)

# Called when the node enters the scene tree for the first time.
func _ready():
	noise_height_text.noise
	generate_world()

func generate_world():
	for x in range(width):
		for y in range(height):
			#Gets the noise value to determine that tile to place
			var noise_val : float = noise.get_noise_2d(x, y)
			if noise_val >= 0.0:
				#Determines the noise values to place the land tile
				tile_map.set_cell(0, Vector2(x,y), source_id, land_atlas)
			elif noise_val > -0.1 and noise_val < 0.0:
				#Determines the noise values to place the sand tile
				tile_map.set_cell(0, Vector2(x,y), source_id, sand_atlas)
			elif noise_val <= -0.1:
				#Determines the noise values to place the water tile
				tile_map.set_cell(0, Vector2(x,y), source_id, water_atlas)

func _on_generate_world_pressed():
	generate_world()

And this is how I have the nodes organized:
image

Did you connect the button’s signal with the function? Alternatively you can also do that via code.

Look carefully - this is not correct path to your button…“Camera2D” and “MarginContainer” are missing in the path. And if you decide to move the generation button, everything will break again. It is best to right-click on the NODE and make it a Unique Name.
That is:
@onready var Generate_World_Button = %Generate_World
Then you code correct path - everything must work as expected !

I don’t know how to do it via code, but the signal should be connected to the right node:
image

You know what I just noticed? I’m not even using that variable. I tried your fix but it didn’t work, and that is when I realized I wasn’t even using the variable. I made that line of code into a comment for now but unfortunately, I am still stuck with my issue.

Print something from the _on_generate_world_pressed function, to verify that it’s getting called?

Apart from printing messages you can also use breakpoints

Yes it is. Maybe your button is working, but generates the SAME landscape, cos i’m not seeing any random func in your func generate_world(). Its must be noise_val, but something go wrong.

1 Like

I found out what the problem was. The _ready() function wasn’t generating new seeds. So I replace “noise_height_text.noise” with the following:

noise.seed = randi()

It worked!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.