Cant figure out the problem with line 15

Godot Version

4.2.2

Question

Can someone help me fix the problem with line 15 in my code?

extends Node2D

var move_speed = 100  # Adjust the speed as needed
var direction = Vector2(1, 1)  # Initial direction
var is_moving = true
var break_timer = Timer.new()

func _ready():
    randomize()  # Initialize random number generator
    add_child(break_timer)
    break_timer.wait_time = randf_range(1, 3)  # Random interval between 1 and 3 seconds for direction change
    break_timer.one_shot = false
    
    # Connect the timeout signal to _on_break_timer_timeout method
    break_timer.connect("timeout", self, "_on_break_timer_timeout")

    # Start the timer
    break_timer.start()

func _process(delta):
    if is_moving:
        var window_pos = Vector2(DisplayServer.window_get_position().x, DisplayServer.window_get_position().y)
        var window_size = Vector2(DisplayServer.window_get_size().x, DisplayServer.window_get_size().y)
        var screen_size = Vector2(DisplayServer.screen_get_size().x, DisplayServer.screen_get_size().y)

        # Update the window position
        window_pos += direction * move_speed * delta

        # Check for window bounds and bounce if necessary
        if window_pos.x < 0 or window_pos.x + window_size.x > screen_size.x:
            direction.x = -direction.x
        if window_pos.y < 0 or window_pos.y + window_size.y > screen_size.y:
            direction.y = -direction.y

        # Apply the new window position
        DisplayServer.window_set_position(Vector2i(int(window_pos.x), int(window_pos.y)))

func _on_break_timer_timeout():
    if is_moving:
        # Randomly decide to change direction or take a break
        if randi() % 2 == 0:
            direction = Vector2(randf_range(-1, 1), randf_range(-1, 1)).normalized()
        else:
            is_moving = false
            break_timer.wait_time = randf_range(5, 8)  # Random break duration between 5 and 8 seconds
    else:
        # End the break
        is_moving = true
        break_timer.wait_time = randf_range(1, 3)  # Random interval between 1 and 3 seconds for direction change

It caused 3 different problems

Line 15:Invalid argument for "connect()" function: argument 2 should be "Callable" but is "res://node_2d.gd".
Line 15:Cannot pass a value of type "String" as "int".
Line 15:Invalid argument for "connect()" function: argument 3 should be "int" but is "String".

Someone please help

Godot 4 changes the API.

It should be this instead

break_timer.connect("timeout", _on_break_timer_timeout)

2 Likes

Thanks!