Difference between revisions of "User:Una"

From Wiki
Jump to: navigation, search
Line 1: Line 1:
== Quick Guide: #True & #False ==
+
= Building up and finishing script. Basic and advanced tehcniques =
  
=== Author: Una ===
+
== First words ==
=== Purpose ===
+
To teach you the idea of #true and #false and how to use em in scripts
+
  
== #True and #False ==
+
=== Purpose/Idea ===
=== How to use em and why ===
+
  
Boolean #true and #false are like ON / OFF variables. If something is ON, Positive, running or it has happened, its #True. If something is OFF, negative or has not yet happened its #False.
+
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.
 
<pre>
 
<pre>
set %UseMagery yes
+
Mainloop:
 +
  gosub CheckForTools
 +
  gosub Dig
 +
  gosub CheckWeight
 +
goto Mainloop
 +
</pre>
 +
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.
  
if %UseMagery = yes
+
=== Gosub: How to take everything out from these ===
  Use Spell Mini heal
+
  
if %UseMagery = no
+
* If something can be thought as separate part of script, make it to Sub
  Heal with bandages
+
* 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?
 +
 
 +
<pre>
 +
gosub Test
 +
 
 +
sub Test
 +
return This_is_test
 +
 
 +
display ok #result
 +
halt
 
</pre>
 
</pre>
Most of beginners do it this way. Theres nothing really wrong, its just harder way. If you do it this way, you dont have to type so much and it looks better.
+
 
 +
Display now should read "This_is_test" . Great isnt it?
 +
 
 +
==== Gosub parameters ====
 +
I show now, HOW to use these and WHY.
 
<pre>
 
<pre>
set %UseMagery #true
+
gosub TestSub first second third
 +
gosub TestSub This Is Great
 +
sub TestSub
 +
  Display ok %1 %2 %3
 +
return
 +
</pre>
 +
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.
  
if %UseMagery
+
"Bad way" (pseudo code)
  Use Spell Mini Heal
+
<pre>
 +
set %BackpackID ABCDEF
 +
set %IDofSomething GHIJKL
 +
gosub TestSub
  
if ! %UseMagery
+
sub TestSub
   Heal with bandages
+
  Open %BackPack
 +
   Find %IDofSomething
 +
return
 
</pre>
 
</pre>
This may look confusing at start, but further look, will make it all clear. Lets take a closer look at those lines:
+
Why is this bad? Becouse it needs those variables to be set in somewhere. Its hard to debug.
 
<pre>
 
<pre>
if %UseMagery
+
set %BackpackID ABCDEF
 +
set %IDofSomething GHIJKL
 +
gosub TestSub
 +
 
 +
sub TestSub %BackpackID %IDofSomething
 +
  Open %1
 +
  Find %2
 +
return
 
</pre>
 
</pre>
This will check, if %UseMagery is #true. If it is, then "if" sentence gets executed.
+
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.
 +
<pre>
 +
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
 +
</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.
 +
 
 +
==== 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.
 +
 
 
<pre>
 
<pre>
if ! %UseMagery
+
gosub FindId ABC #backpackID
 +
if #result = #false
 +
  display ok Did not find item
 +
else
 +
  display ok #result
 +
halt
 +
;=================
 +
; 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
 
</pre>
 
</pre>
This is same, but it will turn things upside down. It will check if its UNtrue. So if this is #false, it gets executed
+
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.
Using #true and #false makes your script look better and easyer to understand. If you can, use these.
+
  
This was all for Booleans today. Hope you got something from this Quickye!
+
=== #True and #False ===
<hr>
+
  
[[Tutorials| Back to Tutorials]]
+
Learn to use these. If you have variables, that state ON/OFF type of things, then you can use #true and #false.

Revision as of 11:42, 3 April 2006

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.

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
else
  display ok #result
halt
;=================
; 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.