Author Topic: Tutorial 3 - Your First Script  (Read 14990 times)

0 Members and 1 Guest are viewing this topic.

Offline CervezaTopic starter

  • Hacksimus Maximus
  • Scripthack
  • *
  • Posts: 5857
  • Activity:
    0%
  • Reputation Power: 80
  • Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!
  • Gender: Male
  • So... Hows that Hopey-Changey thing working out?
  • Respect: +403
  • Referrals: 11
    • View Profile
Tutorial 3 - Your First Script
« on: February 24, 2009, 09:24:10 AM »
0
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: [Select]
;=================================================================
; 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

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: [Select]
if C in #charStatus#charStatus checks to see what status the char is in. C is for poisoned.

Code: [Select]
findItem NUF C_ , #backpackIDfindItem will look for NUF (cure potion) in a container (C_ , ) with the ID of backpack (#backpckID)

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

Code: [Select]
halthalt 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: [Select]
set #lobjectID #findIDset 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: [Select]
set #ltargetKind 1This 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: [Select]
event macro 17 0event 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: [Select]
wait 5wait 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.
« Last Edit: August 03, 2010, 12:49:41 PM by Cerveza »
XXXXXXXXXX________________________________________] 20%
I've forgotten more about this game then most people will ever know.
Thank you for controlling your children. Their manners reflect your love for them.
Give a man a fish and you feed him for a day. Don't teach a man to fish, and you feed yourself. He's a grown man. Fishing's not that hard.

Offline OMGBurgers

  • Hero Member
  • *
  • Posts: 800
  • Activity:
    0%
  • Reputation Power: 7
  • OMGBurgers has no influence.
  • Respect: +44
  • Referrals: 0
    • View Profile
Re: Your First Script
« Reply #1 on: February 24, 2009, 09:56:33 AM »
0
Hope you don't mind me replying but just wanted make a note how important these are imo.

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.

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.

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

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.

Offline Goliath

  • Sr. Member
  • *
  • Posts: 424
  • Activity:
    0%
  • Reputation Power: 5
  • Goliath has no influence.
  • Gender: Male
  • Respect: +37
  • Referrals: 2
    • View Profile
Re: Tutorial 3 - Your First Script
« Reply #2 on: August 19, 2011, 08:58:08 AM »
0
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?

Offline Endless Night

  • Global Moderator
  • *
  • *
  • Posts: 5467
  • Activity:
    0%
  • Reputation Power: 62
  • Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!
  • Respect: +393
  • Referrals: 1
    • View Profile
Re: Tutorial 3 - Your First Script
« Reply #3 on: August 19, 2011, 08:59:24 AM »
0
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?

http://wiki.easyuo.com/index.php?title=Documentation
Outlaw Josey Wales - "Manwink, A Long Gone Scripty, and Endless are always teasing us with their private sections lol. What there realy saying is scripters rule and users drool."
Briza - "Your a living breathing vortex of usefulness."

Offline CervezaTopic starter

  • Hacksimus Maximus
  • Scripthack
  • *
  • Posts: 5857
  • Activity:
    0%
  • Reputation Power: 80
  • Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!
  • Gender: Male
  • So... Hows that Hopey-Changey thing working out?
  • Respect: +403
  • Referrals: 11
    • View Profile
Re: Tutorial 3 - Your First Script
« Reply #4 on: September 20, 2011, 06:32:41 AM »
0
stickied
XXXXXXXXXX________________________________________] 20%
I've forgotten more about this game then most people will ever know.
Thank you for controlling your children. Their manners reflect your love for them.
Give a man a fish and you feed him for a day. Don't teach a man to fish, and you feed yourself. He's a grown man. Fishing's not that hard.

Tags: