0 votes

extends Node2D

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

func ready():
timer = Timer.new()
timer.set
oneshot(true)
timer.set
waittime(bulletdelay)
timer.connect("timeout", self, "ontimeoutcomplete")
add
child(timer)
bullet = ResourceLoader.load("res://Bullet.tscn")
pass

func process(delta):
var dist = global
position.distanceto(Global.player.globalposition)
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 ontimeoutcomplete():
can
shoot = true

Godot version 3.5
in Engine by (12 points)

1 Answer

0 votes

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)
by (3,326 points)

Thank you. I just can't to start the timer when I use: timer.start(). But I connected it to method ontimeout_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.

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.

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

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 emitsignal: Error calling method from signal 'timeout': 'Node2D(Shotgun.gd)::ontimeoutcomplete': Method not found..

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

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.

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.

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.