GameSalad

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Health Bar/Mana Bar

PixelGamingPixelGaming Posts: 19Member, PRO

We creating a Mana bar, where if you reach the max you unlock a special. We figured everything out but what we are stuck on is how to max the bar. Basically we want the bar to stop at 100 intergers. For example, if you kill an enemy you receive 15 mana, but if you reached 100 mana you can't go any higher than that. How do we write the code for that, thank you.

Comments

  • JapsterJapster Posts: 672Member
    edited May 2017

    @PixelGaming - Hi guys - simply use the following function in the expression editor in a change attribute etc, behaviour:-

                   min(100,Game.CurrentManaAmount+Game.ManaToAdd)
    

    ...substituting Game.ManaAmount for whatever attribute stores the amount of Mana you currently have, and Game.ManaToAdd for the attribute or calculation for the amount you're trying to add...

    This will always return whichever of the 2 numbers is lower, so normally Mana will be the one returned (50, 97, etc), but if adding mana would take the mana total above 100, then the function will still return the lower of the 2 numbers (in this case, the 100) - very useful for capping all kinds of things!

    Also, if you're picking 100 (like I did when starting out!) because it's easier than calculating a percentage on a status bar, perhaps consider using a percentage calculation? - ie. no reason why you can't have 150, 175, 2,000 Mana etc, and still have the bar display/fill to the correct percentage?

    Of course, if it's 100 because you actually WANT 100, then ignore me... :wink:

  • PixelGamingPixelGaming Posts: 19Member, PRO

    yes I would like the to know the equation about having more than 100 mana!

  • PixelGamingPixelGaming Posts: 19Member, PRO

    Okay so I made the bar and the code, but whenever i press the power move I want it to reset to 0 and the bar to regenerate but it doesn't regenerate. Also how do I stop it from going to negative points

  • tatiangtatiang Posts: 11,795Member, Sous Chef, PRO, Senior Sous-Chef
    edited May 2017

    Increase value:

    When Touch is Pressed
         change attribute game.CurrentManaAmount to min(100,game.currentManaAmount+game.ManaToAdd)

    Decrease value:

    When Touch is Pressed
         change attribute game.CurrentManaAmount to max(0,game.currentManaAmount-game.ManaToAdd)

    Reset value:

    When Touch is Pressed
         change attribute game.CurrentManaAmount to 0

    What do you mean by regenerate? Can you give an example?

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • PixelGamingPixelGaming Posts: 19Member, PRO

    is game.ManaToAdd an attribute or is it like min(100,game.ManaBar+10) ?

    Also for example, you need 100 mana to use your special, once you use your special I want to reset the mana to 0 and let the play regenerate back up to 100 to use the special again. The issue I am having now is I use a bar to show the amount of mana you have, like a health bar. When it resets the 0 the mana regenerates but the bar image doesn't.

  • tatiangtatiang Posts: 11,795Member, Sous Chef, PRO, Senior Sous-Chef

    There's no need for a separate attribute... yes, you can use an integer such as 10 to add to the value.

    I would need to see the code you're using to display the bar. Are you using Replicate, Constrain Attribute or something else?

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • PixelGamingPixelGaming Posts: 19Member, PRO

    I am using contrain attribute so it doesn't go pass 100, also I use replicate for the bar, so whenever mana is added the bar moves up one. The issue is when the mana resets, the bar doesn't. It either starts in the middle or never spawns again

  • tatiangtatiang Posts: 11,795Member, Sous Chef, PRO, Senior Sous-Chef

    Can you post a screenshot of your rules?

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • JapsterJapster Posts: 672Member
    edited May 2017

    ...also, I'm not sure if replication is better in terms of performance overhead (It might well be, and @Socks certainly threw some really cool code examples with massive amounts of replication into the mix, so maybe it is!) than the usual method, which seems to be to use an image (possibly with a tiled image, set to the width of the image used, if you wanted a nice graphical/gradient fill), and just change the width of the actor for the bar's filled area (of course, with a small calculation to keep it left-aligned)...

    There's a great example I think I saw somewhere in the forums I saw a while back!

  • jamie_cjamie_c ImagineLabs.rocks Posts: 5,541Member

    This is my favorite way to create bars (heath, mana, anything). The first video shows how to build a static bar and the second video show you how to create a bar that will travel with your character and also how to set upper and lower limits on the bar (I think).

Sign In or Register to comment.