Switch signal on and off

Godot Version

4.2.2

Question

Pong
I don’t want the opponent to fidget back and forth, but when he touches the ball, he should go to the middle and wait a moment.
I use ball_y to determine where the ball is with a signal that passes the y value of the ball. Now I want the signal to pause briefly so that the opponent gets to the position he should be, but I don’t know how to do that. I have opponent_position with the Vector2(0, 540). The problem is that the opponent_position is immediately overwritten by the signal y.

Ultimately, I want to try to give the opponent a more human way of moving.

extends CharacterBody2D


var speed = 700
var movement = Vector2(0, speed)
var can_move = false
var ball_y = 0
var max_distance = randf_range(10, 48)
var opponent_position = Vector2(0, 540)


func _physics_process(delta):
	if can_move == true:
		var collision_info = move_and_collide(movement * delta)
		if ball_y > position.y + max_distance:
			await get_tree().create_timer(randf_range(0.02, 0.03)).timeout
			movement = Vector2(0, speed)
			
		if ball_y < position.y - max_distance:
			await get_tree().create_timer(randf_range(0.02, 0.03)).timeout
			movement = Vector2(0, -speed)
		
		if collision_info:
			if collision_info.get_collider().name == "Ball":
				ball_y = opponent_position
				await get_tree().create_timer(1.0).timeout


func _on_ball_position_changed(y):
	ball_y = y


func _on_opponent_timer_timeout():
	max_distance = randf_range(10, 100)
	
 

Thanks for any help

The easiest would probably be to introduce a variable for that. Something like ignore_ball and then your receiver function would be this:

func _on_ball_position_changed(y):
        if not ignore_ball:
                ball_y = y
1 Like

Hmm, somehow I can’t change the y value, no matter what I do, either If Collision_info doesn’t react or the two If statements complain about it. I’ve also tried putting all the If statements into the collision_info but then it goes haywire.