Player can pick up ammos but health not

adcodingadcoding Posts: 22Member
in 3D

Hello
In the MainScene I have added two types of pickups: vita and ammo.
When the player walks on the ammos he reloads the weapon.
But when he walks on life, the health bar doesn't go up.
I can't figure out how to do it.

pickup.gd

    extends Area

    enum PickupType {
        Health,
        Ammo
    }

    #stats
    export(PickupType) var type = PickupType.Health
    export var amount : int = 10

    #bobbing
    onready var startYPos : float = translation.y
    var bobHeight : float = 1.0
    var bobSpeed : float = 1.0
    var bobbingUp : bool = true


    # Called every frame. 'delta' is the elapsed time since the previous frame.
    func _process(delta):
        # move us up or down
        translation.y += (bobSpeed if bobbingUp else -bobSpeed) * delta
        #if we are at the top, start moving downwards
        if bobbingUp and translation.y > startYPos + bobHeight:
            bobbingUp = false
        #if we are at the bottom, start moving up   
        elif !bobbingUp and translation.y < startYPos:
            bobbingUp = true

    #called when another body enters our collider
    func _on_Pickup_body_entered (body):
        #did the player enter our collider?
        #if so giv the stats and destroy the pickup
        if body.name == "Player":
            pickup(body)
            #queue_free()

    #called when the player enters the pickup
    #give them appropriate stat
    func pickup (player):

        if type == PickupType.Health:
            player.add_health(amount)
        if type == PickupType.Ammo:
            player.add_ammo(amount)

Tags :

Comments

  • TwistedTwiglegTwistedTwigleg Posts: 4,589Admin

    Are you sure the add_health function in player updates the health bar? Looking at the code, I do not see anything right off that would be causing it not work, especially since the ammo pickup works.

    I would recommend looking at the add_health function and making sure it is actually being called and see if it's supposed to update the health bar. For seeing if it's being called, I would recommend just adding a print statement like print("add_health function called") or similar.

  • adcodingadcoding Posts: 22Member

    The add_health in the player script:

    #adds an amount of health to the player
    func add_health (amount):
        curHp += amount
        ui.update_health_bar(curHp, maxHp)
        if curHp > maxHp:
            curHp = maxHp
    
    
    #adds an amount of ammo to the player
    func add_ammo (amount):
        ammo += amount  
        ui.update_ammo_text(ammo)
    

    I tried to add print("test") in the add_ammo function and it works.
    But if I try to print the same in the add_healthfunction, It is not shown in the output.
    So the function is not called. but why?

  • adcodingadcoding Posts: 22Member

    I tried to delete the health scene and recreate it ... now it works O-O

  • TwistedTwiglegTwistedTwigleg Posts: 4,589Admin

    Well, not sure why it was not working before, but I'm glad it's all fixed now :smile:

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file