Difference between revisions of "User:Una"
Line 35: | Line 35: | ||
* If something can be thought as separate part of script, make it to Sub | * 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 | * 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. | 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? | ||
+ | |||
+ | <pre> | ||
+ | gosub Test | ||
+ | |||
+ | sub Test | ||
+ | return This_is_test | ||
+ | |||
+ | display ok #result | ||
+ | halt | ||
+ | </pre> | ||
+ | |||
+ | Display now should read "This_is_test" . Great isnt it? | ||
==== Gosub parameters ==== | ==== Gosub parameters ==== | ||
Line 90: | Line 105: | ||
return | return | ||
</pre> | </pre> | ||
+ | |||
+ | 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. | ||
+ | <pre> | ||
+ | ;================== | ||
+ | ; 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 | ||
+ | </pre> | ||
+ | |||
+ | 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. |
Revision as of 10:51, 3 April 2006
Contents
[hide]Building up and finishing script. Basic and advanced tehcniques
First words
Purpose/Idea
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.
Mainloop: 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 halt
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 return
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 return
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 return
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 return
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.