4.1.1 but it doesn’t really matter which version it is
Question
For example, I want to play a sound when a specific variable is true:
func _process():
if variable:
$Audio.play()
This would start the sound on every frame which is not what I want. I could create a second variable and make it true on just one frame and only play the sound then, but that’s so inconvenient and probably really bad code
That I want to play a sound is just an example, I need this in almost every project, HOW DO I DO IT
func _process():
if variable:
$Audio.play()
variable = false
Usually this is somewhat akin to a state machine. I would try to leverage an animation system, or a signal that happens during the event to call the play function.
So basically if the value is set true somewhere, just remove the Boolean = true and just play the audio then.
Sure, it really depends. I don’t think there is anything wrong with your approach. What ever method, your mission is to not play it every frame.
Let’s say this is a treasure chest and you want to transition to an open state. Once open some process is done. “variable” is your open state.
With a state machine you can define a closed and open state. When an action to open happens you would play the sound and transition to open state. I would tuck the play sound into the animation or the action sensor to transition.
This situation can usually be solved with a finite state machine. And there are multiple forms of it. Godot has a built in state machine hidden in the AnimationTree node.
Yes, if we want to keep it simple, and you used variable for other things, you just need to add another Boolean that is set to false once it’s been set to true by some event. I like to call it a “latching” mechanism.
func _process():
if variable:
...
If variable_play:
$Audio.play()
variable_play = false # latch it
...
As you might notice this could get convoluted if you keep using this pattern. Finite state machines try to simplify this. This pattern above could be called a decision tree, which is actually the basis for basic AI.