Scripts:Activate doors with a use key

Jump to navigation Jump to search

If you want to be able to activate all your doors with the use key you can set the following script as the global or per-map on-keypress script.

If you don't want the player to be able to activate a door by stepping on it, either put walls around it, or use the suspenddoors command to prevent doors from activating normally (for example, place that command in a map autorun script, or in your new-game and load-game scripts to affect the whole game).

plotscript, use doors on keypress, begin
  if (player is suspended || hero is walking(me)) then (exit script)
  if (keyval(key:enter) > 1 || keyval(key:ctrl) > 1 || keyval(key:space) > 1) then, begin
    variable (x, y)
    x := hero x(me)
    y := hero y(me)
    # Find the tile in front of the hero
    switch (hero direction(me)) do, begin
      case (up)    y -= 1
      case (down)  y += 1
      case (left)  x -= 1
      case (right) x += 1
    door := door at spot(x, y)
    if (door > -1) then (use door(door))

Note that this script won't work for activating a door over the edge of a wrap-around map.

A simple solution for a single door is to place an invisible NPC at the door and make it run this script:

plotscript, door, door id, begin
  use door (door id)

Set the Script Argument on the NPC to the right door ID.