+1 vote

I'm completely now to Godot and I try to create a simple project there you have few targets on the window and whenever you click on one, it will dissapear. The placement of each target is randomly chosen.

Here is my code:

extends Node

export (PackedScene) var Aim

var aim_mob

var mob_list

var screenSize

var rng = RandomNumberGenerator.new()

var rndX
var rndY

# Called when the node enters the scene tree for the first time.
func _ready():
    screenSize = get_viewport().get_visible_rect().size


# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
#   pass
func initialize_mobs(number : int):
    for x in range(number):

        rndX = rng.randi_range(0, screenSize.x)
        rndY = rng.randi_range(0, screenSize.y)

        aim_mob = Aim.instance()
        aim_mob.position = Vector2(rndX, rndY)

func _on_Aim_clicked():


extends Area2D

var score = 0

signal clicked

func _ready():

func _input_event(viewport, event, shape_idx):
    if event is InputEventMouseButton and event.button_index == BUTTON_LEFT and event.is_pressed():

func on_click():
    self.visible = !self.visible

So right now when I click on one specific target, one completely different disappear.
My questions are:

  • How I would make program to "know" that one target that I clicked
    should disappear?
  • How can I use object's method on specific instance of this object?
in Engine by (13 points)

Is input_event connected? Also the "Clicked" signal isn't doing anything unless another node is waiting for that signal with a yield.

1 Answer

+2 votes


I spent a bit of time on an example project, that spawns an 1 to 3 objects per second. As I understand, that is what you need. I used the "Button" node instead of an Area2D, because it just looked simplistic.

Here's the example project: https://github.com/TeoTheOO/RanGenBut

Here's the code for the spawning (And score system):

extends Node2D

# Varaiables for spawning
export(Vector2) var button_size = Vector2(76,70) # could be a mob
export(float) var time_btw_spawn = 1.5 # The timer's wait time
onready var screenSize = get_viewport().get_visible_rect().size
onready var button = preload("res://Button.tscn") # The scene, that you want to call
export(Vector2) var  minmax_nodes = Vector2(1,3)
# Varaiables for scoring
var score = 0

func _ready():

func _physics_process(delta):
 $RichTextLabel.set_text("Your score is: " + str(score))

# Spawning every time_btw_spawn seconds
func _on_Timer_timeout():
 var numb_of_nodes = rand_range(minmax_nodes.x, minmax_nodes.y)

for i in numb_of_nodes:
    # get random spawn position
    var spawn_pos = Vector2()
    spawn_pos.x = rand_range(button_size.x, screenSize.x - button_size.x)
    spawn_pos.y = rand_range(button_size.y, screenSize.y - button_size.y)

    var node = button.instance()
    node.position = spawn_pos

And Here's the (really simple) code for the button/enemy:

extends Button

onready var world_node = get_parent().get_parent() 

func _on_Button_pressed():
 world_node.score += 1 # The parent is the "World" node
 # You can write here everything you want
 # To happen, when you click the button
by (137 points)

Thank you, very much. After seeing your example I finally figured out what should I do.

Good to hear! :)

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.