How to declare a struct for a character?

Started by Tournk, Thu 17/04/2014 02:56:14

Previous topic - Next topic

Tournk

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

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

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.
Reaction is always funny.

monkey0506

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
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
cNorman.SetHealth(50);
cGuy.SetHealth(cGuy.GetHealth() - 20);

Tournk

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.  :)
Reaction is always funny.

ROOKMAGE

Code: ags
this

Declares it as an extender function, so instead of making the function work like this:
Code: ags
GetHealth(cCharacter);

it works like this:
Code: ags
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

SMF spam blocked by CleanTalk