Scripts:Move NPC with the arrow keys

From OHRRPGCE-Wiki
Jump to: navigation, search

You may sometimes want to be able to control an NPC with the arrow keys rather than the hero. This script creates an NPC and lets you move it around until you press ESC.

You may also be interested in the commands set NPC obstructs (to allow the NPC to walk through other NPCs and heroes), set NPC moves (to prevent the NPC from moving by itself if its movetype isn't Stand Still), and set NPC ignores walls.

plotscript, look, begin
   # Create an NPC 1 tile south of the party leader. You could use an existing NPC instead.
   variable (npc cursor)
   npc cursor := create NPC (npc cursor, hero X (0), hero Y (0) + 1)

   suspend player   
   # Cause the camera to follow the NPC instead of the hero
   camera follows NPC (npc cursor)
   # Alternatively, this command causes the camera to remain fixed on a tile (centred on the screen)
   #focus camera (hero X (0), hero Y (0))
   
   while (true) do (
     if (npc is walking (npc cursor) == false) then (
       # Only move the NPC after they've finished the previous movement,
       # also use elseif to prevent moving in two directions at once.
       if      (key is pressed (key:Left))  then (walk npc (npc cursor, left, 1))
       else if (key is pressed (key:Right)) then (walk npc (npc cursor, right, 1))
       else if (key is pressed (key:Up))    then (walk npc (npc cursor, up, 1))
       else if (key is pressed (key:Down))  then (walk npc (npc cursor, down, 1))
     )
     if (key is pressed(key:ESC) || key is pressed(key:Alt)) then (
       # Stop the while loop
       break
     )
     wait (1)
   )
   # Resume normal player movement and camera
   camera follows hero (0)
   resume player
end

You can just as easily put the movement part of the script in an on-keypress script, like the following, instead of using a while loop. You'll probably want to use a map-autorun script to call suspend player and camera follows npc as above.

plotscript, movement keys, begin
  variable (npc cursor)
  npc cursor := 0   # first NPC with ID 0
  if (npc is walking (npc cursor) == false) then (
    # Only move the NPC after they've finished the previous movement,
    # also use elseif to prevent moving in two directions at once.
    if      (key is pressed (key:Left))  then (walk npc (npc cursor, left, 1))
    else if (key is pressed (key:Right)) then (walk npc (npc cursor, right, 1))
    else if (key is pressed (key:Up))    then (walk npc (npc cursor, up, 1))
    else if (key is pressed (key:Down))  then (walk npc (npc cursor, down, 1))
  )
end