Taking a practice project and making it my own, struggling with figuring out how to adjust my timer

Godot Version

Godot 4

Question

Hi, I followed a project on Youtube to make a basic shooter, but I wanna take it a step further, but my command over coding leaves… much to be desired, being frank. I want to programmatically decrease the interval between spawns to give scaling difficulty to the existing game, and spawning in this project was handled via what I assume is a standard path2d randomized spawning set of nodes that determine position, and a timer that determines the spawn interval.

I’ve tried to programmatically adjust this using what I learned in the video, but can’t seem to get the game to understand I’m trying to hook onto the timer and change the wait_time value. In said video, we use the % and access as unique name function to do a fair few things and that’s what I went with, but it seems to no matter what spit out a “identifier “timer” not declared in the current scope” as an error.

$timer.wait_time -= (value) was the best I could figure out, but frankly, I’m rather inexperienced when it comes to coding - I understand that it’s all logic, but I always seem to struggle with conveying my logic. Any advice for improving at that aspect?

% is for unique names, it’s somewhat rigid in it’s use. If $timer works you should use it. Unique names are more useful if you end up with many children; say $HBoxContainer/VBoxContainer/MarginContainer/ScrollContainer/Timer is a mouthful, so a unique name in this case would be useful, but $timer vs %timer is minutely different so using the $timer notation should be used.

Fair, but even with that, neither of these lines of code run properly, so I don’t know what I’m suppose to do.

Can you show use your scene tree and maybe some more code?

For this specifically, I created the signal LowerSpawnTime in the mob node, and piggybacked off of the existing death program for the mobs to tell the game to emit the signal, then went to the game’s script and added the signal, and applied what I thought would be the code used above.

from mob:
extends CharacterBody2D

signal LowerSpawnTime

var health = 2

@onready var player = get_node(“/root/game/HappyBoo”)

func _ready():
%Slime.play_walk()

func _physics_process(delta):
var direction = global_position.direction_to(player.global_position)
velocity = direction * 300
move_and_slide()

func take_damage():
health -= 1
%Slime.play_hurt()

if health == 0:
	queue_free()
	const SMOKE_SCENE = preload("res://smoke_explosion/smoke_explosion.tscn")
	var smoke = SMOKE_SCENE.instantiate()
	get_parent().add_child(smoke)
	smoke.global_position = global_position
	LowerSpawnTime.emit()

From main_scene.gd
func _on_mob_lower_spawn_time():
$timer.wait_time() -= 0.5

if it’s of any help, I’m using the GDQuest shooter project as my basis here, and aside from variable and file names being different, it’s more or less as shown there if further holes need to be filled.

I had intended for as said above ramping difficulty via decreasing spawn time, to me this code makes sense but it’s clearly not doing anything so i must be misunderstanding something.

it could be that in the code, it should be $“…/SpawnTimer” instead of $timer, or use %SpawnTimer if that could work, though I wouldn’t bet on it.

I tried those as well, nothing.

you can set it as an onready variable by dragging it in and hitting ctrl before letting go of the mouse, then replace $timer with the variable name. also, i don’t think
wait_time is a function
I also just realized that i put … instead of.. in

$“../SpawnTimer”

I’ll try that out when i next sit down to work, but quick screenshot to show what I’m trying to pull here

Yeah, this didn’t seem to work either, any other suggestions?

Should be without the brackets (and with the correct node path, which should be %SpawnTimer according to the node tree you showed us):

%SpawnTimer.wait_time -= 0.5

Can you try that out, and if it doesn’t work, post the error message you get?

Actually, upon further testing, this Does work, but for some reason the function doesn’t increment for the first 8 or so enemies? Whatever, it does what I wanted lol

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.