Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: McMonsterBrothers on Fri 08/07/2011 04:40:36

Title: How to script health?
Post by: McMonsterBrothers on Fri 08/07/2011 04:40:36
I am interested in making a sort of boxing game similar to Mike Tyson's Punch Out!!!! on the NES (Nintendo Entertainment System).  I was wondering how I would go about scripting in a sort of health on how different punches would take down a certain amount of health from the character being hit, and also when a person's health hits zero, the fall to the floor and require to get up before a certain amount of time.  And even if possible, how would i go about setting up a gui to represent how much health a person has???

Im sorry if this is not a beginner question lol
Title: Re: How to script health?
Post by: monkey0506 on Fri 08/07/2011 06:28:07
This has actually been asked many a time. It's relatively simple to store current and max health levels in int variables. You can also use extender methods to link your variables into your characters, which is primarily aesthetic, but is nice for organization and such. :)

// Script.ash

import int GetHealth(this Character*);
import int GetMaxHealth(this Character*);
import void SetHealth(this Character*, int health);
import void SetMaxHealth(this Character*, int maxHealth);

// Script.asc

int character_health[];
int character_max_health[];

function game_start()
{
  character_health = new int[Game.CharacterCount];
  character_max_health = new int[Game.CharacterCount];
}

int GetHealth(this Character*)
{
  return character_health[this.ID];
}

int GetMaxHealth(this Character*)
{
  return character_max_health[this.ID];
}

void SetHealth(this Character*, int health)
{
  character_health[this.ID] = health;
}

void SetMaxHealth(this Character*, int maxHealth)
{
  character_max_health[this.ID] = maxHealth;
}


Usage would then be something like:

player.SetHealth(80);

Of course you may want to add some checks to make sure health isn't set to exceed max health, but that's the basic gist of it.

As for displaying this on a GUI, there's various ways you could implement that. For example you could have a button with a background image set to represent full health, then simply change the button width according to the character's health percentage. So long as you have it set to clip the image then you now have a functioning health bar.

There's other ways to do it too of course, but it would depend exactly what you are wanting to achieve.
Title: Re: How to script health?
Post by: McMonsterBrothers on Fri 08/07/2011 06:39:43
Quote from: monkE3y_05_06 on Fri 08/07/2011 06:28:07
This has actually been asked many a time. It's relatively simple to store current and max health levels in int variables. You can also use extender methods to link your variables into your characters, which is primarily aesthetic, but is nice for organization and such. :)

// Script.ash

import int GetHealth(this Character*);
import int GetMaxHealth(this Character*);
import void SetHealth(this Character*, int health);
import void SetMaxHealth(this Character*, int maxHealth);

// Script.asc

int character_health[];
int character_max_health[];

function game_start()
{
  character_health = new int[Game.CharacterCount];
  character_max_health = new int[Game.CharacterCount];
}

int GetHealth(this Character*)
{
  return character_health[this.ID];
}

int GetMaxHealth(this Character*)
{
  return character_max_health[this.ID];
}

void SetHealth(this Character*, int health)
{
  character_health[this.ID] = health;
}

void SetMaxHealth(this Character*, int maxHealth)
{
  character_max_health[this.ID] = maxHealth;
}


Usage would then be something like:

player.SetHealth(80);

Of course you may want to add some checks to make sure health isn't set to exceed max health, but that's the basic gist of it.

As for displaying this on a GUI, there's various ways you could implement that. For example you could have a button with a background image set to represent full health, then simply change the button width according to the character's health percentage. So long as you have it set to clip the image then you now have a functioning health bar.

There's other ways to do it too of course, but it would depend exactly what you are wanting to achieve.

thanks dude!!! but is there a way that i can lower this health occording to if they get hit (like a fighting game)
Title: Re: How to script health?
Post by: monkey0506 on Fri 08/07/2011 06:55:55
Yes. You script it.

Really I can't be more specific than that until you actually determine what method you're going to use to detect the character being hit. It shouldn't actually be that difficult, but by asking what you just did, you've basically just told us that you have absolutely no sense of direction in how you're going to try and accomplish this.

I don't mean that to come across as rude, but this type of project is rather ambitious for someone who is inexperienced in how to handle events in AGS. You should at least take a look at the tutorial and Character functions and properties before you set your heart on doing this in AGS. It's not impossible or even that difficult, but you need to have some understanding of how AGS operates if you're planning to use it.

Also, please don't quote the entire post above you when replying. Especially when it's me. I can be rather verbose. :P

But really, we can understand who you're talking to without you quoting the whole post. If you really feel the need, just address the person by their name. It won't hurt. ;)