how to make a life bar

Started by Samsam, Thu 18/05/2017 04:33:11

Previous topic - Next topic

Samsam

How can I create a "life bar" or "HP" in a easy way? I also want to increase it with food. Thanks your answers! :D

Grok

In Space Rangers Ep. 52 I use lifebars in i fight scene
https://www.adventuregamestudio.co.uk/site/games/game/2151/




I use a gui for the bars. Using the width to show the length of the bar (in this case the gui for the main character is named gMeterBlue).

gMeterBlue
Height  16
Left    42
Top     39
Width  128

BackgroundColor  36
Clickable  false
Visibility Normal, initially off

Basically just a blue box 


I use an variable (int) for the health (in this case an int named iYazScore).


When ever something happens that changes the health/score I add or subtract from the iYazScore and call a function (Score()) to change the width of gMeterBlue.


Code: ags

function Score()
{
  if(iYazScore>0)
     gMeterBlue.Width=iYazScore;
  else
   {
     gMeterBlue.Width=1;   ////don't allow the width to go below 1 or you will get an error
     gMeterBlue.Visible=false;
     
          --------
   }
 ------
}


The background behind the bars is in this case an object (the meter is shown in only one room). It could be another gui if the meter is to be shown in more rooms.

I hope this is of some help to you. :)

Gurok

#2
I started writing this before Grok made his reply. Follow mine if you want more of a tutorial format.

I think the simplest way to achieve this might be to use a GUI with a button on it.
The button would represent the portion of the health bar that's full and the width of the GUI would be the maximum width of that health bar.
I'm going to suggest you leave the Visibility of the GUI as "Normal, initially on" for now. If you want to turn the health bar on/off, you can add additional scripting for that.



I've positioned the button at 0,0 and made it the full height of the GUI. I called the GUI, "gHealthBar" and the button, "btnHealthBar".
You can set background images for both the GUI and the button to skin the health bar.
As soon as you set background images, the button will no longer resemble a button and the GUI will lose its border.
I also set ClipImage to True on the button. This prevents the parts of the button's background image that fall outside of the button from being shown (exactly what we want for a health bar).
You can see a simple example I've set up here. I used a large green sprite for the health part and a large red sprite for the background of the GUI:



Next, we need a little bit of scripting to control everything.
You can put this in your Global Script:

Code: ags
int health;
int maxHealth = 1;

function UpdateHealthBar()
{
  btnHealthBar.Width = health * gHealthBar.Width / maxHealth;
}

function SetHealth(int value)
{
  if(value < maxHealth)
    health = value;
  else
    health = maxHealth;
  UpdateHealthBar();
}

function SetMaxHealth(int value)
{
  if(value < 1)
    maxHealth = 1;
  else
    maxHealth = value;
  UpdateHealthBar();
}


I've made two variables, one to track the player's current health and one to track his/her maximum health. I've also made a couple of functions to let you control the player's health.
Your goal, as I understand it, is to have a bar that automatically shows the value of these health and maximum health variables.
The bar won't automatically update to show the player's health, but we'll instead update it whenever setHealth or setMaxHealth is called.
With this in mind, you can try a simple test in your game_start to see if the health bar is working:

Code: ags
function game_start() 
{
  SetMaxHealth(30);
  SetHealth(15);
}


This *should* display a health bar that's 50% full. In my case, 50% green and 50% red.
Here's my artistic masterpiece with the health bar in the top left:



With what you have right now, you can call setHealth or setMaxHealth to update the player's health from anywhere in the global script.
What if you want to update the health bar from one of your rooms?
health, maxHealth and the setHealth functions are ONLY available within the script they're defined in.
If you want to make these accessible from elsewhere in your project, you'll need imports.
I would suggest adding two more functions that will help you manage health and maxHealth from rooms and other scripts:

Code: ags
function GetHealth()
{
  return(health);
}

function GetMaxHealth()
{
  return(maxHealth);
}


Now, in the header of your global script, put these lines:

Code: ags
import function SetHealth(int value);
import function SetMaxHealth(int value);
import function GetHealth();
import function GetMaxHealth();


You should now be able to do cool things with your health bar from inside a room, for example:

Code: ags
function oApple_Interact()
{
  SetHealth(GetHealth() * 2);
  oApple.Visible = false;
  Display("You eat the apple and your health doubles!");
}


There are various ways you could extend these functions too. For instance, you could add something to the SetHealth function that, if (value <= 0), shows a game over GUI.
[img]http://7d4iqnx.gif;rWRLUuw.gi

Samsam

#3
Hey Gurok! How i can make that my character lose health? Also thank you both of you for your answers.

Another thing, appears an error, it says " Unable to create a local script: Runtime error: unresolved import 'GetHealth'.

Gurok

#4
If you're using my approach, you can just subtract an amount from the value of GetHealth. Here's an example:

Code: ags
SetHealth(GetHealth() - 20);


For Grok's approach, it would be much the same thing:

Code: ags
iYazScore = iYazScore - 20;
Score();


If you want to have the player steadily lose health over time, you can put it in repeatedly_execute:

Code: ags
function repeatedly_execute()
{
  SetHealth(GetHealth() - 1); // Decrease the player's health by 1 every game cycle
}


and perhaps have something to make it a bit slower:

Code: ags
int healthTick;

function repeatedly_execute()
{
  healthTick++;
  if(healthTick == 5)
  {
    healthTick = 0;
    SetHealth(GetHealth() - 1); // Decrease the player's health by 1 every 5th game cycle
  }
}


Regarding what I mentioned earlier about extending SetHealth, here's a quick example of how you could make the player die if his/her health reached zero:

Code: ags
function SetHealth(int value)
{
  if(value < 1)
    Display("You have died.");
  else
  {
    if(value < maxHealth)
      health = value;
    else
      health = maxHealth;
    UpdateHealthBar();
  }
}


Regarding the error message you received, make sure you have the four lines shown here in your GlobalScript.ash file:
[img]http://7d4iqnx.gif;rWRLUuw.gi

Samsam

#5
I'm sorry for bother you again, Gurok, but i don't get it. The character can be damaged, but the "live bar" can't be update, is it supposed to happen? you commented something about it but i didn't understand what you was tryin to say, my original lenguage isn't english and i get confused quite easy. Please forgive my english. ; --- ;

The last problem is solved, I copied everything again.

Gurok

How does the character get damaged?
[img]http://7d4iqnx.gif;rWRLUuw.gi

Samsam

when he interact with a hotspot he get damaged ( that's the idea, it's just a prove). the ad "you have died" appears but the "live bar" appears like the life is full.

Gurok

Oh, I'm sorry. I made an assumption with that "you have died" message. Here is an updated version that will work as you want:

Code: ags
function SetHealth(int value)
{
  if(value < maxHealth)
    health = value;
  else
    health = maxHealth;
  UpdateHealthBar();
  if(value < 1)
    Display("You have died.");
}
[img]http://7d4iqnx.gif;rWRLUuw.gi

Samsam

it doesn't work

don't care how much time pass it still like that. Maybe is the location. Is this alright?

Slasher

You need to do this one step at a time...

First create your gui that will contain the health bar indicator then create a button that will show the health indication.

Then on to the next step.......

Gurok

#11
Have you set ClipImage to True on the button (btnHealthBar)?

If there are still problems, can you paste in your code for the hotspot interaction?

It might be good (as Slasher indicated) to ensure the basics are right. Can I see a screenshot of your GUI?
[img]http://7d4iqnx.gif;rWRLUuw.gi

Samsam

BEFORE THE TRY: I'm going to do everything again step by step, very slow and pretty carefully.
AFTER ALL (CONCLUTION): Oh, God, I'm pretty embarassed, this is all my fault,  It work but I fell pretty bad for make you lose your time, thanks for your help! I apologize :-[

DBoyWheeler

Just curious, could this also apply to a Stamina and Mana meter, if one were to go for a Quest for Glory style game?

Gilbert

Yes, obviously. There is no reason this cannot be used for some other stats.

SMF spam blocked by CleanTalk