Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Sydrik on Sat 13/11/2010 04:44:39

Title: How to duplicate/multiply a character
Post by: Sydrik on Sat 13/11/2010 04:44:39
Imagine a game where you character has to maneuver through a room of 10 zombies.

The zombies may all use the same sprites, for all I care. But it wouldn't make economical sense to create 10 completely identical zombie characters and scripts for each room, would it?

I've been searching through the help and forums for a solution to duplicate/multiply characters. Doesn't seem to have an answer.

Let's say I have a character called cZombie -- scripting and sprites and views completed. Is there a code that I can duplicate that zombie 9 more times in the room?

Thanks!
Title: Re: How to duplicate/multiply a character
Post by: Khris on Sat 13/11/2010 05:40:01
You have to create ten characters; you can't create characters on the fly in scripts.
Now assign the zombie view to each one.

The final thing to do is write the zombie AI in a generic way.
I used an extender function, "this" points to the character.

function AI(this Character*) {
  // AI code i.e. movement, attacks
  int xd = player.x - this.x;
  int yd = (player.y - this.y)*2;
  if ((xd*xd - yd*yd)<100) {
    // animate attack
    this.SayBackground("Braiiins!");
    // etc.
  }
  etc.
}


AGS starts numbering characters at 0, so say the player character has ID 0 and the zombies have IDs 1 to 10.
Now instead of the script you have for your first zombie, do this:

  int i = 1;
  while (i < 11) {
    character[i].AI();
    i++;
  }


This code is supposed to go inside the Global.asc/repeatedly_execute, I assume that's where you have it.
Title: Re: How to duplicate/multiply a character
Post by: Sydrik on Sat 13/11/2010 06:23:50
Thanks Khris!

I should be putting the AI function in Global.asc too, or the room's asc?
Title: Re: How to duplicate/multiply a character
Post by: Ryan Timothy B on Sat 13/11/2010 06:32:18
That would depend on your game. If you only need it for that one room, you can get away with it being in the room script.
If you want it for all rooms, you should throw it in a global script instead.

If you do put it in a global script, you'll also have to put this in the script header if you want to use the function within the rooms:
import function AI(this Character*);
Title: Re: How to duplicate/multiply a character
Post by: Sydrik on Sat 13/11/2010 06:33:50
Thanks Ryan. I get it. :)
Title: Re: How to duplicate/multiply a character
Post by: Sydrik on Sat 13/11/2010 06:45:21
Quote from: Khris on Sat 13/11/2010 05:40:01
function AI(this Character*) {
 // AI code i.e. movement, attacks
 int xd = player.x - this.x;
 int yd = (player.y - this.y)*2;
 if ((xd*xd - yd*yd)<100) {
   // animate attack
   this.SayBackground("Braiiins!");
   // etc.
 }
 etc.
}


 int i = 1;
 while (i < 11) {
   character[i].AI();
   i++;
 }


One more thing. "Character" in the above codes is the character's REALNAME or is this the correct syntax to use itself?
Title: Re: How to duplicate/multiply a character
Post by: monkey0506 on Sat 13/11/2010 07:26:39
The code Khris provided should be taken as a verbatim example, not a pseudo-code example. That is, where he used "Character" he meant to use "Character" and where he used "character" he meant to use "character". Nothing needs to be changed with the exception of defining your own custom interactions at the appropriate places.
Title: Re: How to duplicate/multiply a character
Post by: Sydrik on Sat 13/11/2010 08:51:36
Thank you. Yes, it works. :)