Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Creator on Wed 09/09/2009 15:39:03

Title: How to make a random character appear (SOLVED)
Post by: Creator on Wed 09/09/2009 15:39:03
I'm making a Wack-a-Mole type game, but I'm making sprites in the likeness of people I know, thus I have more than 1 character. I've already put a function pre-setting the coordinates for the character to appear.


function wackCheck() { // I'm guessing that something has to go in the parenthesis.
 spot = Random(3); // spot is defined in the global script header as it's used in rooms as well as the global script
 
   if (spot == 0) {
   Char.x = 60;
   Char.y = 287;
 }
   else if (spot == 1) {
     Char.x = 448;
     Char.y = 143;
   }
   else if (spot == 2) {
     Char.x = 224;
     Char.y = 128;
   }
   else {
     Char.x = 314;
     Char.y = 378;
   }
   Char.Transparency = 0;
}


How can I tell the function that 'Char' represents a random character?
Title: Re: How to make a random character appear
Post by: Akatosh on Wed 09/09/2009 15:43:26
You can also address characters via a numbered array. As in,


function wackCheck(int charnumber) { // something has to go in the parenthesis.
  spot = Random(3); // spot is defined in the global script header as it's used in rooms as well as the global script
 
    if (spot == 0) {
    character[charnumber].x = 60;
    character[charnumber].y = 287;
  }
    else if (spot == 1) {
      character[charnumber].x = 448;
      character[charnumber].y = 143;
    }
    else if (spot == 2) {
      character[charnumber].x = 224;
      character[charnumber].y = 128;
    }
    else {
      character[charnumber].x = 314;
      character[charnumber].y = 378;
    }
    character[charnumber].Transparency = 0;
}


Note that this is not the only - or most elegant - way to do this by far, but it will certainly work.
Title: Re: How to make a random character appear
Post by: Creator on Wed 09/09/2009 16:03:35
Wow, that was fast. Thanks Akatosh, however I'm having a problem. Here's my code.


function wackCheck(int Char) {
  spot = Random(3);
  Char = Random(1); // I have two characters at the moment and need them to randomly appear
  cJess.Transparency = 100;
  cDaniel.Transparency = 100;
 
  if (spot == 0) {
  character[Char].x = 60;
  character[Char].y = 287;
  }
  else if (spot == 1) {
    character[Char].x = 448;
    character[Char].y = 143;
  }
  else if (spot == 2) {
    character[Char].x = 224;
    character[Char].y = 128;
  }
  else {
    character[Char].x = 314;
    character[Char].y = 378;
  }
  character[Char].Transparency = 0;
  SetTimer(2, 80); //Reset timer
}


I call the funtion with

wackCheck(0); // The 0 is there because I HAVE to specify an integer


The problem is that only the character with the ID of 0 (cJess) appears (still appears at one of the random coordinates, so that's not a problem). Otherwise neither character appears. Is this because character[Char] is getting called differently everytime the line appears?

EDIT - Never mind. Code above works perfectly. cDaniel wasn't in the room. Sorry about that.
Title: Re: How to make a random character appear
Post by: NsMn on Wed 09/09/2009 16:06:31
What happens if you replace the 1 with a higher number (at "Char=Random(1)" )?
Title: Re: How to make a random character appear
Post by: Creator on Wed 09/09/2009 16:08:24
Never mind code works. Explaination in above post.
Title: Re: How to make a random character appear
Post by: Akatosh on Wed 09/09/2009 16:08:54
Well, first of all, I wouldn't put the line with Char inside the function. You can just call it like this...


wackCheck(Random(1));


...meaning you could remove this line...


Char = Random(1); // I have two characters at the moment and need them to randomly appear


Otherwise, I could see no reason why the code shouldn't work. Are you sure cDaniel is character number 1? If yes, how often have you tested the whole thing? The random number generator can screw you over on occasion.

Darn ninjas.

/EDIT: Oh, and just for the laughs - here's a vastly shortened version that should do the same.


function wackCheck(int Char) {
  spot = IntToFloat(Random(3));
  character[Char].x = FloatToInt(154.33*Maths.RaiseToPower(spot, 3.0) - 769.0*Maths.RaiseToPower(spot, 2.0)+ 1002.67*spot + 60.0);
  character[Char].y = FloatToInt( 22.67*Maths.RaiseToPower(spot, 3.0) -   3.5*Maths.RaiseToPower(spot, 2.0)- 163.167*spot +287.0);
  character[Char].Transparency = 0;
  character[1-Char].Transparency = 100; //only works as long as you have exactly two characters
  SetTimer(2, 80); //Reset timer
}


:=