Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Tournk on Thu 17/04/2014 02:56:14

Title: How to declare a struct for a character?
Post by: Tournk on Thu 17/04/2014 02:56:14
This question is about structs, how do you use them on characters?
I already know how to use them (kind of, it works though), but how to use them like the code below?

Code (ags) Select

cNorman.health = 50;
cGuy.health = 50;
cPerson.health = 50;


I'm getting tired of using the struct of NPC.. Like this code below:

Code (ags) Select

struct NPCHealth
{
int health;
};

NPCHealth NPC[10];

// after some line of code...

NPC[0].health = 50;
NPC[1].health = 50;


It's easier to have the character name than the array NPC to have a .health extension. Can you help me?

Thanks.

p.s I hope my question is not too garbled up, but if it is, tell me so that I can make it clearer.
Title: Re: How to declare a struct for a character?
Post by: monkey0506 on Thu 17/04/2014 05:14:12
AGS doesn't directly have a way to add new members to the existing structs, but you can add new methods. So while you can't add a Character.health member, you could add Character.GetHealth and Character.SetHealth:

Code (ags) Select
struct CharStats
{
  int Health;
};

CharStats CharStat[100];

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

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


Usage:

Code (ags) Select
cNorman.SetHealth(50);
cGuy.SetHealth(cGuy.GetHealth() - 20);
Title: Re: How to declare a struct for a character?
Post by: Tournk on Fri 18/04/2014 14:06:19
Wow. That looks complicated, I try it later and compare which is easier. I haven't fully understand the usage of 'this' and '.ID' yet.

Thanks for the help though.  :)
Title: Re: How to declare a struct for a character?
Post by: ROOKMAGE on Fri 18/04/2014 20:31:40
Code (ags) Select
this
Declares it as an extender function, so instead of making the function work like this:
Code (ags) Select
GetHealth(cCharacter);
it works like this:
Code (ags) Select
cCharacter.GetHealth();.
This will then be your variable for the character. Now you take this.ID, which will find the number the character is referenced to (hopefully in your arrays as well) and use it to find the array for the character.
Sorry if this isn't what you meant when you said you don't fully understand :P