Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Atelier on Sat 29/05/2010 16:08:50

Title: Solution to Character Status? [SOLVED]
Post by: Atelier on Sat 29/05/2010 16:08:50
I'm having some trouble with my game; not a technical problem as such, but figuring out the best way to do what I need. When a player enters the room, I need the names of characters who are there. All characters are always at 0, 0. I'd also like to cross-reference these names with a struct I made, CharStatus. For example, if Monk is in the room, but Monk.Status == "Dead", I don't want it to add "A monk is here in good health." So basically I need to get the names of characters in the room, then check their statuses, then produce an appropriate string for both.

Thanks for any help,
AtelierGames
Title: Re: Solution to Character Status?
Post by: Khris on Sun 30/05/2010 13:15:32
You can loop through all characters:

  int i;
  Character*c;
  while (i < Game.CharacterCount) {
    c = character[i];
    if (c != player && c.Room == player.Room) {
      if (c.Status == "Dead") ...
      else ...
    }
    i++;
  }
Title: Re: Solution to Character Status?
Post by: Atelier on Sun 30/05/2010 14:06:07
Hi, thanks for the reply.

I've functioned the code as CheckCharacters(), but on running it says .Status isn't a public member of Character (pointing to 6th line). How can this be solved?
Title: Re: Solution to Character Status?
Post by: Khris on Sun 30/05/2010 15:50:46
Yeah, this was sort of pseudo-code.
You could do something like this

// header
enum cstatus {
  eStatusAlive = 0,   // everybody alive by default
  eStatusDead = 1
};

import int ch_status[100];

// global script
int ch_status[100];
export ch_status;

int Status(this Character*) {
  return ch_status[this.ID];
}


Now you can do this:

// character dies
ch_status[cGuy.ID] = eStatusDead;

  if (c.Status() == eStatusDead) ...
Title: Re: Solution to Character Status?
Post by: Atelier on Sun 30/05/2010 17:01:44
(http://img263.imageshack.us/img263/634/11848715.png)

Thank you so much Khris! :D
Title: Re: Solution to Character Status? (& something else) =D
Post by: Atelier on Mon 31/05/2010 21:18:18
Sorry for bump and double post - new problem at top :-\
Title: Re: Solution to Character Status? (& something else) =D
Post by: Charity on Mon 31/05/2010 22:03:38
Quote from: AtelierGames on Sat 29/05/2010 16:08:50
I know there isn't but is there anything close to Room.CharacterCount/player.InventoryItemCount?

I've faced a similar problem in my on again off again RPG engine project.  The best workaround I was able to come up with is a custom function that checks all the characters in the game and and returns that number of characters in the room.  I was using it along with some funky array business, so I had to call the function every time a new room was loaded or the the Character.ChangeRoom function was called (ended up making a custom function for changing rooms, just so I wouldn't forget.  Not very eloquent.)  But you can probably just make something like

function RoomCharacterCount() {
 int c=0;
 int count=0;
 while (c<Game.CharacterCount) {
   if (character[c].Room == player.Room) {  count++;  }
   c++;
 }
 return count;
}


The only reason you might want to bother with arrays is if you want to make character numbers relative to the room that you can cycle through.

Only problem I can think of is the loops might start to add up and cause slowdown, especially if you have a lot of characters.  But probably not a huge issue.
Title: Re: Solution to Character Status? (& something else) =D
Post by: Atelier on Tue 01/06/2010 14:36:25
Hey, thanks. I've been playing around and seem to have got somewhere. I just need to run a check to see whether count only has one loop left before count equals exactly the number of characters in the room. Something like:


if (Room. ObjectCount >= 1 && o.Visible == false && [something] == count-1) q = 1;
count++;


Unfortunately I have no idea what the [something] needs to be. How can I check when count is on its penultimate loop?
Title: Re: Solution to Character Status? (& something else) =D
Post by: Khris on Tue 01/06/2010 18:37:41
It's really confusing that you edited your first post. Why not simply reply with the second problem?
I for one always check out threads with new replies, even if it says [solved] in the title.

On topic:
It's probably way easier for us to help you if you tell us what you want the game to do as if you didn't know how to code.
What's up with the q?

I'm guessing you want the description to list all characters and objects in one sentence, proper punctuation and all, right?
Title: Re: Solution to Character Status?
Post by: Atelier on Tue 01/06/2010 19:38:40
Right, sorry for the confusion.

I have a custom function Add, with three parameters looking like this:

function Add(String Text, int colour, int space)

String Text is the text to add into the display box.
Int colour is the colour that text is.
Int space defines whether a blank space comes after the text just added. (int space is an optional parameter - by default there is no space when Add is called).

I choose whether to have spaces or not because it makes it so much easier to read when playing (see here). (http://www.adventuregamestudio.co.uk/yabb/index.php?topic=41000.msg542535#msg542535)
So, every time the player enters a new room, I run function CheckRoom, and this checks which objects/characters are there.


int i, q;
...

    while (i < Game.CharacterCount) {
    c = character[i];
    if (c != player && c.Room == player.Room) {
   
       if (c.Status() == eStatusNormal) Add(String.Format("%s is here.", c.Name), 2, q);
       else if (c.Status() == eStatusDead) Add(String.Format("%s is here, dead!", c.Name), 2, q);
       else if (c.Status() == eStatusSleeping) Add(String.Format("%s is here, snoozing soundly.", c.Name), 2, q);
       else if (c.Status() == eStatusDrunk) Add(String.Format("%s is completely drunk!", c.Name), 2, q);

    } i++; }


q is just a variable I'm using for the third parameter of Add, and as I didn't define it there won't be any spaces after the text.

But I want q to be 1 (and therefore have a space) when ALL the character names in the room have been added, like at the bottom of the screenshot a few posts up.

I figured out how to do this by adding a line into the code after i++.


if (i == Game.CharacterCount-1) q = 1;


This means the while loop is on its penultimate run, as it only has one last character to check. So the last time Add is called in the function, q is 1 and a space is added!

However... Game.CharacterCount checks all the characters in the whole game, meaning q will only ever be 1 if all the characters are in the same room. I need something like Room.CharacterCount... but as there is no such thing I need to find an alternative.

So bottom line, how do I set q as 1 when the last while loop is run?

Ps. I seem to have written more than intended. Hope it's clearer though :P

Thanks,
Atelier
Title: Re: Solution to Character Status?
Post by: Khris on Tue 01/06/2010 19:45:41
Couldn't you just add a blank line after the loop?
Title: Re: Solution to Character Status?
Post by: Atelier on Tue 01/06/2010 19:51:12
Blow me down. Thanks again! :D