Author Topic: Tutorial 5 - Combining the Potion Drinker and Bandage Self  (Read 14224 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 5 - Combining the Potion Drinker and Bandage Self
« on: February 25, 2009, 05:56:44 AM »
0
If you've read the first 4 tutorials, you've seen how to set up a script flow, how to use items, and have a beginning understanding of how to set triggers (if statements) and timers in a script. You've also seen how to use a sub routine and learned a few basic rules of scripting (NEVER goto out of a sub, always end subs with return, etc...).

Now that we have a script to drink Cures and Heals (you should be able to add in Total Refresh pots on your own at this point) and we have a script to use a bandage on ourselves when we hit a hotkey, lets combine the two together.

As always, there's some stuff to learn first thats going to help us here, and REALLY help us later when we're writing the big scripts.

Since you have already seen each script working, and how it works, you're thinking "putting these together will be simple" and you're right. But since the goal is to learn to script better along the way... lets learn a few things right here.

First off is a thing called NAMESPACE. TrailMyx has an awesome write up on NameSpace and you should take a minute to read it before continuing here.

Hopefully you start to understand that using ! variables in a namespace is a great way of sharing the variable between scripts. In our case here, we want to start getting in the habit of using a LOCAL namespace in ALL of our sub routines. Even if we don't use any variables in the sub routine, it's a great habit to get into. Trust me here - it will save you headaches in later scripting.

Here's our DrinkIt sub routine to drink a potion:
Code: [Select]
sub DrinkIt
  wait 5
  findItem %1 C_ , #backpackID
  if #findKind = -1
    return
  set #lobjectID #findID
  set #ltargetKind 1
  event macro 17 0
  wait 5
return

Lets go ahead and namespace this out. Remember to do this with ALL of your sub routines. It's a really good scripting practice and like I said, saves you aspirin in the long run.
Code: [Select]
sub DrinkIt
  namespace push
  namespace local DIT ; short for DrinkIT
  wait 5
  findItem %1 C_ , #backpackID
  if #findKind = -1
    return
  set #lobjectID #findID
  set #ltargetKind 1
  event macro 17 0
  namespace clear
  namespace pop
  wait 5
return

There, we namespaced our DrinkIT sub routine. Only 4 extra lines of code and much cleaner scripting.

You know... this looks very familiar to our bandage sub routine  ??? , I know we're all into saving lines of code... I wonder if there's a way we could combine our Drinking and Bandaging SUB routines into one SUPER SUB?!

First lets look at the difference between them. Really the only thing is that when we use the bandages, we actually have to target ourselves. I think we can send some arguments with the gosub line to let the sub know if it's a bandage or a potion to be used.

Remember, the "gosub" statement? I talked about it in an earlier tutorial....

gosub {Sub Name} {%1} {%2} {%3} ; where %1,%2,%3 are arguments sent to the sub routine.

Here's how we're going to do this..... we'll make the sub routine look for new information. Lets call it a target. So our new drink pots gosub statement will look like this:

gosub DrinkIt NUF NONE ; %1 = NUF and %2 = NONE

See how we can send those along to the sub routine? "But why are you doing this" asks squeeky. Quiet squeeky.... We have to change our sub routine a bit now, so it will actually use the %2.

Code: [Select]
sub DrinkIt
  namespace push
  namespace local DIT ; short for DrinkIT
  wait 5
  findItem %1 C_ , #backpackID
  if #findKind = -1
    return
  set #lobjectID #findID
  set #ltargetKind 1
  event macro 17 0
  if %2 = SELF
; Note that if this sub sees the word SELF in %2, then it WILL execute
; the following line, or in this case, the lines inside the {}
; If ANY other word is in the %2 argument we sent to this sub
; the sub will skip over the {} section
  {
    target 3s
    event macro 23 0
  }
  namespace clear
  namespace pop
  wait 5
return

OMG you all shout! I GET IT!! Except for squeeky  >:(

Ok squeeky... see we set a new variable to be passed to our DrinkIt sub routine. It's %2. Remember that %1 is the item TYPE that we want to find and use, and %2 is the target... if we're drinking a potion we want %2 to be NONE (actually the word NONE) and if we want to target ourselves (like using a bandage) then we would use SELF. You'll see it in the completed script:

Code: [Select]
;=================================================================
; Script Name: Cerveza's Cure And Heal Pot Drinker w/Hotkey Bandages
; Author: Cerveza
; Version: 1.0
; Shard OSI / FS: OSI / FS OK
; Revision Date: 2/24/2009
; Purpose: Drink Cure/Heal Potions when needed, Applies bandages on hotkey
; Globals: None
;=================================================================

;************************ Setup **********************************
set %healpotHP ( #maxHits - 40 ) ; change this for how many points down to drink heal
set %timer_pot_heal #sCnt
set %bandage_hotkey F10 ; you can change this to whatever you want your bandage hotkey

;*********************** MainLoop ********************************
SUO:
repeat
  
  if C in #charStatus
    gosub DoIt NUF NONE ; %1=NUF %2=NONE
  
  if #hits < %healpotHP && #sCnt > %timer_pot_heal
  {
    gosub DoIt UUF NONE ; %1=UUF %2=NONE
    set %timer_pot_heal ( #sCnt + 10 )
  }
  wait 5

  onhotkey %bandage_hotkey
    gosub DoIt ZJF SELF ; %1=ZJF %2=SELF
  
until #CharGhost = YES
while #CharGhost = YES
  wait 0
GoTo SUO

;************************ Subs ***********************************
; %1 - Item TYPE to use. NUF=Cure Pot, UUF=Heal Pot, ZJF=Bandage
; %2 - Target. SELF to target self, all other will skip targetting
sub DoIt
  namespace push
  namespace local DIT ; short for DrinkIT
  wait 5
  findItem %1 C_ , #backpackID
  if #findKind = -1
    return
  set #lobjectID #findID
  set #ltargetKind 1
  event macro 17 0
  if %2 = SELF
  {
    target 3s
    event macro 23 0
  }
  namespace clear
  namespace pop
return

I snuck in a little variable for the setup. You should be able to spot it. Just a little hint for later scripts. Always good to keep all the user setup stuff in variables and not hard coded. That way noobs can easily see where they need to make changes to adapt the script over to their use.

I also snuck in some stuff into the sub routine. If you write a sub it's good practice to include a description of the %1, %2 etc.. variables used in the sub.

And I renamed the sub. Since it can do more then drink it needed a new name.

Your homework, if you haven't done so already, is to add in Total Refresh  ;D

Hints: you need to set a variable to tell the script when to drink one.
there's no timer on TR's
TR pots are item type ZUF
« Last Edit: August 03, 2010, 12:49:24 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 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 5 - Combining the Potion Drinker and Bandage Self
« Reply #1 on: September 20, 2011, 06:32:06 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: