0 votes

"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
in Engine by (30 points)

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.

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).

1 Answer

+3 votes
Best answer

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
by (266 points)
selected by

it works, thanks

I'm glad to hear it.

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

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

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.