Topic was automatically imported from the old Question2Answer platform.
Asked By
AcidPanda
How can I make a variable that was changed slowly go back to it’s original value?
I’m trying to find out how to slowly, over time, return a variable to it’s default value.
The default would be 0. if it was changed to -1, it would work it’s way back to 0; ( Same with positive 1)
const VARIABLE_DELTA # how much per frame to de-increment variable
const DEFAULT_VALUE = 0 # desired resting position of variable
func _process(delta):
if variable > DEFAULT_VALUE: # can also use != or <, obviously
reduce(variable, delta) # but this is specific to greater than comparison
...
func reduce(_variable, delta):
_variable -= VARIABLE_DELTA * delta # omitt delta if you don't need fps precision
clamp(variable, DEFAULT_VALUE ) # don't let it get too small
...
func clamp(_variable, _default_value): # underscores because reusing variable names is scary
if _variable < _default_value
_variable = _default_value
To fix the “Of course, the indentation is incorrect”…
Paste working code into your forum post (or edit your existing post)
Select the code
Press the “{ }” button in the forum editor’s toolbar.
Done
jgodfrey | 2020-07-30 20:53
Thanks, didn’t think you could highlight a block and then apply the formatting. I write the code for my answers in this editor and the code block doesn’t behave well when you write like that.