Difference between revisions of "User:Una"

From Wiki
Jump to: navigation, search
(Nimiavaruus muuttujat( Namespace variables ))
(#True and #False)
 
(29 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Suomenkielinen EasyUO scriptaus opas ==
 
  
=== Una, oppaan kirjoittaja ===
 
  
Moro, olen käyttynät EasyUO:ta reilut 3 vuotta ja suurimman osan siitä myöskin scriptannut. Mitään ihmeellistä en ole saanut sinänsä aikaan, monet tietävät minut "Piece of Cake Sparring" scriptan ansiosta. Sekään kerro mitään minun tän hetkisistä taidoistani, koska se on vanha kuin taivas. Ymmärrän kuitenkin koodia kuin tavallista tekstiä ja osaan tehdä yhden jos toisenkin asian EUO:lla.
 
Päätin kirjoittaa tällaisen oppaan, jotta suomea puhuvilla immeisillä ( joilla on englannin taidoissa pieniä aukkoja ) olisi mahdollisuus oppia tämä "jalo" taito.
 
  
=== EasyUO lyhyesti ===
 
  
Mietit varmasti, että mitä kaikkea tällä voikaan tehdä. EUO EI kykene mihinkään mitä et normaalisti pystyisi tekemään. Eli unohda haaveet speedhackista ja toisen laukun sisällön tarkastelusta. On joitakin hienoja asioita missä EUO pystyy "huijaamaan". Esimerkiksi voit "scannata" toisen pelaajan varusteet ja laskea resisti ja muut herkut yhteen vaikka hänellä olisi kaapu peittämässä niitä. Pysty tehdä asioita nopeammin kuin voisit kuvitellakkaan tekeväsi käsin ja jos osaat hommasi, voit tehdä MITÄ tahansa, mitä olet tehnyt pelissä tähän mennessä.
 
On olemassa scriptoja, jotka taistelevat yksinään gauntletissa ja raudankaivajia, jotka käyttävät kymmeniä runebookkeja avuksi taisetelevat vihollisia vastaan ja vievät pankkiin kaikki valmiiksi sulatetut harkot. Mahdollisuudet on rajattomat, juuri tämä sai minut innostumaan EUO:sta.
 
  
 +
= Building up and finishing script. Basic and advanced tehcniques =
  
=== Oppaan tarkoitus ===
+
== First words ==
  
Käsittelemme nyt ainakin aluksi perusasioita, puhumista, liikkumista, tavaroiden käyttämistä ja muita perushommia. Koitan käyttää mahdollisimman paljon selkeitä esimerkkejä ja ehkäpä keksin joitain harjoituksiakin, mistäs sen tietää vielä tässä vaiheessa.
+
=== 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.
  
=== Peli välineet ===
+
=== About author: Una ===
  
Tarvitset:
+
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.
* Tietokoneen ( Ylläri )
+
* Uusimman version EasyUO 1.5 betasta. ( HUOM: Ehdottomasti 1.5, koska jotkut komennot joita esitän, ei toimi vanhassa )
+
* Aivot
+
  
'''HUOM:'''
+
=== Target audience ===
* Oletan oppaan aikana, että sinulla on Ultima online 2D clientti päällä ja olet kirjautuneena sisään aivan normaalisti.
+
  
== Osa 1. Perusteet ==
+
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.
  
Jokainen ohjelmointi kieli tarvitsee jotain, mihin tallettaa asioita joita ohjelma sitten voi myöhemmin käsitellä. Niitä kutsutaan muuttujiksi. On tärkeää että ymmärrät eron erilaisten muuttujien välillä.
+
== Starting to write a script ==
  
=== Globaalit muuttujat % ( Global variables ) ===
+
Break your script to blocks ( in your head or paper ). Say you are building mining script, then it should look like this.
Tämä on yleisin muuttuja tyyppi, jota tulet käyttämään scriptoissasi. Näiden avulla voi tallentaa kaikenmaailman asioita, niin numeroita kuin sanoja ja jopa lauseita.
+
* 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>
 +
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.
 +
 
 +
=== 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
  
Muuttujan nimeä ei voi käyttää ihan mitä tahansa.
+
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>
 
<pre>
set %Kunnollinen
+
gosub Test
set %Sa_moin
+
set %_Ja_Taas
+
set %jne1
+
  
 +
sub Test
 +
return This_is_test
  
set %1EIole
+
display ok #result
 +
halt
 
</pre>
 
</pre>
  
Käyttö on helppoa, kuten seuraavassa esimerkissä näkyy.
+
Display now should read "This_is_test" . Great isnt it?
  
 +
==== Gosub parameters ====
 +
I show now, HOW to use these and WHY.
 
<pre>
 
<pre>
set %Muuttuja 10
+
gosub TestSub first second third
set %Toinen 5
+
gosub TestSub This Is Great
msg Muuttuja: %Muuttuja Toinen: %Toinen $
+
sub TestSub
halt
+
  Display ok %1 %2 %3
 +
return
 
</pre>
 
</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.
  
Tämä oli yksinkertainen esimerkki. Hajoitetaan se nytten osiin ja tarkastellaan näitä komentoja vähän.
+
"Bad way" (pseudo code)
[[set]] komento kertoo EasyUO:lle, että nytten toimitaan muuttujien kanssa ja muutetaan niiden arvoja.
+
<pre>
[[msg]] komento mahdollistaa puhumisen hahmollasi UO:ssa.
+
set %BackpackID ABCDEF
[[halt]] Lopettaa scriptan pyörimisen.
+
set %IDofSomething GHIJKL
 +
gosub TestSub
  
'''Tehtävä:'''
+
sub TestSub
* Kokeile mitä tapahtuu jos poistat [[halt]] komennon.
+
  Open %BackPack
* Koita muuttaa muuttujien sisältöä ja nimiä
+
  Find %IDofSomething
 
+
return
%Muuttuja ja %Toinen ovat kuten ehkä arvasit, muuttujia. Huomaa " % " merkki ennen nimeä. Se tarkoittaa että muuttuja on globaali, eli "näkyy" jokapuolella scriptaa. Voit siis kutsua ja muokata globaalia muuttujaa missä tahansa osassa scriptaa. Mutta VAIN siinä kyseisessä scriptassa, missä olet muuttujan luonut. Eli vieressä pyörivä scripta ei tiedä siitä muuttujasta mitään. Kun scripta lopetetaan nämä muuttuja lopettavat olemassa olon. Tämä kannattaa muistaa.
+
</pre>
 
+
Why is this bad? Becouse it needs those variables to be set in somewhere. Its hard to debug.
=== Pysyvät muuttujat * ( Persistent variables ) ===
+
<pre>
Näiden muuttujien erikoisuus on pysyvyys ja näkyvyys. KAIKKI Scriptat pystyvät näkemään nämä muuttujat ja nämä EIVÄT katoa kun sammutat tietokoneen. EasyUO tallentaa ne tietokoneen registeriin. Tämä on mainio keino tallentaa scriptan tiedot, jotta niitä ei tarvitse kysyä ensikerralla.
+
set %BackpackID ABCDEF
 +
set %IDofSomething GHIJKL
 +
gosub TestSub
  
 +
sub TestSub %BackpackID %IDofSomething
 +
  Open %1
 +
  Find %2
 +
return
 +
</pre>
 +
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>
 
<pre>
set *UnaTesti Toimii
+
set %BackpackID ABCDEF
halt
+
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>
  
Aja tuo pätkä EUO:lla.
+
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.
Sitten kokeile tätä.
+
 
+
 
<pre>
 
<pre>
msg *UnaTesti $
+
;==================
halt
+
; 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>
 
</pre>
  
Mitä tapahtui? Aivan oikein, se muistaa nyt, mitä pistits inne talteen. Eli toisin sanoen, se tallensi sen kovalevylle talteen.
+
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.
  
=== Systeemi muuttujat # ( System variables ) ===
+
==== Return from gosub ====
Avaa EUO ja katso oikeaan reunaan. Siellä pitäisi näkyä kaikenlaisia ihmeellisiä muuttujia. Monen nimestä ja sisällöstä voi päätellä mitä ne pitävät sisällään. Nämä systeemi muuttujat siis ovat sellaisia joita Ultima antaa meille.
+
  
Katsotaas vähän listaa:
+
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.
  
Character info
+
<pre>
* [[Var charPosX|#CharposX]] (luku)
+
gosub FindId ABC #backpackID
* [[Var charPosY|#CharposY]] (luku)
+
if #result = #false
 
+
  display ok Did not find item
Kuten arvata saattaa, niin nämä kertovat hahmon sijainnin maailmankartalla. (x,y) (0,0) on kartan vasen alakulma.
+
else
* [[Var charGhost|#CharGhost]]
+
  display ok #result
Kertoo onko hahmo haamu vai ei.
+
halt
 
+
;=================
Statusbar
+
; name: FindId
* [[Var charName|#CharName]] Sisältää hahmon nimen
+
; Author: una
* [[Var hits|#Hits]] Kertoo tämän hetkisen Energian määrän
+
; 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.
  
Jne jne.. Katsele vähän listaa jotta ymmärrät vähän, mitä kaikkea UO meille tarjoaa.
+
=== #True and #False ===
  
=== Nimiavaruus muuttujat( Namespace variables ) ===
+
Learn to use these. If you have variables, that state ON/OFF type of things, then you can use #true and #false.
Tästä aiheesta voisi kirjoittaa oman oppaan ja ne ovat vain tasokkaille koodareille. Eli ei mitään mitä alussa pitäisi tietää. Jos kuitenkin kiinnostaa, niin voit katsoa lisää aiheesta täällä ( englanniksi ) [[NameSpace]] .
+
I wrote separated quide for these and you can find it [[QG_True_False|here]]

Latest revision as of 01:42, 4 December 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. I wrote separated quide for these and you can find it here