Giving health to characters, help me please

Started by G, Sun 19/02/2006 19:32:27

Previous topic - Next topic

G

Hi there, people.

I'm making a game with a player character that can die if he gets damaged several times.

I supose I sould use variables and structs, but I can't make them work.

By the other side, I'd like to show the health status of the cahracter, by a graphic that changes in the GUI.

Some ideas? I try one time and another, but I have no good results.

Thank you all.

monkey0506

#1
As for storing the health...try something like this:

Code: ags
struct HealthCharacter {
  int Health;
  Character* Char;
  };

HealthCharacter hcEgo;


Then in game_start you could do something like:

Code: ags
hcEgo.Char = cEgo;
hcEgo.Health = 100;


Or, if you wanted to set them both at once you could define HealthCharacter like this:

Code: ags
struct HealthCharacter {
  int Health;
  Character* Char;
  import bool Set(Character* Char, int Health);
  };

bool HealthCharacter::Set(Character* Char, int Health) {
  if (Char == null) return false;
  if (Health < 0) Health = 0;
  if (Health > 100) Health = 100;
  this.Char = Char;
  this.Health = Health;
  return true;
  }


Then in game_start you could do:

Code: ags
hcEgo.Set(cEgo, 100);


And of course with either method to set the Character's health you could do:

Code: ags
hcEgo.Health++; // increase the character's health
hcEgo.Health--; // decrease the character's health
hcEgo.Health = 50; // set the character's health to 50
// etc.


As for the graphic to display the health, make the GUI, then put a button on it the size of the health bar, and set it's image to the full health bar image.  Set it to Clip the image, and then, in repeatedly_execute put something like:

Code: ags
btnHealth.Width = hcEgo.Health * 2; // this will set the button to be 2 times the character's health, i.e. 1 health will be 2 pixels, 100 health will be 200 pixels


I hope this helps.  Let me know what kind of results you get and if you need anything else. :D

[EDIT:]

And just FYI, if you use this method, you could replace all instances of cEgo with hcEgo.Char (presuming you make the HealthCharacter struct (by placing it in the header) and the hcEgo instance (by exporting and importing it) global).  That way you could keep track of the character and his health all with one variable.

G


Charles

Greetings,

Monkey, this seems like the best way to do the lifebar, at least the best way I could find on the forum.  However I can't seem to get it to work.

I put this into the begining of my 'Global Script'

Code: ags
 struct HealthCharacter {
  int Health;
  Character* Char;
  import bool Set(Character* Char, int Health);
  };

bool HealthCharacter::Set(Character* Char, int Health) {
  if (Char == null) return false;
  if (Health < 0) Health = 0;
  if (Health > 100) Health = 100;
  this.Char = Char;
  this.Health = Health;
  return true;
  }


This into #sectionstart game_start:
Code: ags
hcEgo.Set(cEgo, 100);


Then this into #sectionstart repeatedly_execute:
Code: ags
btnHealth.Width = hcEgo.Health * 2;


I made the image and tried naming different things that seemed to coincide with the script lines...  Nothing seems to get it going.

hcego calls errors no matter what I do...

Yes I'm new here, but I have been messing with AGS for a few weeks, looking through the forums and I even went so far as to convert the entire manual to wrd format so I could print it in under 200 pages.  Reading the manual cover to cover doesn't qualify me as an expert by no means but I don't think I'm flying blind.

Any help you could offer would be greatly appreciated.

Ashen

#4
Knowing what errors you get would be a start. When you say you put the code "into #sectionstart game_start (/ repeatedly_execute)", are you actually putting it into the function, or just on the line after the #sectionstart? (Obvious I know, but it's always best to check the simple stuff first.)

Also, you need to have declared hcEgo somewhere as monkey did:
Code: ags

HealthCharacter hcEgo;



Have you just not shown it here, or is it missing? (Again obvious but maybe worth checking.)
I know what you're thinking ... Don't think that.

Joe

Hey, why don't you try this?

Code: ags

//At the top of global script

int Health[300]; //creates 300 health variables

//At game_start section

int i=0;
while (i<300){
Health[i]=100; //All Health variables come 100 at the begining
i++;
}

//Some script, e.g.:

Health[player.ID]-=10; // player loses 10 health points

//At your GUI label

healthlabel.Text=String.Format("Health: %d",Health[player.ID]);


Copinstar © Oficial Site

SSH

Even better would be:
Code: ags

//At the top of global script

int Health[AGS_MAX_CHARACTERS]; //creates health variables for everyone

//At game_start section

int i=0;
while (i<AGS_MAX_CHARACTERS){
Health[i]=100; //All Health variables come 100 at the begining
i++;
}


12

monkey0506

I prefer to try and keep things grouped together. If he uses a struct then he could use hcEgo.Char in place of cEgo, and his health variables would also be stored there. Plus to me hcEgo.Health is more descriptive than Health[0], but then you could use Health[cEgo.ID]. So it just comes down to preference I suppose.

Charles

#8
Quote from: Ashen on Mon 06/11/2006 16:30:51
Knowing what errors you get would be a start. When you say you put the code "into #sectionstart game_start (/ repeatedly_execute)", are you actually putting it into the function, or just on the line after the #sectionstart? (Obvious I know, but it's always best to check the simple stuff first.)

Also, you need to have declared hcEgo somewhere as monkey did:
Code: ags

HealthCharacter hcEgo;


Have you just not shown it here, or is it missing? (Again obvious but maybe worth checking.)


You were absolutely right... Ã, I had the scripts outside the functions which is what was causing the errors. Ã, So now the initial script works right, I think....

Now I need to figure how to make a hotspot add health.
I'm guessing the local action has to change the global variable for the health to show up?

What I have so far:

Code: ags
 
function hotspot1_a() {
if (Health < 10) {display ("The pad repairs your damage"); health += 90; }
if (Health => 10) {display ("Your Damage is not great enough, the pad has no effect");  }
}




Khris

Shouldn't struct definitions go into the header?

You'll have to put "export hcEgo;" right beneath "HealthCharacter hcEgo;", then you'll have to put "import HealthCharacter hcEgo;" at the end of the script header.
Thus hcEgo is a global variable that can be accessed from room scripts, too.

Code: ags
function hotspot1_a() {
  if (hcEgo.Health < 10) {
    Display("The pad repairs your damage");
    hcEgo.Health += 90;
  }
  else
    Display("Your Damage is not great enough, the pad has no effect");


First of all, it has to be hcEgo.Health.
You don't have to use {} if there's only one command. {} is used to group multiple commands together.
Last, your original code would've displayed both messages if health was lower than 10, because after the health was rised by 90, the second condition would have been true, too.

monkey0506

Structs don't have to go in the script header unless they need to be global (like the HealthCharacter struct would be).

Also, capitalization is not just important, it's vital. You used Health and health to mean hcEgo.Health*. You also called the display function instead of Display.


*Assuming you were using the struct-method I displayed, which based on your comments, you are.

SMF spam blocked by CleanTalk