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?
cNorman.health = 50;
cGuy.health = 50;
cPerson.health = 50;
I'm getting tired of using the struct of NPC.. Like this code below:
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.
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:
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:
cNorman.SetHealth(50);
cGuy.SetHealth(cGuy.GetHealth() - 20);
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. :)
this
Declares it as an extender function, so instead of making the function work like this:
GetHealth(cCharacter);
it works like this:
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