how can i decrease all value to zero before timer can stop, and check if all has become zero?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By potatobanana

what i want to do is, when all value in GlobalDta.skill_list["test_cd"][x].c_cd become 0 timer will stop, then i can check if all GlobalDta.skill_list["test_cd"][x].c_cd <=0 then i get true.

but after i get this result it stop decreasing.

one more thing,why when i make GlobalDta.skill_list["test_cd"][x].c_cd to var cd_list = GlobalDta.skill_list["test_cd"][x].c_cd then continue like this cd_list -=1 it wont work. it just decrease once then stop decreasing.

how should i make work? and why it happen?

func _on_Timer_timeout() -> void:
	for x in GlobalDta.skill_list["test_cd"].keys():
		if GlobalDta.skill_list["test_cd"][x].c_cd >0:
			GlobalDta.skill_list["test_cd"][x].c_cd -=1
			print(GlobalDta.skill_list["test_cd"][x].c_cd)
		else:
			$Timer.stop()
			print("done")

result

0
0
4
2
done
done
3
1

json dict

{
  "test_cd": {
    "skill1": {
      "name": "fire",
      "c_cd": 1,
      "max_cd": 3.5
    },
    "skill2": {
      "name": "water",
      "c_cd": 1,
      "max_cd": 10
    },
    "skill3": {
      "name": "wind",
      "c_cd": 5,
      "max_cd": 5
    },
    "skill4": {
      "name": "earth",
      "c_cd": 3,
      "max_cd": 6.5
    }
  }
}
:bust_in_silhouette: Reply From: MrEliptik

You don’t get the correct result because you’re running your code on the timeout of the timer. Meaning that your timer has already stopped!

I don’t know what you’re trying to do exactly, but you should use a timer if you know in advance how much time you want to run something. In your car it seems to be the opposite?

I suggest you to move your code to _process(), that way you’ll decrease every c_cd value by one, each frame. If this is too fast for you, you could setup a timer to do that every x seconds. But make sure to use a timer WITHOUT one_shot activated. That way, the timeout function will be triggered every x seconds indefinitely. You could then use stop if EVERY value is at 0.