Making a confirm button when selecting tiles

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Kayou

Hi everyone!
I’m on a mobile game project with my girlfriend and I, and we are stuck on a problem with our game…
Let me explain :

We are making an Idle Tycoon game where the player will have to place buildings.
In order to achieve that, I’ve got the idea of clicking on a button to activate an new popup menu where the player will have the ability to place confirmations tiles (to see where the buildings are gonna be build).
I’ve already done this part (I will give you the code).

The problem is where we want to make a confirmation button (the problem is also the same for the cancel button) :

Once we click on the confirmaton button… A “confirm tile” is summonned beneath the button! So the player is actually building an “extra” building under the button, and no matter what I try (getting the button position and see if the click is inside etc) it’s not working and I still didn’t find any help over the internet.

I’m stuck on this problem since 3 days and I would really appreciate some hints so I can continue programming it ! :slight_smile:

extends TileMap
var terre = false
var charbon = false
var nbrplaced = 0
var popup1
var confirm_button = false
onready var buttonpos = 
signal coal1()
func _ready():
    popup1 = get_parent().get_node("Control/Canvas/Buildmenu")
    var tilemap_rect = get_parent().get_node("TileMap").get_used_rect()
    var tilemap_cell_size = get_parent().get_node("TileMap").cell_size
    get_parent().get_node("Camera2D").limit_left = tilemap_rect.position.x * 
    get_parent().get_node("Camera2D").limit_right = tilemap_rect.end.x * 
    get_parent().get_node("Camera2D").limit_top = tilemap_rect.position.y * 
    get_parent().get_node("Camera2D").limit_bottom = tilemap_rect.end.y * 

func _process(delta):
    if get_global_mouse_position().distance_to(buttonpos.get_position()) > 30:
	    if Input.is_mouse_button_pressed(BUTTON_LEFT) and terre == true: # Test avec terre BETA
		    var mousePos = get_global_mouse_position()
		    var loc = world_to_map(mousePos)
		    var cell = get_cell(loc.x,loc.y)
		    if(cell == 15):
			    set_cell(loc.x,loc.y, 13)

	    if Input.is_mouse_button_pressed(BUTTON_LEFT) and charbon == true: # Test avec charbon
		    var mousePos = get_global_mouse_position()
		    var loc = world_to_map(mousePos)
		    var cell = get_cell(loc.x,loc.y)
		    if(cell == 13):
			    set_cell(loc.x,loc.y, 0)
			    nbrplaced = nbrplaced + 1

Also I know why it’s actually not working, it’s because the button position is “static” and is not following the camera, i’m using a CanvasLayer.

Also I’m gonna give you some screenshots of my scene tree so you could probably find where the problem is :

scene tree
Thank you for helping me :slight_smile:

:bust_in_silhouette: Reply From: exuin

I think you should look into the _unhandled_input() function and how inputs are handled in Godot. You want to consume the input when the player presses the button so they don’t place a building down as well.

God, I didn’t know how this was working, so I checked a lot of youtube videos and I successfully managed to understand how it was working and how to apply this, thanks a lot !

So for other people who are reading this, here are the explanations.

First, when you click on the screen, godot is gonna check if the input wasn’t already set inside your scripts. If not, then it’s gonna check for the gui_input function (for exemple, i’m actually using it on a control node where I set this for a test) :

func _gui_input(event):
if event is InputEventScreenTouch:
	if $Canvas/ConfirmMenu/Confirmbutton.pressed:
		get_parent().get_node("TileMap").confirm_button = true

So when I click on the button, Godot is gonna stop transmitting the input to the other Nodes.
BUT When I actually click on the screen, (without clicking on the GUI), then godot will look to the unhandled_input function.
So from there, you can set all the unhandled_input, check if it’s a touchscreenevent. If Yes, then make the process.

It was as simple as that! Thank’s a lot, it’s always a pleasure to learn this wonderful Game Engine! :slight_smile:

Kayou | 2021-03-23 16:58