0 votes

Sorry if i cant explain myself good in the title, but i do have a var called stamina, and it reduces when the player is running, until 0, but it can stop until any number beetween 0 and 100 if the player stops running, if the player stops running then a func called recharge adds 20 points to stamina until it hits 100 or more, the problem is that the stamina can add up to 119 if stopped running at 99 stamina, how can i put a maximum to the stamina adding? sorry for my bad english.

Godot version 3.3.2
in Engine by (37 points)

1 Answer

+1 vote

How about just checking the stamina value?

const MAX_STAMINA = 100

func _recharge(stamina):
    #your code
    if stamina > MAX_STAMINA:
        stamina = MAX_STAMINA

UPDATE:
I wanted to add another example since Wakatta had a brilliant idea to use clamp (see in the comments).

const MIN_STAMINA = 0
const MAX_STAMINA = 100

var stamina = 79


func _ready():
    _recharge(20)
    _recharge(20)


func _recharge(stamina_increase):
    stamina = clamp(stamina + stamina_increase, MIN_STAMINA, MAX_STAMINA)
    print(stamina)

This will give you the following output:

99
100
by (84 points)
edited by

yes, thanks for the help

Happy to help. Please vote my answer if that's not too much trouble. :)

Another way to skin the cat is to use clamp

func _recharge(stamina):
    stamina = clamp(stamina, 0, 100)

0 being your minimum value
100 being your max

Excellent point. I don't think I'd use a clamp for skinning cats though. :D

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.