Doors – scripts sample

Doors

https://www.modd.io/play/doors-LLsample

Index

Description

Here is an example of how to make doors. Only map tiles and scripting is used (to make an animated door you need to use an entity instead, with the frames you need for the animation).

Door types

I made 5 doors, using two types of tiles (wall layer) to distinguish normal doors (that need interaction, in the sample a space key press) from automatic doors (the glass ones in my sample).
Also another distinction should be made between fixed (or static, that means they show as always closed) and dynamic doors (that open and close).
Here is a short description of each, from left to right:

  1. Automatic, just teleports. Needs distinct portal and landing regions on both sides, to avoid a portal loop.
  2. Manual, teleports when you press the space key. Needs only one region per side.
  3. Automatic, opens when a unit is near.
  4. Manual, opens and closes by pressing space.
  5. Manual, opens and closes by pressing space. If left open for more than 5 seconds, it closes automatically.

Technical annotations:

  • All scripts regarding unit teleporting or unit interaction should be local scripts, whereas the scripts to open and close the doors (removing or adding again the map tile for the wall layer) should be in the global scripts.
  • Make an entity variable of type number for the unit types that will use the doors and assign initial value 0, that stands for no door active. Static doors need two variable values for the two sides (i recommend using negative numbers, e.g. 1 and -1 for the two sides of the same door, as I did).
  • Make one only local (entity) script to deactivate manual doors’ interaction: simply reset the entity door variable to 0 whenever the unit (this entity of script) leaves any region.
  • For the first-type door, extreme left in the sample (fixed and automatic, that means simple teleport), make sure that the portal region and the landing region aren’t too near to each other, to avoid a bug that makes you pass twice or more in a row.
  • When making dynamic doors, remember that the wall layer (layer 3) is layer 2 for the scripts, as code starts counting from zero.
  • For the last (extreme right) door, the self-closing one, the only way at the moment to do it is a delayed repeat loop with a return if the door is meanwhile closed manually (this avoids a self-close before the expected time). Use a global counter variable.
  • Manual dynamic doors (the last two doors, that manually open and close), need a global boolean variable each to know in which state they are, as different units (players) can interact with them.

Credits

All credits for this short guide and for the sample go to myself. The scripts and all the advice is public and open source, ready to be reused.



LL 2024                     ↑ Back to top ↑

3 Likes

Wow, this is an incredible demo. Great description as well!
I recommend linking to a game’s scripts directly.

2 Likes