Jibble

Author Topic: Door Switches and NPCs  (Read 75 times)

DeadSuperHero

  • There's no sense crying over spilled tears.
    • I can help with backgrounds
    • I can help with characters
    • I can help with making music
    • I can help with PR
    • I can help with story design
    • I can help with voice acting
    • I can help with web design
Door Switches and NPCs
« on: 06 Jun 2020, 23:10 »
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: Adventure Game Studio
  1. // Note: this example assumes that you have a door object in your room with the ID of 0, or name of oDoor.
  2. // Another assumption is that the WalkableArea value that connects the indoors and outdoors has an ID of 3
  3. // Effectively, all this script does is checks and sets states based on when the player steps on a given region.
  4.  
  5. // Keep in mind that the Object Properties are prepared outside of this script. The properties are:
  6. // "open", and "inside_building", both boolean values.
  7.  
  8. // These properties are set based on which region the player steps on.
  9. // It's assumed that region 1 is inside the doorway, region 2 is outside it.
  10. // Each region, upon being stepped on, switches itself off and turns on the other one
  11. // while setting the "inside_building" state.
  12.  
  13. // One last assumption: Currently, the "animations" only consist of a single initial frame, so no actual animation
  14. // is done in this example. I may update this later as I add animation into my own game to better demonstrate the
  15. // "correct" way to animate door objects.
  16.  
  17.  
  18. function openDoor()
  19. // 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
  20. // Turns on a Walkable area that bridges indoors and outdoors
  21. {
  22.   object[0].SetView(6,  1,  0);
  23.   oDoor.SetProperty("open", true);
  24.   object[0].Baseline = 160;
  25.   RestoreWalkableArea(3);
  26. }
  27.  
  28. function closeDoor()
  29. // Same thing as last function, but in reverse
  30. // 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.
  31. {
  32.   object[0].SetView(6, 0,  0);
  33.   oDoor.SetProperty("open", false);
  34.   object[0].Baseline = 145;
  35.   RemoveWalkableArea(3);
  36. }
  37.  
  38. function oDoor_Interact()
  39.  
  40. // 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",
  41. // Then we run the openDoor function!
  42. {
  43.  
  44.   if (object[0].GetProperty("open") == false) {
  45.  
  46.  // 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.
  47.    
  48.     if (object[0].GetProperty("inside_building") == false) {
  49.     cEgo.Walk(49, 157,  eBlock, eWalkableAreas);
  50.     cEgo.FaceObject(oDoor, eBlock);
  51.     openDoor();
  52.     cEgo.Walk(68,  138, eBlock, eWalkableAreas);
  53.     }
  54.    
  55.     else {
  56.     cEgo.FaceObject(oDoor, eBlock);
  57.     openDoor();
  58.     cEgo.Walk(52,  145, eBlock, eWalkableAreas);
  59.     }
  60.   }
  61.  
  62. // Do the same thing as before, but backwards.
  63. // Run the closeDoor function!
  64.  
  65.   else {
  66.    closeDoor();
  67.   }
  68.    
  69. }
  70.  
  71. function region1_WalksOnto()
  72. // 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
  73.  
  74. {
  75.   closeDoor();
  76.   oDoor.SetProperty("inside_building", true);
  77.   region[1].Enabled = false;
  78.   region[2].Enabled = true;
  79. }
  80.  
  81. function region2_WalksOnto()
  82. // Same idea, this time we denote the player as being outside instead.
  83. {
  84.   oDoor.SetProperty("inside_building", false);
  85.   region[1].Enabled = true;
  86.   region[2].Enabled = false;
  87.   closeDoor();
  88. }
  89.  

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.
"There are three side effects of acid: enhanced long-term memory, decreased short-term memory, and I forget the third." - Timothy Leary

Check out my
art