Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: G on Sun 19/02/2006 19:32:27

Title: Giving health to characters, help me please
Post by: G on Sun 19/02/2006 19:32:27
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.
Title: Re: Giving health to characters, help me please
Post by: monkey0506 on Sun 19/02/2006 19:58:15
As for storing the health...try something like this:

struct HealthCharacter {
  int Health;
  Character* Char;
  };

HealthCharacter hcEgo;


Then in game_start you could do something like:

hcEgo.Char = cEgo;
hcEgo.Health = 100;


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

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:

hcEgo.Set(cEgo, 100);

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

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:

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.
Title: Re: Giving health to characters, help me please
Post by: G on Sun 19/02/2006 20:04:09
Thank you very much

I'll try.
Title: Re: Giving health to characters, help me please
Post by: on Mon 06/11/2006 16:06:23
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'

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:
hcEgo.Set(cEgo, 100);

Then this into #sectionstart repeatedly_execute:
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.
Title: Re: Giving health to characters, help me please
Post by: 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:

HealthCharacter hcEgo;



Have you just not shown it here, or is it missing? (Again obvious but maybe worth checking.)
Title: Re: Giving health to characters, help me please
Post by: Joe on Mon 06/11/2006 16:51:21
Hey, why don't you try this?


//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]);


Title: Re: Giving health to characters, help me please
Post by: SSH on Mon 06/11/2006 16:58:32
Even better would be:

//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++;
}


Title: Re: Giving health to characters, help me please
Post by: monkey0506 on Mon 06/11/2006 17:57:03
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.
Title: Re: Giving health to characters, help me please
Post by: on Mon 06/11/2006 18:06:18
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:

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:


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");  }
}



Title: Re: Giving health to characters, help me please
Post by: Khris on Mon 06/11/2006 23:15:38
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.

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.
Title: Re: Giving health to characters, help me please
Post by: monkey0506 on Mon 06/11/2006 23:25:58
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.