Official ScriptUO EasyUO Scripts > Scripting Tutorials

Tutorial 3 - Your First Script

(1/1)

Cerveza:
This will be simple enough, but give you an idea of how to find and use items, and how to use them when a condition exists.

Here's how I go about writing scripts.

1 - What do I want to do
I list out the general overview of what I want to do. Just write it out.

I want to drink a cure potion whenever I get poisoned.

2 - Start to define what needs to happen, being a little more specific then the overview.

I want to monitor my status
If I get poisoned
Look for a cure potion
if it's found - drink it
if it's not found - stop the script

3 - Gather any information you might need.

Cure potion item type = NUF

4 - Start writing

--- Code: ---;=================================================================
; Script Name: Cerveza's Cure Pot Drinker
; Author: Cerveza
; Version: 1.0
; Shard OSI / FS: OSI / FS OK
; Revision Date: 2/24/2009
; Purpose: Drink Cure Potion when poisoned
; Globals: None
;=================================================================

SUO:
repeat

if C in #charStatus
{
  findItem NUF C_ , #backpackID
    if #findKind = -1
      halt
  set #lobjectID #findID
  set #ltargetKind 1
  event macro 17 0
  wait 5
}

until #CharGhost = YES
while #CharGhost = YES
  wait 0
GoTo SUO

--- End code ---

Now lets look at whats actually happening in this script.

If you read the Basic Script Flow then you recognize the main loop "wrapper". If not then you really should read that before continuing here.

Line by line:

--- Code: ---if C in #charStatus
--- End code ---
#charStatus checks to see what status the char is in. C is for poisoned.


--- Code: ---findItem NUF C_ , #backpackID
--- End code ---
findItem will look for NUF (cure potion) in a container (C_ , ) with the ID of backpack (#backpckID)


--- Code: ---if #findKind = -1
--- End code ---
if checks a condition of the last item found, which is NUF for cure potions, the attribute -1 indicates NO items were found.


--- Code: ---halt
--- End code ---
halt stops the script. Since this line immediately follows the if line, it's only executed when the if line is a true statement. So if you don't have any cure potions in your backpack (searched during the IF line) then this line is executed and the script stops. If a cure potion is found, the script skips this line and continues.


--- Code: ---set #lobjectID #findID
--- End code ---
set allows you to "push" specific information or create storage variables with information. In this case we want to make #lobjectID the ID of the cure potion we found earlier. When we found it, it stored in #findID


--- Code: ---set #ltargetKind 1
--- End code ---
This will set the #ltargetKind to an object. THIS LINE IS VERY IMPORTANT. When you are telling the script how to deal with items, this HAS to be set. It's common to miss this and have scripts act bonkey.


--- Code: ---event macro 17 0
--- End code ---
event macro is a way of performing in game macros. In this case 17 0 means USE LAST ITEM, which we already set to the cure potion we found.


--- Code: ---wait 5
--- End code ---
wait will wait for 1/4 second before continuing. Another area that can cause scripts to fail is not putting waits, or putting wrong waits in scripts. Some things MUST have a wait or they will fail (dragging and dropping for example) and others MUST have a wait or they will cycle around too quickly and try to do the action twice (like this script, you drink a cure and before you are cured the script sees you poisoned and tries to drink another cure before the first one finishes it's action). Thats why we put in waits. As a general rule, make your waits bigger then you think you'll need. You can always trim them down to speed up the script, but troubleshooting a 1000 line script that's flakey because of waits that aren't long enough will make you quickly hate life.

So there you have it.

Check to see if your poisoned
If you get poisoned - Look for a cure potion
If you don't find one - QUIT
Set the Cure Potion as Last Item (because to get here you MUST have found one)
Set the Last Item type to Object
Use Last Item
Wait a bit to make sure you don't drink 2

There's your Drink Cure Pot script.

OMGBurgers:
Hope you don't mind me replying but just wanted make a note how important these are imo.


--- Quote from: Cerveza on February 24, 2009, 09:24:10 AM ---1 - What do I want to do
I list out the general overview of what I want to do. Just write it out.

I want to drink a cure potion whenever I get poisoned.

--- End quote ---

Most important part of a script in my opinion.  It's easy to have an idea in your head, but without that idea to "look at" it makes it harder to complete.  (Mainly because you forget parts of that idea, or the order/proccess in which it should be carried out which can cause headaches).  I usually do the same thing, write it out.  I'm trying to get to a point where I leave my comments in because it makes it easier for the script writer AND the users to debug and give good feedback to overcome issues.

Always a good idea to write it out, especially in a long script.  "Storyboard" your scripts, it really helps.


--- Quote from: Cerveza on February 24, 2009, 09:24:10 AM ---2 - Start to define what needs to happen, being a little more specific then the overview.
--- End quote ---

Exactly what I do.  I always do the essentials frist.  For something like my crafter I start with the "restock materials" subs, then I just test it in the loop even though it's really doing nothing but it did restock correctly.  From there I move on to my "took check" subs.  The process keeps going on covering the vital subs first that are needed to carry out the subs/actions afterwards.  The end result is your fully functioned script working almost flawlessly.  You know the flow is probably correct in the order of things to be done, from there you release/test.  When people have an issue with your script it could be very well pinpointed to a specific sub by the description of their problem as long as you were decently detailed, defined and organized with your subs/structure.

Goliath:
First let me say I am loving the method in which you write your tutorials.  The way you break things down is superb:)

Here is my question and maybe I just haven't there yet or have over look a step:

As I read through the tutorials you make reference to certain things such as...
    C in #charStatus
    event macro 17 0

You break down that the C if for a character status of being poisoned and that event macro 17 0 is for "use last item".
Where do I find these lists of things you call.  I understand the concept of libraries but as I read I would like to be able to see for myself the references and where they are coming from.

I hope I am not off base here?

Endless Night:

--- Quote from: Goliath on August 19, 2011, 08:58:08 AM ---First let me say I am loving the method in which you write your tutorials.  The way you break things down is superb:)

Here is my question and maybe I just haven't there yet or have over look a step:

As I read through the tutorials you make reference to certain things such as...
    C in #charStatus
    event macro 17 0

You break down that the C if for a character status of being poisoned and that event macro 17 0 is for "use last item".
Where do I find these lists of things you call.  I understand the concept of libraries but as I read I would like to be able to see for myself the references and where they are coming from.

I hope I am not off base here?

--- End quote ---

http://wiki.easyuo.com/index.php?title=Documentation

Cerveza:
stickied

Navigation

[0] Message Index

Go to full version