# Show a variable in the form of a bar.

#### Zane

##### Food for thought
Hey whats going on community.

I have a question regarding a scripted variable bar.

I understand and have scripted numerous health bars for huds using the .hp_rate etc.

But I want to know is this a exception for variables?

Here is how a health bar would be drawn:

def draw_life(actor, x, y)    bitmap = Cache.system("Bar")    rect = Rect.new(0, 0, bitmap.width * actor.hp_rate, bitmap.height)    contents.blt(x, y, bitmap, rect)    bitmap.dispose  end
My question is how can I apply this to a variable?

Or is this method not possible?

#### Shaz

##### Global Moderators
I'd do something like this:

def draw_generic_bar(x, y, current, max) bitmap = Cache.system("Bar") rect = Rect.new(0, 0, bitmap.width * current / max, bitmap.height) contents.blt(x, y, bitmap, rect) bitmap.disposeendA gradient bar requires TWO values - the current and the maximum. For an actor's hp, that calculation is done inside the hp_rate method.So you need to replicate that, by sending in the current value and the maximum value. This may require two variables, or a variable and a constant.

Last edited by a moderator:

#### Zane

##### Food for thought
The only slight issue I see with this is that the variable is chosen at random and therefore the max and current value would be different every time. Reason backing this is because I was planning on doing a evented battle type game which this bar would define the enemy's health and the random number would depend on difficulty. I can do it easily with the numbers but was hoping to draw it via a graphic or a regular gradient.

#### Shaz

##### Global Moderators
If you can figure out the numbers, you know what to send in to the method.

How are you choosing "the variable" at random, and why?

#### Zane

##### Food for thought
These Variables are for the A.I actor

All stats are designed by variables

1 Variable is the health

When starting a new game it can range depending on difficulty

when easy

100...250

when normal

250...400

when hard

400...1000

This was all set up with a event which works really good. If you were to write this out in a message window it would work out correctly, You can even script the variable on screen if you wanted to without any problems. Doing a graphic bar is more of a eye candy to the user and it would look nicer then 4 sets of numbers.

(However on a small note I might be able to get away with setting up all the actors in the database but this method allows me to randomize it more.)

Last edited by a moderator:

#### Shaz

##### Global Moderators
Well, you still know, at the time you're battling a monster, what the HP and MAXHP are going to be, aren't you? Otherwise what use is the bar if you don't know what the maximum is, or what % they have remaining?

#### Zane

##### Food for thought
Idk if you were wondering about scene switching. It happens all on the map in case you were wondering.

As soon as I call the random number that's when I will have the value for the max life.

That's exactly where I stopped scripting it because there's is no way to determine the bar really, as where actor.hp_rate is hard coded in more.

Hence why I had to bring up this question.

#### Shaz

##### Global Moderators
If you call a random number to get the value for max life, save that into one variable. Use another variable to keep track of current life (HP).

There are the two values you pass into the method. I assume you determine the max life before you make the bar appear.

#### Zane

##### Food for thought
Ah that's something to look into however how am I gonna tell var 2 to check var 1.

Var 1 is the life itself. Well now Max what am I using for var 2? Like the operation of that.

I know it has to operand var 1

#### Shaz

##### Global Moderators
The operation is =.

If you have your random number in var1 (current life), set var2 to be equal to var1 at the start. Then as the battle goes on, var1 will decrease, but var2 will stay the same.

#### Zane

##### Food for thought
And then put the var id's in the current/max slots. Thanks Shaz I'll try it out. Seems like that method will work so you can lock this thread.

#### Shaz

##### Global Moderators
You would just do

draw_generic_bar(x, y, \$game_variables[1], \$game_variables[2])

However, I'm not sure where you want to PUT that script. The original one is in Window_Base. But if your battles are on the map, there IS no reference there to Window_Base. And I don't think Game_Map or Scene_Map have a "contents" to blt to. So there's still a bit of playing around for you to figure out where that script should go, and how to modify it, if necessary, based on where it is.

#### Zane

##### Food for thought
I could build a child class from window_base

and

alias scene_base's start and update and create the window if the scene is on the map

that's how I design all my huds unless that's not a appropriate way?

#### Shaz

##### Global Moderators
It would be Scene_Map's, not Scene_Base's, because you only want these showing on the map, don't you? No need to have them defined in any other scene, then.

#### Zane

##### Food for thought
Your right but I use scene base for quick access to other scenes just in case

I don't think either method hurts really being they are both aliased.

### Latest Profile Posts

Well, I've got a party tomorrow, and my friends are coming over. I wanted to work a bit on the Fandom Scouts assets so I can be ready when that update arrives... in other news, Pizza Time Horror is getting a second teaser, and it shows Cabaret Helen's last performance before the location closed.
Also who used to be scared of Chuck E as a kid? I was! Now I love him!
The new cards and what they do:

(I am clearly not a youtuber... Haha)
Just for the record you CAN use the "Exclude unused files" successfully to trim a project down seriously even if you're doing calls to various assets via plugins and such. BUT you need to do a full playtest of pretty much every option to ensure you did get every one of those other assets added in. Then I dumped the assets into a fresh copy of the project and yay it was like 400 Meg......

If you've played Knight Shift Chapter 1 this image'll mean something to you. If not, that's also fine!