Door Switches and NPCs

Started by deadsuperhero, Sat 06/06/2020 23:10:41

Previous topic - Next topic

deadsuperhero

Hey all!

I've gotten back into playing with AGS regularly after a long hiatus. It's been a lot of fun learning how to work with a newer release of the engine, digging into features I had never bothered with before.
One thing that I'm working with for my new game involves opening and closing doors - in the past, I'd just move a character to a coordinate and change rooms, but I wanted to go with something a little more advanced.

Here's a script that I wrote that allows the player character to open and close the door on a building:

Code: ags

// Note: this example assumes that you have a door object in your room with the ID of 0, or name of oDoor.
// Another assumption is that the WalkableArea value that connects the indoors and outdoors has an ID of 3
// Effectively, all this script does is checks and sets states based on when the player steps on a given region.

// Keep in mind that the Object Properties are prepared outside of this script. The properties are:
// "open", and "inside_building", both boolean values.

// These properties are set based on which region the player steps on. 
// It's assumed that region 1 is inside the doorway, region 2 is outside it.
// Each region, upon being stepped on, switches itself off and turns on the other one
// while setting the "inside_building" state.

// One last assumption: Currently, the "animations" only consist of a single initial frame, so no actual animation
// is done in this example. I may update this later as I add animation into my own game to better demonstrate the
// "correct" way to animate door objects.


function openDoor()
// Sets the Door object to a specific loop in a set view, then sets an object propery of Open to True, and adjusts the baseline for the new door height
// Turns on a Walkable area that bridges indoors and outdoors
{
  object[0].SetView(6,  1,  0);
  oDoor.SetProperty("open", true);
  object[0].Baseline = 160;
  RestoreWalkableArea(3);
}

function closeDoor()
// Same thing as last function, but in reverse
// This time, we set the view to a closed door picture, set the value to false, adjust the baseline, and turn the walkable area off.
{
  object[0].SetView(6, 0,  0);
  oDoor.SetProperty("open", false);
  object[0].Baseline = 145;
  RemoveWalkableArea(3);
}

function oDoor_Interact()

// Checks the state of the Door object. If the "open" value is false, we'll set a view for an open door, set the "open" property to "true",
// Then we run the openDoor function!
{
  
  if (object[0].GetProperty("open") == false) {
 
 // The inside_building check is to determine whether or not the player character is outside or indoors, so that we know where to correctly move him to.
    
    if (object[0].GetProperty("inside_building") == false) {
    cEgo.Walk(49, 157,  eBlock, eWalkableAreas);
    cEgo.FaceObject(oDoor, eBlock);
    openDoor();
    cEgo.Walk(68,  138, eBlock, eWalkableAreas);
    }
    
    else {
    cEgo.FaceObject(oDoor, eBlock);
    openDoor();
    cEgo.Walk(52,  145, eBlock, eWalkableAreas);
    }
  }
  
// Do the same thing as before, but backwards.
// Run the closeDoor function!
  
  else {
   closeDoor();
  }
    
}

function region1_WalksOnto()
// We use a region to act as a switch; when the player steps onto it, the game registers the player as being inside the building

{
  closeDoor();
  oDoor.SetProperty("inside_building", true);
  region[1].Enabled = false;
  region[2].Enabled = true;
}

function region2_WalksOnto()
// Same idea, this time we denote the player as being outside instead.
{
  oDoor.SetProperty("inside_building", false);
  region[1].Enabled = true;
  region[2].Enabled = false;
  closeDoor();
}


Sorry for the wall of text. Effectively, my current setup is a switch system based on regions and the evaluation of the door's state, as well as whether the player is indoors or not. I have two semantic questions, with which I can try to make my script better:

1. Currently, the Properties are assigned to the Door object itself: "open" and "inside_building". However, the second property would seem to pertain more towards the player character than the door. Would it make more sense to instead apply that that property and value to the player character instead?

2. While everything I've already written works, it currently only works for the player character, cEgo, and it only works on one specific door. Is there a good way to further abstract this code into a generic function that works for every character, on every door? My thought here is that maybe it would be possible to pass a character's script name as a variable, so that walk coordinate instructions and "inside_building" states could be set individually on any character.
The fediverse needs great indie game developers! Find me there!

SMF spam blocked by CleanTalk