How do I write map autorun scripts?

From OHRRPGCE-Wiki
Jump to: navigation, search


Map autorun scripts have 2 basic uses: to do something when you enter the map, and to run constantly in the background for that map to script some special behaviour. This article will talk about the latter because the first is nothing unusual.

While loops[edit]

A constantly running-in-background script consists primarily of a while loop, which you might want some way to quit out of:

plotscript, autorun script, begin
  variable (exit loop)
  while (exit loop == false) do, begin
    #do actions here...

    wait (1)
  end
end

Note the wait (1) inside the while. The script will continue to run until it encounters a wait or finishs. While it is running, the game isn't, so it is absolutely necessary.

It is then up to you to fill the loop with actions. Write

exit loop := true

to stop the script.

Potential Problems[edit]

Beware changing maps! If you want the script to only run on one map, then you should make it quit when you change map!

variable (map)
map := current map
while (exit loop == false, and, current map == map) do, begin
#...

Bigger Problems[edit]

If the map you change to also has an autorun script which runs for the duration of the map, the above method will not work. Why not? Because when you enter that map, that autorun script is loaded and run before the currently active script. The result is that the older script is never run and can't quit. If all your maps have autorun scripts of this nature, bad things will happen. There is no easy way around this. One suggestion is not to stick loops in the autorun script at all, but to call your scripts from a master script that selects which script to run.

In the following example (which is only one possible solution), map A behaviour and map B behaviour create two different effects on some maps. Each map would have run script A or run script B to trigger each effect, or they would have no autorun script for no special behaviours. master could be the new game script or called at the end of the intro, or anything inbetween.

global variable (1, which autorun script)

plotscript, master, begin
  while (true) do, begin
    if (which autorun script) then (
      run script by ID (which autorun script)
    ) else (
      wait (1)
    )
  end
end

plotscript, run script A, begin
  which autorun script := @map A behaviour
end

script, run script B, begin
  which autorun script := @map B behaviour
end

plotscript, map A behaviour, begin
  which autorun script := 0

  variable (map)
  map := current map
  while (current map == map) do, begin
  #...
end

plotscript, map B behaviour, begin
  which autorun script := 0

  variable (map)
  map := current map
  while (current map == map) do, begin
  #...
end


See Also[edit]