enabling hotspots/regions depending on NPCs room

Started by DrakeStoel, Sat 11/08/2012 11:25:04

Previous topic - Next topic

DrakeStoel

I'm trying to make it so that when a guard is in a particular room, a hotspot becomes available, and a region preventing you from moving on disappears. This is the code I'm using:

Code: AGS
function room_AfterFadeIn()
{
    if (player.PreviousRoom == 9) player.Walk(500, 260, eBlock, eAnywhere);
    
    if (cMGrd.Room == 1){
      hMansionDoor.Enabled = true;
      region[1].Enabled = false;
    }
    else if (cMGrd.Room == 10){
      hMansionDoor.Enabled = false;
      region[1].Enabled = true;
    }
    else Unhandled();
}


When I run the game, the hotspot is already on, and the region is still there. Anyone's help would be greatly appreciated  :grin:

Andail

#1
First of all, why not use a walkable area to decide where the player can and cannot go?

Second, mind that you're blocking the rest of the script when the player is instructed to walk with an eBlock. So the hotspots and stuff aren't affected until the player is done walking. One way to solve this is by handling all the enabling/disabling of hotspots and areas in the "enter room before fade-in" instead.

If nothing works, check that the other character is indeed in the specified room by having something like
function room_AfterFadeIn()
{
Display ("The guard is in room %d", cMGrd.Room);
}
or similar. Just to make sure.

Khris

Alternatively, you can move the player.Walk line below the rest of the code.
Btw, what does Unhandled() do here?

DrakeStoel

Hmmmm... I tried your suggestions, but they didn't work. And I'm not sure what the else Unhandled() was for, so I took that out.
It's strange because when I try running it with the guard staring in room 1, the hotspot is on, and the region's off. But when I start with the guard where he should be, in room 10, then get rid of him, it doesn't work.

Andail

#4
Ah.
This code won't magically update just because the guard character leaves.
You need to check this repeatedly, so put it in repeatedly execute instead.
Code: ags

function room_AfterFadeIn()
{
    if (player.PreviousRoom == 9) player.Walk(500, 260, eBlock, eAnywhere);
}
function room_RepExec()
{
    if (cMGrd.Room == 1 && hMansionDoor.Enabled != true){
        hMansionDoor.Enabled = true;
        region[1].Enabled = false;
    }
    else if (cMGrd.Room == 10 && hMansionDoor.Enabled !=false){
        hMansionDoor.Enabled = false;
        region[1].Enabled = true;
    }
}

(I put an extra if-clause there to prevent the repeatedly execute to keep running the same function over and over again - not that it's very necessary)

DrakeStoel

Bingo! Thanks a bunch, it worked perfectly!

Khris

Btw, assuming some AI isn't sending the guard through multiple rooms, a simpler way would be to just call the relevant commands right when you're removing the guard from the room.
Putting stuff in rep_exe is only necessary to handle reactions to situations that happen at some arbitrary point in the future.

SMF spam blocked by CleanTalk