Need script for follow character (SOLVED - Open-source game)

Started by holden24, Sun 07/01/2007 23:28:15

Previous topic - Next topic

holden24

But how I want the character to follow is not like how you think.
I want the character to follow the player like in '7 Days a Skeptic'.

Dan_N

#1
Basically, all you have to do is tell your little welder to walk to the x and y coordinates of the player in the repeadly execute function. Like this:

cWelder.Walk(cEgo.x, c.Ego.y, eBlock, eWalkableAreas);

Unfortunately, this only works good when in the same room. So when the player leaves the room, you need to tell you welder to follow the player, until they are in the same room. Like this:

function repeatedly_execute
{
if (cWelder.Room==cEgo.Room)
{
  cWelder.Walk(cEgo.x, c.Ego.y, eBlock, eWalkableAreas);
}
if (cWelder.Room!=cEgo.Room)
{
  cWelder.FollowCharacter(cEgo, int distancefromcharacter, int eagerness);
}
if (cWelder.IsCollidingWithChar(cEgo)==1)
{
  // kill the player off functions
}
}

Hope that helps.

PS: Writing script is harder when you're not doing it in AGS ;).

Khris

Dan, calling a blocking Walk every 1/40 seconds isn't gonna help... :=

I'm not exactly sure how it was done in 7daS, I believe the welder appeared at a random door after a random amount of time.

You'd have to store the door coordinates of every room in an array:
Code: ags
// top of global script
int doorx[100];
int doory[100];
int x; int y;

//inside game_start
  doorx[10]=2; // room 1 has 2 doors
  doorx[11]=120;  // door 1: x-coord 120
  doory[11]=220;  // door 1: y-coord 220
  doorx[12]=180;  // door 2: x-coord 180
  doory[12]=220;  // door 2: y-coord 220
  doorx[20]=3;  // room 2 has 3 doors
  doorx[21]=110;  // aso
  ...


then use something like this:
Code: ags
// global script

function repeatedly_execute() {
  if (IsTimerExpired(1)) {
    cWelder.x=x;
    cWelder.y=y;
    cWelder.room=player.room;
    cWelder.FollowCharacter(player, 1, 0);
  }
  if (cWelder.IsCollidingWith(player)) {
    ..death
  }
}

bool welderhunting=false;  // global bool holding whether welder is hunting
export welderhunting;

function on_event (EventType event, int data) {
  if (event==eEventEnterRoomBeforeFadein && welderhunting) {
    int i=data*10;
    int ra=Random(doorx[i]-1)+1;
    i=i+ra;
    x=doorx[i];
    y=doory[i];
    SetTimer(1, Random(120)+80);  // 2 to 5 seconds till welder's gonna appear
  }
}

PIT

KrisMUC all that scripting scares me!!!

Quote

function repeatedly_execute
{
if (cWelder.Room==cEgo.Room)
{
  cWelder.Walk(cEgo.x, c.Ego.y, eBlock, eWalkableAreas);
}
if (cWelder.Room!=cEgo.Room)
{
  cWelder.FollowCharacter(cEgo, int distancefromcharacter, int eagerness);
}
if (cWelder.IsCollidingWithChar(cEgo)==1)
{
  // kill the player off functions
}
}

I'm gonna try this and see what happens.

holden24

I think I'm Going to try that too.

Were you looking for a script like this too PIT?

Khris

Hehe, good luck. You'll need it :=

This part:
Code: ags
if (cWelder.Room==cEgo.Room)
 {
  cWelder.Walk(cEgo.x, c.Ego.y, eBlock, eWalkableAreas);
 }

is already faulty.

What exactly scares you about my code?
If you want behavior like 7DaS, you won't get around using a tiny bit of "advanced" scripting, you know?

Akatosh

He'll come back, I guess. It's a bit unfair if the welder doesn't give you the slightest chance to escape  ;D

PIT

#7
EDIT: KhrisMUC's code removed - see 5 posts up for full thing.

All of this is really hard to understand, does doorx mean my own door or what.
and how this:

Code: ags
if (cWelder.Room==cEgo.Room)
 {
  cWelder.Walk(cEgo.x, c.Ego.y, eBlock, eWalkableAreas);
 }


Faulty?

Ashen

#8
There's two major things wrong with that, that I can see: It's blocking, so the player wouldn't have a chance to escape the Welder character (as Akatosh noted, not necessarily a problem if that's what the games maker wanted, but it seems a bit unfair). And if it wasn't blocking (as KhrisMUC said about Dan's original post), it'll be called once a game loop (default 40 times a second) - which means the Welder will never actually start moving, if it doesn't cause the game to crash. Changing the eBlock parameter and adding a check if (cWelder.Moving == false) will solve that.

As to the rest of the code - although this might be psuedocode, you'll obviously need to replace int distancefromcharacter, int eagerness with actual values. You'd probably also be better using if(cWelder.IsCollidingWithChar(cEgo) != 0) (or more simply if (cWelder.IsCollidingWithChar(cEgo)), as in Khris' code), as IsCollidingWithChar can give fairly arbitary values and may never actually return exactly 1. There also doesn't seem to be a way to start or stop the Welder following, which Khris' code has.
(EDITED: As apparently you CAN use FollowCharacter on someone not in the current room.)

Looking at Khris' code, I'd say doorx[Room*10] is how many possible entrances to a room there is (e.g. doorx[10] = 2; means there are 2 doors in room 1. doorx[(Room*10)+n)] and doory[(Room*10)+n)] are the coordinates a charcter will appear at coming into Room through door n. So:
Code: ags

  doorx[10]=2; // room 1 has 2 doors     1(room) * 10 = 10
  doorx[11]=120;  // door 1: x-coord 120  1(room) * 10 + 1(door) = 11
  doory[11]=220;  // door 1: y-coord 220
  doorx[12]=180;  // door 2: x-coord 180  1(room) * 10 + 2(door) = 12
  doory[12]=220;  // door 2: y-coord 220


Set Door 1, Room 1 to 120, 200 and Door 2, Room 1 to 180, 200. It does look a bit overcomplected, but it's a way to make sure the Welder character appears at the right point - by a door rather than in the middle of the room. The first part of the code, in game_start would be where you set these. The last bit (on_event) randomly picks a door for the welder to appear at, and sets a delay before he shows up.

A simpler way might be to use Hotspot walk-to points to set the positions, and perhaps a Custom Property to identify whether or not the Hotspot is a door. You could then randomly choose a door for the Welder to walk though and have them appear at the right point without quite so much messing around. Some semi-advanced scripting would still be needed, though.
I know what you're thinking ... Don't think that.

Dan_N

Aw, I messed up. Sorry for confusing you there, holden.

holden24

Quote from: Dan_N_GameZ on Thu 11/01/2007 06:42:17
Aw, I messed up. Sorry for confusing you there, holden.

Don't worry about it, I knew I needed to change eBlock to eNoBlock.
I also changed eWalkableAreas to eAnywhere beacuse my "welder" can fly  :P

I'm using Dan_n_Gamez's code to do it, I also added a label that shows where the "welder" is. It works OK.
Did you know that when characters follow you, sometimes they go through HUNDREDS of rooms in about 2 seconds to find you?!

WHAM

I've been thinking about having a chaser in my games...

Would the following sound like it would work (I wont type the code, 'cause I cant remember it without having AGS open in front of me)?

Two regions in the middle of a corridor. When the player steps off region 1 (left) onto the region 2(right), the character giving chase is moved immediately to the left end of the corridor, and be given a script to go to the same coordinates as the player.

This way I would not need to script any ways for the chaser (welder) to FIND the room the player is in, as the script only kicks in when the player is in a certain location. This would also make it impossible for the chaser to appear too close to the player character, and kill him off right there and then, without a possibility of escape. This way the chaser will also always appeat to be BEHIND the player, and not apperar annoyingly in FRONT of the player, like the welder did in 5 day and 7 days.

I'll try to create this within a few days, and see what happens.
Wrongthinker and anticitizen one. Utterly untrustworthy. Pending removal to memory hole.

Khris

Sounds definitely like it would work although this is more of a game design-related question than a technical one.

WHAM

Quote from: KhrisMUC on Tue 17/04/2007 12:10:42
Sounds definitely like it would work although this is more of a game design-related question than a technical one.

Yeah, kinda offtopic, but I just wanted to ask if it sounded like it would work.

Besides, in this thread there's a ton of good stuff that I'll use, and If I come up with anything new, I'll be posting here too.
Wrongthinker and anticitizen one. Utterly untrustworthy. Pending removal to memory hole.

holden24

That's actually not a bad idea WHAM. I can't beleive (with my unseemingly small mind) that I did't think of that.  ;D
I'm gonna try this out  :)
Thanks   :D

holden24

#15
It works.

http://rapidshare.com/files/26943488/Chase.zip

EDIT- Oops, sorry, double-post  :P

WHAM

I've been trying my version of this now, but I get an error on this:

Code: ags

// main global script file

function repeatedly_execute() {
  if (cAi3.IsCollidingWith(player)) {
    QuitGame;
  }
}


The error says that IsCollidingWith is not a public member of 'character'. What am I doing wrong here? What am I missing? This part would be the only part I need to have as repeatedly execute, as all the rest is done is separate room's scripts.
Wrongthinker and anticitizen one. Utterly untrustworthy. Pending removal to memory hole.

Ashen

IsCollidingWith isn't a valid function. Try replacing it with cAi3.IsCollidingWithChar(player). (And looking stuff like that up in the manual before posting :))

Is that a mistake in the game code, or a typo of yours, WHAM?

Sidenote; I can't download this to check, it keeps coming out as a corrupted file. If the problem is with the downloadable code, can someone fix it and re-upload? Or just re-upload if it's OK.
I know what you're thinking ... Don't think that.

WHAM

well, I copy/pasted it from this thread so I THOUGHT it would work as is, and therefore believed the mistake was mine. Sorry about this.
Wrongthinker and anticitizen one. Utterly untrustworthy. Pending removal to memory hole.

SMF spam blocked by CleanTalk