Make a character follow you (playable character) in some room but not in others

Started by JoanCortada, Tue 26/09/2017 18:14:21

Previous topic - Next topic

JoanCortada

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.

Slasher

Hi

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

Code: ags

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...


Crimson Wizard

Quote from: Slasher on Tue 26/09/2017 18:59:28
a quick example of one way that should do it...

Code: ags

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

function on_event (EventType event, int data)
{
    if (event == eEventEnterRoomBeforeFadein)
    {
       // your follow code here
    }
}

Slasher

Of course you would be correct.. But I expect this new user knows nothing about on_event function ...

Snarky

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.

Crimson Wizard

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?

Khris

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?

JoanCortada

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);
}
}

morganw

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:

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

JoanCortada

morganw thanks a lot, I'm going to try it later and let you know if this Works.

JoanCortada

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

Slasher

You can't put a function within a function to start with, afaik.

on_event types usually go in the Global.

Khris

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.

morganw

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.

Crimson Wizard

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.

SMF spam blocked by CleanTalk