Difference between revisions of "User:Una"

From Wiki
Jump to: navigation, search
(#True and #False)
 
(24 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Suomenkielinen EasyUO scriptaus opas ==
 
  
=== Una, oppaan kirjoittaja ===
 
  
Moro, olen käyttänyt 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. Muuttujat ==
+
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 ==
  
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ä.
+
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
=== Globaalit muuttujat % ( Global variables ) ===
+
* Use mining tool and target ground
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 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.
Muuttujan nimeä ei voi käyttää ihan mitä tahansa.
+
 
+
 
<pre>
 
<pre>
set %Kunnollinen
+
Mainloop:
set %Sa_moin
+
  gosub CheckForTools
set %_Ja_Taas
+
  gosub Dig
set %jne1
+
  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 ===
  
set %1EIole
+
* If something can be thought as separate part of script, make it to Sub
</pre>
+
* Always create subs so, that they dont use global variables, but use Gosub parameters
 +
* Use return parameter
  
Käyttö on helppoa, kuten seuraavassa esimerkissä näkyy.
+
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 %Muuttuja 10
+
gosub Test
set %Toinen 5
+
msg Muuttuja: %Muuttuja Toinen: %Toinen $
+
halt
+
</pre>
+
  
Tämä oli yksinkertainen esimerkki. Hajoitetaan se nytten osiin ja tarkastellaan näitä komentoja vähän.
+
sub Test
[[set]] komento kertoo EasyUO:lle, että nytten toimitaan muuttujien kanssa ja muutetaan niiden arvoja.
+
return This_is_test
[[msg]] komento mahdollistaa puhumisen hahmollasi UO:ssa.
+
[[halt]] Lopettaa scriptan pyörimisen.
+
  
'''Tehtävä:'''
+
display ok #result
* Kokeile mitä tapahtuu jos poistat [[halt]] komennon.
+
* Koita muuttaa muuttujien sisältöä ja nimiä
+
 
+
%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.
+
 
+
=== Pysyvät muuttujat * ( Persistent variables ) ===
+
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.
+
 
+
<pre>
+
set *UnaTesti Toimii
+
 
halt
 
halt
 
</pre>
 
</pre>
  
Aja tuo pätkä EUO:lla.
+
Display now should read "This_is_test" . Great isnt it?
Sitten kokeile tätä.
+
  
 +
==== Gosub parameters ====
 +
I show now, HOW to use these and WHY.
 
<pre>
 
<pre>
msg *UnaTesti $
+
gosub TestSub first second third
halt
+
gosub TestSub This Is Great
 +
sub TestSub
 +
  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.
  
Mitä tapahtui? Aivan oikein, se muistaa nyt, mitä pistits inne talteen. Eli toisin sanoen, se tallensi sen kovalevylle talteen.
+
"Bad way" (pseudo code)
 +
<pre>
 +
set %BackpackID ABCDEF
 +
set %IDofSomething GHIJKL
 +
gosub TestSub
  
=== Systeemi muuttujat # ( System variables ) ===
+
sub TestSub
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.
+
  Open %BackPack
 +
  Find %IDofSomething
 +
return
 +
</pre>
 +
Why is this bad? Becouse it needs those variables to be set in somewhere. Its hard to debug.
 +
<pre>
 +
set %BackpackID ABCDEF
 +
set %IDofSomething GHIJKL
 +
gosub TestSub
  
Katsotaas vähän listaa:
+
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>
 +
set %BackpackID ABCDEF
 +
set %IDofSomething GHIJKL
 +
gosub TestSub
  
Character info
+
;=================
* [[Var charPosX|#CharposX]] (luku)
+
; name: TestSub
* [[Var charPosY|#CharposY]] (luku)
+
; Author: Una
 
+
; Purpose: To open backpack and findstuff
Kuten arvata saattaa, niin nämä kertovat hahmon sijainnin maailmankartalla. (x,y) (0,0) on kartan vasen alakulma.
+
; %1 = Backpack ID
* [[Var charGhost|#CharGhost]]
+
; %2 = What to look for
Kertoo onko hahmo haamu vai ei.
+
sub TestSub %BackpackID %IDofSomething
 
+
  Open %1
Statusbar
+
  Find %2
* [[Var charName|#CharName]] Sisältää hahmon nimen
+
return
* [[Var hits|#Hits]] Kertoo tämän hetkisen Energian määrän
+
</pre>
 
+
Jne jne.. Katsele vähän listaa jotta ymmärrät vähän, mitä kaikkea UO meille tarjoaa.
+
  
Jos haluaisit vaikka saada nykyiset energia talteen, muuttujaan siis, niin se käy näin.
+
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>
 
<pre>
set %Energiat #Hits
+
;==================
msg HP: %Energiat TAI: #Hits Mana: #Mana $
+
; 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>
  
=== Nimiavaruus muuttujat( Namespace variables ) ===
+
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.
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_local|NameSpace]] .
+
  
 +
==== Return from gosub ====
  
== Osa 2. Perusteet ==
+
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.
Koitetanpas saada aikaiseksi jotain "siistiä".
+
 
+
=== Liikkuminen ===
+
Liikkuminen on erittäin helppoa ja monesti erittäin tarpeellista, joten tarkastellaampa vähän miten se tapahtuu.
+
  
 
<pre>
 
<pre>
set %PaikkaX #CharPosX + 1
+
gosub FindId ABC #backpackID
set %PaikkaY #CharPosY + 1
+
if #result = #false
 
+
  display ok Did not find item
move %PaikkaX %PaikkaY 0 5s
+
else
 +
  display ok #result
 
halt
 
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 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.
  
Ajappa tuo ohlema EUO:ssa ja katso mitä se tekee. Sen ainakin pitäisi kävellä johonkin suuntaan askeleen verran.
+
=== #True and #False ===
Koodia tarkastelemalla, helposti ymmärtää mitä siinä tapahtuu. Aluksi luodaan kaksi muuttujaa ja sijoitetaan niihin tiedot ukon sijainnista JA lisätään siihen 1.
+
Sen jälkeen käytämme komentoa [[move]] ja annamme sille koordinaatit ja muut tiedot ja sitten se yrittää päästä siihen annettuun pisteeseen.
+
[[move]] komento tarvitsee tietoa pikkaisen, kuten esimerkissä näkyy.
+
move {X} {Y} [toleranssi] [yritysaika]
+
 
+
{X} = X piste johon yritetään liikkua
+
{Y} = Y piste johon yritetään liikkua
+
[Toleranssi] = 0 tarkoittaa, että yritetään TÄSMÄLLEEN siihen pisteeseen, mikä annettiin. Joten 5 tarkoittaa että riittää kunhan ollaan 5 askeleen päässä pisteestä.
+
[yritysaika] = Kuinka kauan yritetään päästä kyseiseen pisteeseen
+
  
'''Tehtävä:'''
+
Learn to use these. If you have variables, that state ON/OFF type of things, then you can use #true and #false.
* Katso Ukon sijainti EUO:n systeemi muuttujista, kävele kauemmaksi ja koita päästä scriptan avulla takaisin.
+
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