I was hoping I could get some guidance with an issue I am having with Godot. I am about 1/2 through the Alien Attack tutorial for the GoDot course with professor Kaan. The course has done a great job at teaching me all about the use of signals in GoDot, but I am having one minor issue.
I have been tinkering with the projects outside of the videos to learn more/have fun and I am attempting to create a timer that will prevent my gun from being able to fire constantly (or in this case the rocket from the ship).
What I have tried.
I created a timer variable that was attached to my player character. My player character is a CharacterBody2D node, consisting of a sprite, collisionshape2d, area2d (with a collisionshape2d child), node2d (rocket container) and a timer.
The timer has access as a unique name turned on. I created an onready variable called shootTimer and accessed it with $Timer
so it looks like this currently:
#Node variables to instantiate (@onready)
@onready var rocket_scene = preload("res://scenes/rocket.tscn")
@onready var rocketContainer = $RocketContainer
@onready var shootTimer = $Timer
#timer for shooting
func _ready():
shootTimer.timeout.connect(shoot)
#handle inputs from player
func _process(delta):
#shooting
if Input.is_action_just_pressed("shoot"):
shoot()
#movement
func _physics_process(delta):
velocity = Vector2(0,0)
if Input.is_action_pressed("move_right"):
velocity.x = speed
if Input.is_action_pressed("move_left"):
velocity.x = -speed
if Input.is_action_pressed("move_up"):
velocity.y = -speed
if Input.is_action_pressed("move_down"):
velocity.y = speed
move_and_slide()
#limit player movement to the viewport
var rect = get_viewport_rect().size
global_position = global_position.clamp(Vector2(0,0), rect)
#instance rocket and add a spawning position
func shoot():
var rocket_instance = rocket_scene.instantiate()
rocketContainer.add_child(rocket_instance)
rocket_instance.global_position = global_position
rocket_instance.global_position.x += 75
So in my mind this code checks out. There are no errors thrown, and I have autostart selected for my timer. So it should be allowing me to fire every three seconds, but nothing has changed and my player can still spam the fire button and get a wall of rockets.
I was hoping that this would be easy to address and I’m missing something minor. Please, if I am far off base just let me know and I will stick with the videos, but I do feel that I’m close to the solution.
– minor edit to note that I have watched the video on connecting the spawner signals, which is what gave me the idea that I could easily convert this timer node into one for my firing.
For example, if I want say a pump shotgun there would be more of a delay in between shots than an smg, but no matter what I don’t want the bullets to stack on top of one another, so this is the issue I’m currently facing.