0 votes

i want to make a delay time or something like that so i cant spam the bullet
this the code im using for the bullet scene:

extends Area2D

const SPEED = 230
var velocity = Vector2()
var direction = 1

func _ready():
pass

func setswordshootingdirection(dir):
direction = dir
if dir == -1:
$Sprite.flip
h = true

func physicsprocess(delta):
velocity.x = SPEED * delta * direction
translate(velocity)

func onswordshootingbodyentered(body):
queue
free()

func die():
yield(gettree().createtimer(1.0), "timeout")
queue_free()

func onVisibilityNotifier2Dscreenexited():
queue_free()

Godot version latast
in Engine by (12 points)

1 Answer

0 votes

I would put a timer node as a "buffer" between shots.
You could script it like this, in whatever object is firing bullets:

export var shotBuffer = 1 
var canShoot = true
onready var bullet = preload("bulletScenePathHere.tscn")

func input(event):
    if Input.is_action_just_pressed("ui_shoot"):
        if canShoot == true:
           var b = bullet.instance()
           add_child(b)
           canShoot = false
           $Timer.set_wait_time(shotBuffer)
           $Timer.start()
        else:
           pass

func on_Timer_timeout():
     canShoot = true

The timer will reset the Boolean and then allow you to shoot again. Since you export the shotBuffer value, you can adjust it until you get the shooting time necessary between shots.

by (498 points)
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.