[ACE]Having some trouble creating a scrolling health bar

Hi, so I wanted to give it a bit before asking but I'm kind of stuck here - I bumped into this tutorial on how to make an HP bar common event - and I figured it got me into trying to attempt my own system but I'm having a few problems that I need help with.

I figured it would be better to post here rather then the script call support thread - since I tried to get it into a test script - I'm trying to keep it as a function so that I can have another part of the script just trigger things based on conditional statements.

I've been trying to mess with the script and here's what it looks like now:
Spoiler
def healthbardraw

#Checks to see if HP is higher or lower than the scroll variable, then fixes itself to match.
if $game_variables[21] < $game_actors[1].hp
$game_variables[21] += 1
end

if $game_variables[21] > $game_actors[1].hp
$game_variables[21] -= 1
end

#turns the equation into a nice bit of text that I can just throw around in formulas.
def actorhealth
25*($game_variables[21]/$game_actors[1].mhp)
end

screen.pictures[1].show("hp_bar", 0, 12, 12, 25, 25, 255, 0)
screen.pictures[1].move(0, 10, 10, actorhealth, 25, 255, 0, 60)

end

So the big problem here, is that I feel like the health bar is constantly getting "refreshed" causing it to flicker. And the health bar doesn't move - when the script is like this. However, If I take out the bit that draws the picture from the function - and have it run once, the flickering stops and that's fine - but then then the health bar decides to just start shrinking itself really low, or just stretching so far it goes off screen. And seems to be ignoring the script all together.

If anyone know's what's going on and can help with this - I'd appreciate it.

I've moved this thread to Script Support. Thank you.

I managed to get the player health bar working - but the enemy hp bar is not working at all.

I've resorted to making variables since finding the enemy "battle index" seems impossible - I did find an "index" function in the script editor but I have no idea how to call it and it seems pointless right now.

For whatever reason though the enemies movable bar is not appearing on screen - but it is updating because it will give a "division by zero" error when the enemies hp is at 0. So it's checking for measurements - but just not drawing itself onscreen even though it has no excuse not to be doing that?

This is the script:
Spoiler
def enemybar

hp = $game_variables[21]
mhp = $game_variables[22]

if $game_variables[21] > 0
rate = (hp+0.1)/mhp
else
rate = 0
end

screen.pictures[103].show("hp_bar", 0, 300, 12, 25*rate, 25, 255, 0)
screen.pictures[104].show("hp_bar_back", 0, 300, 12, 25, 25, 255, 0)

end
Have you tried using a number under 100 for the pictures? I think the max is 100, unless you have a script that alters that.
Thanks, I tried lowering the number to be under the values the actor[1]'s bar pictures but it doesn't seem to change anything visually.

But - I did try and make a common event to maintain the variables, using Hime's scene interpureter + Hime's global events and it does draw the enemy's green health bar's now - but there's still a big problem when I use the mock hp_rate variables.

If I just plug in "hp" into the xscale it draws a movable health bar (at the wrong size) but putting in the variable that does the hp check results in an empty bar.

I just have no idea what the problem is with this?
Have you set the variables to anything?
Ok, I found out the problem - or a solution to it(?). I kind of had to combine both things to get it working.

Mainly I needed the common event to get the battlers hp/mhp - then I used the script to set up the hp/mhp stuff since the third variable wasn't working (and isn't needed anymore thankfully), and put that in the "enemy hp bar function" script - and now it seems to be working?

Which I'm really glad - though I would like to figure out a way to just have it be only script without the common event (and the variables), but I don't want to tempt fate right now, so its good for now.

Thanks for the help - I was coming really close to giving up on this.
Great that it's all working. Just wondering, where did you put the script? Is it in a particular Class? Are you calling it from the common event?
Haven't really looked into classes yet. It's just a function that runs in a common event (that gets triggered at the start of the game).
Haven't really looked into classes yet. It's just a function that runs in a common event (that gets triggered at the start of the game).
That could be the cause of your problems...

The event command "script" is programmed as a sandbox. As soon as the command ends, all values defined within the scriptbox will be deleted.
That is the main difference to a script placed into the script editor: with a real script, you decide where and when to define or delete any variables.

Another possible cause is how you define and run the common event - it would be helpfull if you give a screenshot of the common event (including trigger)
Well, it's working now - but the common event looks like this:
Spoiler
3K0V2iy.png

The two functions it calls look like this:
Spoiler
def healthbardraw
screen.pictures[102].show("hp_bar", 0, 12, 12, 25*$game_actors[1].hp_rate, 25, 255, 0)
screen.pictures[101].show("hp_bar_back", 0, 12, 12, 25, 25, 255, 0)
end

def troop_hp1

ahp = $game_variables[21]
amhp = $game_variables[22]
ahprate = (ahp+0.1)/amhp

screen.pictures[99].show("hp_bar", 0, 300, 12, 25*ahprate, 25, 255, 0)
screen.pictures[98].show("hp_bar_back", 0, 300, 12, 25, 25, 255, 0)

end

Originally I used the move picture command too - but it was working poorly for me but it works just fine with this.
The two functions it calls look like this:
Spoiler
Code:
def healthbardraw
  screen.pictures[102].show("hp_bar", 0, 12, 12, 25*$game_actors[1].hp_rate, 25, 255, 0)  
  screen.pictures[101].show("hp_bar_back", 0, 12, 12, 25, 25, 255, 0)
end

def troop_hp1
  ahp = $game_variables[21]
  amhp = $game_variables[22]
  ahprate = (ahp+0.1)/amhp # <-- This line breaks the hp rate calculation
  screen.pictures[99].show("hp_bar", 0, 300, 12, 25*ahprate, 25, 255, 0)
  screen.pictures[98].show("hp_bar_back", 0, 300, 12, 25, 25, 255, 0)
end
I have no idea how your images look like, but you made them look 25x bigger? O_O

That hp rate calculation for the enemy is wrong. Your initial calculation probably didn't work because you used:
Code:
ahprate = ahp/amhp
Which will always be 1 or 0, because an integer divided by another integer can only return another integer in coding.
But if you use this:
Code:
ahprate = ahp/amhp.to_f
It will work how it should, since if there are any float values in the calculation, the end result will also be a float value, and that's what you need.
You added 0.1 to the current hp in your calculation, which, even if just by a small amount, will result in display errors for your enemy HP bar.

Your actor HP bar display worked right off the bat because the hp_rate method returns the correct hp rate value already with a float number.

Use code tags for your codes in posts from now on, pretty please! *.*
And they are methods not functions! These JavaScript people can't keep JS terms where they belong! >.>
I;m obviously joking a bit, but they are really called methods! 😛
Haha, They aren't 25 times bigger - they're 1/4th normal size. And I'll make sure to call them methods from now on and use the float stuff.
Ahh, right, I forgot that the zoom you set there is divided by 100 in Sprite_Picture. What a needless extra calculation. Ohh, well, default code is default. 😀

Original thread: https://forums.rpgmakerweb.com/threads/ace-having-some-trouble-creating-a-scrolling-health-bar.90330/