Issues with random timer

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

I need a random timer… I have not much clue about gdscript, so I watched a tutorial and this came up… first issue was the connect, I changed it to timeout.connect(init_rand()) … no clue if that works, can’t test it because I’m stuck at issue 2


I don’t know what this is suppose to do but I get an error on this:
.start()
→ Error: Expected statement, found “.” instead.

How do I translate this into Godot4 script.

enter image description here

:bust_in_silhouette: Reply From: TRAILtheGREAT

Replace .start() with super()

Line 10:The function signature doesn’t match the parent. Parent signature is “start(float = ) → void”.

Line 10:The method “start” overrides a method from native class “Timer”. This won’t be called by the engine and may not work as expected. (Warning treated as error.)

Wurzelpilz | 2023-04-27 22:43

Whoops, it looks like your overriding method also has the wrong signature. Your start function should be start(time_sec = -1) and thus the third line should be super(time_sec)

TRAILtheGREAT | 2023-04-27 22:50

This leads to:
The method “start” overrides a method from native class “Timer”. This won’t be called by the engine and may not work as expected. (Warning treated as error.)

Thanks for your time anyway I simply integrated it into my spawn script… that’s simpler I guess.

extends Node2D

@export var enemy : PackedScene
var random = RandomNumberGenerator.new()
var timer = Timer.new()

func _ready():
    random.randomize()
    timer.timeout.connect(_on_timer_timeout)

    add_child(timer) #to process
    timer.set_wait_time(random.randf_range(1, 300))
    timer.start() #to start

func _on_timer_timeout():
    print("spawn")
    var e = enemy.instantiate()
    add_child(e)
    timer.set_wait_time(random.randf_range(1, 5))

Wurzelpilz | 2023-04-28 00:44