how do i get a number to stop incrementing at a certain point

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

“All i want is, when im not pressing Q(shield) shield_health will increment all the way to 165
I just cant seem to get anything to work (it keeps going overboard), hopefully you guys would know the answer”

extends RigidBody

func _process(delta):
	if Input.is_action_pressed("shield"):
		print(shield_health)
		show()
	else:
		if shield_health < 165:
			shield_health = MAX_SHIELD_HEALTH
		shield_health += 1
		hide()


var velocity = Vector3(0,0,0)

export var shield_health = 165
export var MAX_SHIELD_HEALTH = 165
export var MIN_SHIELD_HEALTH = 10
onready var timer = get_node("Timer")

func _ready():
	timer.set_wait_time(0.2)
	timer.start()

func _on_Timer_timeout():
	shield_health -= 0.5

I’m kind of curious if adding an else: in the else: of your if statement will work. I’ll try it when I get home and re-comment unless you try it before I do.

   func _process(delta):
        if Input.is_action_pressed("shield"):
            print(shield_health)
            show()
        else:
            if shield_health < MAX_SHIELD_HEALTH:
                shield_health += 1
            else:
                shield_health = MAX_SHIELD_HEALTH  
            hide()

edit: changed 165 to max shield health, figured that’d work better.

Aireek | 2020-04-16 08:56

shield_health += 1 isn’t in the if statement checking if it’s below 165. Think of functions and conditional statements as ‘boxes’ of code to execute that are separated by the tab level (I’m not sure what jargon there is for the tab key).

Magso | 2020-04-16 09:27

:bust_in_silhouette: Reply From: caprinae

As stated in the comments, you probably want to change your code to something like this:

const MAX_SHIELD_HEALTH := 165

func _process(delta):
  if Input.is_action_pressed("shield"):
    print(shield_health)
    show()
  else:
    if shield_health < MAX_SHIELD_HEALTH:
      shield_health += 1
    hide()

Be aware that your code is frame-dependent, which means the rate at which your shield regenerates will depend on the player’s framerate. If you want it to regenerate at a certain rate over time, you should do something more like this:

const MAX_SHIELD_HEALTH := 165
const SHIELD_REGEN_PER_SECOND := 1
var shield_regeneration_time := 0

func _process(delta):
  if Input.is_action_pressed("shield"):
    print(shield_health)
    show()
  else:
    hide()
    # regenerate shields over time
    if shield_health < MAX_SHIELD_HEALTH:
      shield_regeneration_time += delta
      # one second has passed
      if shield_regeneration_time >= 1:
        shield_regeneration_time -= 1
        shield_health += SHIELD_REGEN_PER_SECOND

it works, thanks

Traitors Avatar | 2020-04-16 17:12

I’m glad to hear it.

I edited the answer with some more code that might be closer to what you want.

caprinae | 2020-04-16 17:16

If this answers your question, don’t forget to accept it as the best answer!

caprinae | 2020-04-16 18:59