From Wiki
Jump to: navigation, search

Building up and finishing script. Basic and advanced tehcniques

First words


Im writing this becouse so many beginner and even some Advanced scripters, build hard to edit and nonreadable code. Im trying to show some basic and advanced ideas about good code and teach you to script better scripts.

About author: Una

I have scripted more or less about 3 years now. I know couple of things from scripting and coding and i love to code ( and script ). Im not Pro and i dont claim to be one, but i think i can teach lots to beginners and even to people who have been in "business" for sometime already.

Target audience

In order to be able to read and fully understand this document, you are required to understand the very basics of EUO scripting language. If you are advanced writer and have already puplished some scripts, i think i can still give you something. Hope you like to read and find this "tutorial" usefull. Remember, what i show here, is only my way to do things but i have founded these to be good in my 3 year career with EUO.

Starting to write a script

Break your script to blocks ( in your head or paper ). Say you are building mining script, then it should look like this.

  • Check tools from backpack
  • Use mining tool and target ground
  • Check weight and if too heavy recall/unload

Now, you know where to start scripting. This is the heart of this script. We can now move it to next level. Pseudo- code.

  gosub CheckForTools
  gosub Dig
  gosub CheckWeight
goto Mainloop

This is the mainloop. Every script should have something like this, that gets looped over and over again, until script execution is halted. Next thing would be building those actual subs.

Gosub: How to take everything out from these

  • If something can be thought as separate part of script, make it to Sub
  • Always create subs so, that they dont use global variables, but use Gosub parameters
  • Use return parameter

Return, Use this command always to get out of sub. DONT goto, no matter what. Its bad coding and leads to problems. Did you know:

  • Return command can return info?
gosub Test

sub Test
return This_is_test

display ok #result

Display now should read "This_is_test" . Great isnt it?

Gosub parameters

I show now, HOW to use these and WHY.

gosub TestSub first second third
gosub TestSub This Is Great
sub TestSub
   Display ok %1 %2 %3

Feel free to test that. Did you notice, how those variables are set? %1 is the first variable passed to sub and %2 is second , %3 is third and so on. Always build sub so, that you think its idiot, so that you need to give everything to it, before it can do something. Never assume that it knows it already. Heres two examples, Bad one and good one.

"Bad way" (pseudo code)

set %BackpackID ABCDEF
set %IDofSomething GHIJKL
gosub TestSub

sub TestSub
   Open %BackPack
   Find %IDofSomething

Why is this bad? Becouse it needs those variables to be set in somewhere. Its hard to debug.

set %BackpackID ABCDEF
set %IDofSomething GHIJKL
gosub TestSub

sub TestSub %BackpackID %IDofSomething
   Open %1
   Find %2

This is much easyer to debug, becouse you know exactly what variables it gets in and what it does. Even better sub would be with comments. Like this.

set %BackpackID ABCDEF
set %IDofSomething GHIJKL
gosub TestSub

; name: TestSub
; Author: Una
; Purpose: To open backpack and findstuff
; %1 = Backpack ID
; %2 = What to look for
sub TestSub %BackpackID %IDofSomething
   Open %1
   Find %2

If you create subs with this way, you can easily reuse them. Do it once with time and you dont have to create it ever again. For example, almost every script needs code that opens some bag. Maybe its your backpack, reagent bag or loot bag. Why would you create 100+ lines of code for different bags in different scripts, when you could build just one nice looking sub.

; name: OpenBag
; Author: Una
; Purpose: Opens any bag 
; %1 = ID of bag
sub OpenBag
   if %0 = 0
     return #false
   set #LobjectId %1   ; Sets the first give parameter to #lobjectID
   event macro 17 0    ; Use last object ( ie. Opens bag that id was give )
return #true

See, this is very simple sub, that does not have anykind of error detection ( it does not check, if the bag really opened ) but you can easily use it in any other script you have and even share it with others. Remember to comment subs, so you remember what is it for.

Return from gosub

Return command is very important. If you dont use it, it can cause wierd problems with your script. Always return from sub, dont goto. As i already mentioned previously, you can get parameters out from sub with return command. I told you to always give parameters to sub, never set variables in subs, becouse that makes it harder to use in other scripts, unless its namespaced or there is really something that needs to be set.

gosub FindId ABC #backpackID
if #result = #false
  display ok Did not find item
  display ok #result
; name: FindId
; Author: una
; Purpose: Find id of given type from given bag
; %1 = Type of item
; %2 = Bagid
sub FindId
   Open %2
   FindItem %1
     IF NOT found
       return #false
return #findid

This simple Pseudo script will look for items and if does not find it returns and set #result to #false. If it finds item, it will set #result Id of item. You can return info this way from sub and act on dependin results.

#True and #False

Learn to use these. If you have variables, that state ON/OFF type of things, then you can use #true and #false. I wrote separated quide for these and you can find it here