Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: JoanCortada on Tue 26/09/2017 18:14:21

Title: Make a character follow you (playable character) in some room but not in others
Post by: JoanCortada on Tue 26/09/2017 18:14:21
Hello,
That's my first post, and I'm also creating my first AGS game, I'm working on it for 2 month, since it's my first experience with that took me long time to get everything done, even if I'm reading post and watching all tutorials I can, but there is a problem I can not solve in anyway:
I want to have a gost (a character) following the main character just in the dark rooms, some of the room have light and others are dark, I would like that ghost to follow me just in the dark ones. I have tryied all I know and it doesn't works, seems to be really easy to do but I do not find the way, I have tryied to do a "if" and say that if the character enters to a especific room the ghost do not follow the playable character, but it doesn't work. I will be happy with all the help you can give me.
Thanks.
Title: Re: Make a character follow you (playable character) in some room but not in others
Post by: Slasher on Tue 26/09/2017 18:59:28
Hi

a quick example of one way that should do it...

Code (ags) Select

GLOBAL.asc REP EXECUTE

function repeatedly_execute() {
if(Player.Room==3 || Player.Room==6 || Player.Room==8){ // Your Dark Rooms
cGhost.ChangeRoom(player.room); // changes cGhost to player room
cGhost.FollowCharacter(player, 5, 80); // Distance and eagerness.. Depends what you want to happen.
}
else {
cGhost.FollowCharacter(null); // stops cGhost following player to next light room
}
}


You could try it...

Title: Re: Make a character follow you (playable character) in some room but not in others
Post by: Crimson Wizard on Tue 26/09/2017 19:50:46
Quote from: Slasher on Tue 26/09/2017 18:59:28
a quick example of one way that should do it...

Code (ags) Select

GLOBAL.asc REP EXECUTE


Why repeatedly_execute again? no need to restart same actions continiously 40 times per second!

There is a global function called on_event, you may add it to GlobalScript.asc. Within it you may catch a moment when any new room is loaded:
Code (ags) Select

function on_event (EventType event, int data)
{
    if (event == eEventEnterRoomBeforeFadein)
    {
       // your follow code here
    }
}
Title: Re: Make a character follow you (playable character) in some room but not in others
Post by: Slasher on Tue 26/09/2017 20:35:21
Of course you would be correct.. But I expect this new user knows nothing about on_event function ...
Title: Re: Make a character follow you (playable character) in some room but not in others
Post by: Snarky on Tue 26/09/2017 20:47:58
A good rule of thumb for novice AGS coders is that if you're putting code in repeatedly_execute_always(), you're almost certainly Doing It Wrong. Or at least, there's probably a better way.
Title: Re: Make a character follow you (playable character) in some room but not in others
Post by: Crimson Wizard on Tue 26/09/2017 21:59:43
Quote from: Slasher on Tue 26/09/2017 20:35:21
But I expect this new user knows nothing about on_event function ...

But are not we replying here to tell about things new users do not know?
Title: Re: Make a character follow you (playable character) in some room but not in others
Post by: Khris on Tue 26/09/2017 22:55:07
Quote from: Slasher on Tue 26/09/2017 20:35:21Of course you would be correct.. But I expect this new user knows nothing about on_event function ...
And this matters how?
Title: Re: Make a character follow you (playable character) in some room but not in others
Post by: JoanCortada on Fri 20/07/2018 11:49:49
Hello, I still have the same problema, for some reason I am not able to make that NPC behave as I want, the idea is to have a ghost following you in certain rooms, (rooms with light off) and not in others (rooms with light on), I have tried many things, this is the las code I use but the ghost keeps showing himself in all the rooms. I really have no idea how to solve this.

function cFantasma1_AnyClick()
{
  if(cJordi.Room==20 || cJordi.Room==21 || cJordi.Room==22 || cJordi.Room==23 || cJordi.Room==24 || cJordi.Room==25 || cJordi.Room==26 || cJordi.Room==27 || cJordi.Room==28 || cJordi.Room==29 || cJordi.Room==30 || cJordi.Room==31 || cJordi.Room==32 || cJordi.Room==33 || cJordi.Room==34 || cJordi.Room==35){
    cFantasma1.FollowCharacter(cJordi);
}
  else {
    cFantasma1.FollowCharacter(null);
}
}
Title: Re: Make a character follow you (playable character) in some room but not in others
Post by: morganw on Fri 20/07/2018 12:17:09
If you are running that in response to a click, and the click was the one which changed the room, cJordi.Room will return the previous room number (even if you change room on an earlier line in the script, that change won't occur until the script processing has completed). If the click is in the new room (i.e. you are using a light switch) that would be too late.

You could try this:

function on_event (EventType event, int data) {
    if (event == eEventEnterRoomBeforeFadein)
    {
        // data is the room number of the new room
        if (data >= 20 && data <= 35)
        {
            cFantasma1.FollowCharacter(cJordi);
        }
        else
        {
            cFantasma1.FollowCharacter(null);
        }
    }
}


...which should (untested) run the code as the room changes.
Title: Re: Make a character follow you (playable character) in some room but not in others
Post by: JoanCortada on Mon 23/07/2018 03:16:31
morganw thanks a lot, I'm going to try it later and let you know if this Works.
Title: Re: Make a character follow you (playable character) in some room but not in others
Post by: JoanCortada on Tue 24/07/2018 05:46:50
Hello, I tried the code you provide me and seems to be a error on it. I tipe this:


function cFantasma1_AnyClick()
{
function on_event (EventType event, int data)
{
  if (event == eEventEnterRoomBeforeFadein);
{
    if (Room >= 20 && Room <= 35)
    {
      cFantasma1.FollowCharacter(cJordi);
    }
    else
    {
      cFantasma1.FollowCharacter(null);
    }
  }
}
}

But I get the message saying there are compilations errors
Title: Re: Make a character follow you (playable character) in some room but not in others
Post by: Slasher on Tue 24/07/2018 06:23:08
You can't put a function within a function to start with, afaik.

on_event types usually go in the Global.
Title: Re: Make a character follow you (playable character) in some room but not in others
Post by: Khris on Tue 24/07/2018 11:04:03
Please always post the exact error message you get.

Anyway, on_event is supposed to go inside GlobalScript -> Edit Script. If you want the ghost to only follow after a click, you'll have to add a variable to keep the state of that. If the ghost has to be clicked in each room first, a different approach is needed.
Please tell us exactly what the desired behavior is.
Title: Re: Make a character follow you (playable character) in some room but not in others
Post by: morganw on Tue 24/07/2018 11:56:58
Quote from: Khris on Tue 24/07/2018 11:04:03Anyway, on_event is supposed to go inside GlobalScript -> Edit Script.
Defining on_event will also work in a room script.
Title: Re: Make a character follow you (playable character) in some room but not in others
Post by: Crimson Wizard on Tue 24/07/2018 12:59:14
Quote from: JoanCortada on Tue 24/07/2018 05:46:50
Hello, I tried the code you provide me and seems to be a error on it. I tipe this:


function cFantasma1_AnyClick()
{
function on_event (EventType event, int data)
{
...

You have a function "on_event" inside function "cFantasma1_AnyClick". Functions cannot be inside other functions.