universal script for different characters

Started by Gepard, Fri 30/10/2009 01:27:55

Previous topic - Next topic

Gepard

I have a room with multiple characters. I have a region. The characters are moving randomly on the screen. I want a script (an animation for example) to take place, whenever a character is colliding with the region, but I dont want to write script for each character. I want the script to take place whenever there is any character in the region, BUT I want only the character standing on the region to be affected by that script (understand: only that character to animate). I just want to avoid long script. I want script that will be started by any character, but will only affect the triggerer character. Hope its clear now :fD.

Thanks.
Drink up me 'arties! Yo ho!

RickJ

#1
The approach would be to have an interaction or event handler function for each character (i.e. Character Enters Region).   Each of those functions could call a custom function and pass the character as a parameter.  Here is an untested snippet of code to illustrate what I mean.  Note, you will need to use the lightening bolt icon to create the event function and I'm am no longer certain how the default function names are formed so they will likely differ from cEgo_EntersRegion() as shown below.  And of course CustomFunction() is a name given by you that describes what the function actually does.

Code: ags

*** Room Script ***
function CustomFunction(Character *chr) {
   chr.Say("Hello!");
}

function cEgo_EntersRegion1() {
   CustomFunction(cEgo);
}


[edit]
Fixed BB tags soas to not F$#&up the F%$*ing thread  := 

Gepard

I have troubles implementing this into the game.

My script starts like this:

if (character[C2].IsCollidingWithObject(object[12])==1) {

What I mean is: isnt there a way to go around that [C2]? isnt there something like any character code for this?
Drink up me 'arties! Yo ho!

NsMn

Give the character a script name, like cRoger.

Gepard

That is ok. I mean. The script is working. I just dont want to have a hundred lines script for every one of the twenty characters in the room. I want like universal script that will affect the triggerer character.
Drink up me 'arties! Yo ho!

NsMn

What you need is a for loop.

Code: ags

int for=5;
while(for>0){
if (character[for+3].IsCollidingWithObject(object[12])==1) {
 -code-
}
for++
}  


Will check the IsColliding for the characters with ID 3 to 8.

RickJ

Sorry, gave bad advice before.  Region events are for player character only.  NsMn has the right idea.  Here is some code to use with region instead of object.    The first thing to do is to make an extender function that checks if a character is standing on a region.  Then use that function in the repeatedly execute function of the room script to loop through the list of characters.  COde below is untested but illustrates the method.

Code: ags

*** Script Header ***
import function IsStandingOnRegion(this Character*, int region_id);

*** Global Script ***
// Extender function to detect character standing on specified region
function IsStandingOnRegion(this Character*, int region_id) {
   // Return true if character is standing on the specified region, return false otherwise
   if (Region.GetAtRoomXY(this.x, this.y)==region[region_id) return true
   else return false;
}

*** Room Script ***

function room_RepExec() {
   int i;

    // Make character standing on region 1 say his name
   // First define some constants
   #define REGION_ID 1
   #define NUMCHARS 20
   #define FIRSTCHAR 1

   // Check to see if each character is standing on the region
   i = FIRSTCHAR;
   while (i<NUMCHARS) {
      if character[i].IsStandingOnRegion(REGION_ID) {
         character[i].Say("My name is %s",character[i].Name);
      }
      i++;
   }
}

SMF spam blocked by CleanTalk