interraction with objects not working

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

i’m trying to create an interraction system where when you press the enter key a dialog pops up and by pressing the enter key again it goes to another dialog or exits the dialog if it’s finished but this doesn’t seem to work

here’s the code for the object i want to interract with:

extends KinematicBody2D
var is_first_interract:= bool(true)
var only_once:= int(0)
var tim = Timer.new()
signal interract_key_pressed

func _process(delta: float) -> void:
	print(is_first_interract)
	if not is_first_interract:
		if Input.is_action_pressed("interract"):
			emit_signal("interract_key_pressed")

func _on_perso_interract_signal() -> void:
	if is_first_interract:
		if only_once < 1:
			$text.printText($text.text_to_add)
			is_first_interract = false
			only_once += 1
			yield(self, "interract_key_pressed")

func _on_perso_left_perimeter() -> void:
	$"Sprite-0004-Sheet".hide()
	$AnimationPlayer.stop()
	$text.clear()
	only_once = 0


func _on_text_on_finished_showing() -> void:
	$"Sprite-0004-Sheet".show()
	$AnimationPlayer.play("idle")


func _on_interractable_object_interract_key_pressed() -> void:
	$"Sprite-0004-Sheet".hide()
	$AnimationPlayer.stop()
	$text.clear()
	only_once = 0
	is_first_interract = true

and here’s the code for my player character:

extends KinematicBody2D

var speed: = Vector2(300,300)
var direction: = Vector2(0,0)
signal interract_signal
signal left_perimeter


func _physics_process(delta: float) -> void:
	get_raycast_direction()
	direction = get_direction()
	var velocity = speed * direction
	velocity = move_and_slide(velocity)
	var object_colliding = get_node("RayCast2D").get_collider()
	if object_colliding != null && object_colliding.is_in_group("interractable_objects") && Input.is_action_pressed("interract"):
		emit_signal("interract_signal")
	elif object_colliding == null:
		emit_signal("left_perimeter")



func get_direction() -> Vector2:
	return Vector2(Input.get_action_strength("right") - Input.get_action_strength("left"),
					Input.get_action_strength("down") - Input.get_action_strength("up"))


func get_raycast_direction():
	if direction == Vector2(0,1):
		$RayCast2D.rotation_degrees = 0
	elif direction == Vector2(0, -1):
		$RayCast2D.rotation_degrees = 180
	elif direction == Vector2(1,0):
		$RayCast2D.rotation_degrees = 270
	elif direction == Vector2(-1,0):
		$RayCast2D.rotation_degrees = 90

and the code for the richtextabel:

extends RichTextLabel


var text_to_add = "hey there "
var wait_time: float
signal on_finished_showing

func printText(text):

	#create a timer to print text like a typewriter
	var t = Timer.new()
	t.set_wait_time(0.005)
	t.set_one_shot(true)
	self.add_child(t)

	for letter in text:
		t.start()
		self.add_text(letter)
		yield(t, "timeout")
	emit_signal("on_finished_showing")
:bust_in_silhouette: Reply From: DawidM

I think you should use Input.is_action_just_pressed() for events that you want to happen only once after pressing the button.

thanks i’ll try that

ROBOTOO007 | 2020-03-08 21:14

i changedde a bit but that helped THANKS

ROBOTOO007 | 2020-03-09 14:44