how to make the timer work again?

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

extends Node2D

var bullet : PackedScene
onready var shotgun = $AnimationPlayer
var timer = null
var bullet_delay = 1
var can_shoot = true

func _ready():
timer = Timer.new()
timer.set_one_shot(true)
timer.set_wait_time(bullet_delay)
timer.connect(“timeout”, self, “_on_timeout_complete”)
add_child(timer)
bullet = ResourceLoader.load(“res://Bullet.tscn”)
pass

func _process(delta):
var dist = global_position.distance_to(Global.player.global_position)
pass

func aiming():

look_at(Global.player.global_position)
if Global.player.global_position <= self.global_position:
	$Shot.flip_v = true
else:
	$Shot.flip_v = false

if can_shoot:
	var bull = bullet.instance()
	bull.dir = rotation
	bull.rotation = rotation
	bull.global_position = global_position
	get_tree().current_scene.add_child(bull)
	can_shoot = false
	timer.start()

func _on_timeout_complete():
can_shoot = true

:bust_in_silhouette: Reply From: Gluon

Its very hard to help you with this as you haven’t told us what you mean by its not working and you havent formatted your code so its very difficult for anyone to read and we cannot see if there are any issues with formatting.

I would suggest likely given the timer is setting can_shoot to true but you are never seemingly calling aiming() I would guess you would want to put that in process

func process(delta):
    aiming()
    var dist = global_position.distance_to(Global.player.global_position)

Thank you. I just can’t to start the timer when I use: timer.start(). But I connected it to method _on_timeout_complete; And I don’t know where is the problem. I can say that when I load my game enemy can aim on player, and when it is happening enemy can shoot only one bullet, and then timer won’t work. I checked video on YouTube about timers, but I can’t start timer how it do others. Sorry for my English.

Spider m | 2022-12-28 22:13

Okay I think I understand in that case use this code

func ready():
     TimerStartFunction()

func TimerStartFunction():
	t = Timer.new()
	t.autostart = true
	t.one_shot = true
	t.wait_time = 12
	t.connect("timeout", self, "on_time_out_complete")
	add_child(t)

func aiming():
    look_at(Global.player.global_position)
    if Global.player.global_position <= self.global_position:
        $Shot.flip_v = true
    else:
        $Shot.flip_v = false
    if can_shoot:
        var bull = bullet.instance()
        bull.dir = rotation
        bull.rotation = rotation
        bull.global_position = global_position
        get_tree().current_scene.add_child(bull)
        can_shoot = false
        TimerStartFunction()

Your problem is you are trying to start a timer which doesnt exist any more as it was a oneshot. This will have a separate timer function and each time you shoot a bullet will set up a new timer to reset your boolean.

Gluon | 2022-12-28 22:19

Wow, thank you very much. I will try your code later. I think that it is working :slight_smile:

Spider m | 2022-12-28 23:09

I checked your variant and I see that TimerStartFunction() won’t start. I don’t understand why, but when enemy shot godot showed me this error: E 0:00:13.281 emit_signal: Error calling method from signal ‘timeout’: ‘Node2D(Shotgun.gd)::on_time_out_complete’: Method not found…

Spider m | 2022-12-29 13:44

Oh well you needed to leave your other functions in place with these functions. The error is telling you that there is no on_time_out_complete which I didnt add above as it was already in your code. below should be all the code including your existing code which the above was to be added too. This made it difficult for me though as you never formatted the code in your original question so I had to retype it all unnecessarily. Please format your code in your questions in the future.

var bullet : PackedScene
onready var shotgun = $AnimationPlayer
var timer = null
var bulletdelay = 1
var can_shoot = true

func ready():
     TimerStartFunction()
     bullet = ResourceLoader.load("res://Bullet.tscn")

func process(delta):
    var dist = global_position.distance_to(Global.player.global_position)

func TimerStartFunction():
    t = Timer.new()
    t.autostart = true
    t.one_shot = true
    t.wait_time = 12
    t.connect("timeout", self, "on_time_out_complete")
    add_child(t)

func aiming():
    look_at(Global.player.global_position)
    if Global.player.global_position <= self.global_position:
        $Shot.flip_v = true
    else:
        $Shot.flip_v = false
    if can_shoot:
        var bull = bullet.instance()
        bull.dir = rotation
        bull.rotation = rotation
        bull.global_position = global_position
        get_tree().current_scene.add_child(bull)
        can_shoot = false
        TimerStartFunction()

func on_time_out_complete():
    can_shoot = true

Gluon | 2022-12-29 13:50

Now it is better, but I have an error: Invalid call. Nonexistent function ‘instance’ in base ‘Nil’. I think that bullet = ResourceLoader.load(“res://Bullet.tscn”) in functuin ready() is a problem.

Spider m | 2022-12-29 14:01

Oh, I’m sorry. I checked code again. It was my mistake. I forgot to connect bullet_delay to my timer. Thank you, you are really good man.

Spider m | 2022-12-29 14:10

No problem I am glad it worked for you, good luck with your game.

Gluon | 2022-12-29 14:10