Difference between revisions of "User:Una"

From Wiki
Jump to: navigation, search
Line 1: Line 1:
= Building up and finishing script. Basic and advanced tehcniques =
+
== Quick Guide: #True & #False ==
  
== First words ==
+
=== Author: Una ===
 +
=== Purpose ===
 +
To teach you the idea of #true and #false and how to use em in scripts
  
=== Purpose/Idea ===
+
== #True and #False ==
 +
=== How to use em and why ===
  
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.
+
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.
 
+
=== 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>
Mainloop:
+
set %UseMagery yes
  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.
+
  
=== Gosub: How to take everything out from these ===
+
if %UseMagery = yes
 +
  Use Spell Mini heal
  
* If something can be thought as separate part of script, make it to Sub
+
if %UseMagery = no
* Always create subs so, that they dont use global variables, but use Gosub parameters
+
  Heal with bandages
* 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>
gosub TestSub first second third
+
set %UseMagery #true
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.
+
  
"Bad way" (pseudo code)
+
if %UseMagery
<pre>
+
  Use Spell Mini Heal
set %BackpackID ABCDEF
+
set %IDofSomething GHIJKL
+
gosub TestSub
+
  
sub TestSub
+
if ! %UseMagery
  Open %BackPack
+
   Heal with bandages
   Find %IDofSomething
+
return
+
 
</pre>
 
</pre>
Why is this bad? Becouse it needs those variables to be set in somewhere. Its hard to debug.
+
This may look confusing at start, but further look, will make it all clear. Lets take a closer look at those lines:
 
<pre>
 
<pre>
set %BackpackID ABCDEF
+
if %UseMagery
set %IDofSomething GHIJKL
+
gosub TestSub
+
 
+
sub TestSub %BackpackID %IDofSomething
+
  Open %1
+
  Find %2
+
return
+
 
</pre>
 
</pre>
This is much easyer to debug, becouse you know exactly what variables it gets in and what it does.
+
This will check, if %UseMagery is #true. If it is, then "if" sentence gets executed.
Even better sub would be with comments. Like this.
+
 
<pre>
 
<pre>
set %BackpackID ABCDEF
+
if ! %UseMagery
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>
 
</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
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.
+
Using #true and #false makes your script look better and easyer to understand. If you can, use these.
<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>
+
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>
+
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.
+

Revision as of 11:33, 3 April 2006

Quick Guide: #True & #False

Author: Una

Purpose

To teach you the idea of #true and #false and how to use em in scripts

#True and #False

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.

set %UseMagery yes

if %UseMagery = yes
   Use Spell Mini heal

if %UseMagery = no
   Heal with bandages

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.

set %UseMagery #true

if %UseMagery
   Use Spell Mini Heal

if ! %UseMagery
   Heal with bandages

This may look confusing at start, but further look, will make it all clear. Lets take a closer look at those lines:

if %UseMagery

This will check, if %UseMagery is #true. If it is, then "if" sentence gets executed.

if ! %UseMagery

This is same, but it will turn things upside down. It will check if its UNtrue. So if this is #false, it gets executed Using #true and #false makes your script look better and easyer to understand. If you can, use these.