A number is required in operator '%' Can't Find the Error

Godot Version

v4.1.3.stable.official [f06b6836a]

I FOUND THIS CODE IN YOUTUBE I THOUGH THIS WILL FIX MY COOLDOWN
ISSUE

extends TextureButton

@onready var progress_bar = $TextureProgressBar
@onready var timer = $Timer
@onready var time = $time

func _ready():
	progress_bar.max_value = timer.wait_time
	set_process(false)

func clock():
	var time_left = timer.time_left
	var minute = floor(time_left /60)
	var second = int(time_left) % 60
	return ("minute, second")

func _process(_delta):
	time.text = "%02d:%02d" % clock()
	progress_bar.value =  timer.time_left

func _on_pressed():
	timer.start()
	disabled = true
	set_process(true)

func _on_timer_timeout():
	disabled = false
	time.text = ""
	set_process(false)

The clock() function currently returns a string (which always is “minute, second” and not e.g. “15, 35”)

You could do the string interpolation "%02d:%02d" % inside the clock() function to remove the error.

(This is a good time to start using static typing, which will help you track down these kinds of errors easier)

It’s caused by return value of function clock.
It returns a string: minute, second.
But the string %02d:%02d need to be format with number, not the string minute, second

Solutions:

  • Change the return value of clock to [minute, second].
  • Format the string inside clock.

Finally, it works. I remember having an error using parentheses, while the system expected using quotations. I changed it to square brackets, and it works perfectly.

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