Scripting advice

From OHRRPGCE-Wiki
Jump to navigationJump to search

Here is some advice you can follow when writing scripts to reduce the number of bugs in your scripts and generally make the whole experience more pleasant.

Readable scripts[edit]

The most important thing is to make your scripts easy to read and easy to understand. That way the bugs will be easier to find, you will be able to figure out what the script is doing when you come back to it months later and have forgotten everything, and other people will be able to understand your scripts when you ask for help.

Structure and indent your code, it prevents mistakes, and makes missing begin and end errors far easier to fix. Add comments explaining things! Also, when asking other people to look over your code, they will be far less likely to throw you out of a window :)

Try not to write scripts that are too long. Splitting things up into several little scripts (even for big scenes) generally makes them easier to understand.

Use constants from your game's HSI file, the builtin constants, including those in scancode.hsi, and define your own constants using define constant whenever possible. "get item(item: hp potion)" is so much easier to understand than "get item(13)". You will thank yourself later!

Name your scripts, variables and script arguments things that tell what they actually are or do. npcref could be a good name for a variable which holds an npc reference, but guard is much better.

Consider for example this properly indented and commented script with one mistake:

# Triggered when player approaches guards at the bridge outside the port
plotscript, stopped by guards, begin
  show text box (4)  # "Halt, the toll is 50 gold"; choice box "Pay"/"Don't pay"
  wait for textbox
  if (check tag (tag: pay toll)) then (
    if (pay money (50)) then (
      show textbox (5) # Paid
      set tag (tag: paid toll, on)
    ) else (
      show textbox (6) # Can't afford it
  ) else (
    show textbox (7) # "Get out of here!"
  )
  wait for textbox
end

Can you see where the missing ) is?

General advice[edit]

If you are a beginner, using one of the Plotscripting Tools instead of Notepad will let you more easily get help about each script command. Otherwise, keep the Plotscripting Dictionary open in another window for quick reference while you are working, and check commands you are uncertain of (if you are using Hamster Whisper, by press F1 on the command). The same is true of any reference materials that you might find useful, such as a dictionary, if English is not your first language.

You may need to start the game from the beginning (don't use a previous save) when

  • you have changed the stats or spells of one of the heroes in the hero editor
  • you have changed certain options in the general game data

When you have a partially finished script that you do not want included with your compiled scripts, you can disable it without deleting it by adding # before each line (a # makes the rest of the line a comment, so it is ignored by the compiler). When you are ready to work on the script again, you can un-comment it by deleting the #'s

As often as possible, test the scripts you have just made several times just to be sure that they work correctly.

See Also[edit]