How to duplicate/multiply a character

Started by Sydrik, Sat 13/11/2010 04:44:39

Previous topic - Next topic

Sydrik

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!

Khris

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.

Code: ags
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:

Code: ags
  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.

Sydrik

Thanks Khris!

I should be putting the AI function in Global.asc too, or the room's asc?

Ryan Timothy B

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:
Code: ags
import function AI(this Character*);


Sydrik


Sydrik

Quote from: Khris on Sat 13/11/2010 05:40:01
Code: ags
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.
}


Code: ags
  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?

monkey0506

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.

Sydrik


SMF spam blocked by CleanTalk