Questuion about methods of specific instane

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

Hello
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:
Main:

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
		
	initialize_mobs(5)
	
# 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):
		rng.randomize()
	
		rndX = rng.randi_range(0, screenSize.x)
		rndY = rng.randi_range(0, screenSize.y)
	
		aim_mob = Aim.instance()
		add_child(aim_mob)
		aim_mob.position = Vector2(rndX, rndY)
	

func _on_Aim_clicked():
	aim_mob.on_click()

Aim:

extends Area2D

var score = 0

signal clicked

func _ready():
	pass
	

func _input_event(viewport, event, shape_idx):
	if event is InputEventMouseButton and event.button_index == BUTTON_LEFT and event.is_pressed():
		emit_signal("clicked")
		
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?

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

Magso | 2020-07-01 12:53

:bust_in_silhouette: Reply From: Czselu349

Hello!

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():
 $Timer.set_wait_time(time_btw_spawn)
 $Timer.start()

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

# Spawning every time_btw_spawn seconds
func _on_Timer_timeout():
 randomize()
 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()
	self.add_child(node)
	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
 queue_free()

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

lava_goose | 2020-07-01 15:50

Good to hear! :slight_smile:

Czselu349 | 2020-07-02 08:51