Add 2 numbers Continuously on Item Pickup

Godot Version

v4.2.2

Question

I am wanting to create a system where a small number will add to a value when an item is picked up.

For example, our ninja character can pick up items dropped by enemies. One of those items is a shuriken boost that doubles the rate of fire for 5 seconds on a cool down timer. Instead, I would like for when our ninja hero picks up the item, it adds a small rate of fire, and that small rate continues to grow as the shuriken items are picked up. The same would go for the speed boost item as well.

Here is the current code. It works fine, but the fire rate and speed boost will only fire within the time of the timers:

Constans to set the values on the Player

const START_SPEED : int = 200
const BOOST_SPEED : int = 400
const NORM_SHOT : float = 0.5
const FAST_SHOT : float = 0.1

and then the functions to kick them off

func boost():
	#$boostTimer.start()
	speed = BOOST_SPEED
	$puSound.play()
	
func quick_fire():
	#$fastFireTimer.start()
	$shotTimer.wait_time = FAST_SHOT
		
		
func _on_shot_timer_timeout() -> void:
	can_shoot = true


func _on_boost_timer_timeout() -> void:
	speed = START_SPEED


func _on_fast_fire_timer_timeout() -> void:
	$shotTimer.wait_time = NORM_SHOT

Finally, the on_entered_body from the items:

func _on_body_entered(body):
	#noodle
	if item_type == 0:
		$bonusSound.play()
		body.boost()
	#heart
	elif item_type == 1:
		main.lives += 1
		life_label.text = ("X ") + str(main.lives)
	#star
	elif item_type == 2:
		body.quick_fire()
	#delete item
	queue_free()

I am fairly new to Godot and expanding on a tutorial, so any help is appreciated!

I’ve found that setting a variable to true in the on_body_entered and then setting it to false when exiting is much more reliable.

func _physics_process(delta):
	if noodle_eat == true:
		body.quick_fire()

func _on_body_entered(body):
	#NOODUL TIME
	noddle_eat = true

func _on_body_exited(body):
	#NO MORE NOODUL
	noddle_eat = false

You can add to the Timer’s time_left via the start function. It’s hard to tell exactly how everything is set up but I think you want this, it will add 5 seconds to the fast fire timer for every pickup.

func quick_fire():
	$shotTimer.wait_time = FAST_SHOT
	$fastFireTimer.start($fastFireTimer.time_left + 5.0)

Thanks a lot! Because of implementation, this didn’t really work but led me down the right path for sure. What I wound up landing on was:

const NORM_SHOT : float = 0.5
var FAST_SHOT : float = 0.48

func quick_fire():
	#$fastFireTimer.start() #fastFireTimer is the length the fast fire runs
	$shotTimer.wait_time = FAST_SHOT
	FAST_SHOT = (FAST_SHOT - 0.02)
	print($shotTimer.wait_time)

I changed the FAST_SHOT constant to a variable and implementing the above, now every time I pick up the shuriken bonus, the ninja hero fires 0.02% times faster. EXACTLY what I wanted to do.

Thanks again!

1 Like