Run-time error in custom script

Posted by Xandler on Thu 11 Jan 2018 09:22 PM — 14 posts, 47,917 views.

#0
I currently have a script set up to pick up a certain item and count it so I can keep track of how many of these said items I have, the problem I have encountered is I'm getting a run-time error when the script runs (it counts the item and everything). The following is the error I receive:

Run-time error
Plugin: dragonball_checker (called from world: Dragonball Evolution)
Immediate execution
[string "Trigger: "]:4: attempt to compare number with string
stack traceback:
[string "Trigger: "]:4: in main chunk

The coding I have is as follows:

pkdball1 = tonumber (GetVariable ("pkdball1")) or 0
pkdball1 = pkdball1 + 1
SetVariable ("pkdball1", pkdball1)
if GetVariable("pkdball1") >= 1 then
if GetVariable("pkdball2") >= 1 then
if GetVariable("pkdball3") >= 1 then
if GetVariable("pkdball4") >= 1 then
if GetVariable("pkdball5") >= 1 then
if GetVariable("pkdball6") >= 1 then
if GetVariable("pkdball7") >= 1 then
Send ("ewish agility")
end
end
end
end
end
end
end

Just wondering how I can fix this issue. The ifchecks are for when I gather all the items and complete a "wish" for that option.
Australia Forum Administrator #1
GetVariable returns a string, so you need to convert them back into numbers:


if tonumber (GetVariable("pkdball1")) >= 1 then
if tonumber (GetVariable("pkdball2")) >= 1 then
if tonumber (GetVariable("pkdball3")) >= 1 then

...


And if you really want send "ewish agility" if they are all greater than or equal to one, use "and" rather than a lot of deeply-nested ifs:


if tonumber (GetVariable("pkdball1")) >= 1 and
   tonumber (GetVariable("pkdball2")) >= 1 and
   tonumber (GetVariable("pkdball3")) >= 1 and
   tonumber (GetVariable("pkdball4")) >= 1 and
   tonumber (GetVariable("pkdball5")) >= 1 and
   tonumber (GetVariable("pkdball6")) >= 1 and
   tonumber (GetVariable("pkdball7")) >= 1 then
     Send ("ewish agility")
end
#2
Perfect! Thank you!
#3
Having a bit of trouble with this part of the script working:


  <trigger
   enabled="y"
   expand_variables="y"
   group="dragonballs"
   match="^\[Quest\]\: <Your Character here> has made a wish with the Dragonballs\!$"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>pkdball1 = tonumber (GetVariable ("pkdball1")) or 0
pkdball1 = pkdball1 - 1
SetVariable ("pkdball1", pkdball1)
pkdball2 = tonumber (GetVariable ("pkdball2")) or 0
pkdball2 = pkdball2 - 1
SetVariable ("pkdball2", pkdball2)
pkdball3 = tonumber (GetVariable ("pkdball3")) or 0
pkdball3 = pkdball3 - 1
SetVariable ("pkdball3", pkdball3)
pkdball4 = tonumber (GetVariable ("pkdball4")) or 0
pkdball4 = pkdball4 - 1
SetVariable ("pkdball4", pkdball4)
pkdball5 = tonumber (GetVariable ("pkdball5")) or 0
pkdball5 = pkdball5 - 1
SetVariable ("pkdball5", pkdball5)
pkdball6 = tonumber (GetVariable ("pkdball6")) or 0
pkdball6 = pkdball6 - 1
SetVariable ("pkdball6", pkdball6)
pkdball7 = tonumber (GetVariable ("pkdball7")) or 0
pkdball7 = pkdball7 - 1
SetVariable ("pkdball7", pkdball7)</send>


Basically inside the brackets will be the character name and when that message comes up it SHOULD take one number off EVERY item required for the wish, but for some reason its not quite working, all the variables (pkdball1-7) remain unchanged (until I have to manually reset it). I may just have it in wrong (I've been testing every now and again), but getting all 7 is kinda rare so I haven't been able to specifically test this portion that thoroughly.
Amended on Sun 14 Jan 2018 04:14 AM by Nick Gammon
Australia Forum Administrator #4
Is it actually firing? Try putting a print into the script and seeing if that appears (or change the colour of the matching line to yellow or something).

When I tested it all of those variables were set (and became -1). When I tested it again they all became -2, so in principle it is working.
#5
Well, like I said, I have been playing around with it (each time I get all 7 items and do a wish), and if it doesn't work like its supposed to, I tinker with it a little. (Is there a way to force it to fire, so I can see if it works?)
Australia Forum Administrator #6
Yep, go to the Game menu -> Test Trigger (Shift + Ctrl + F12).

Then enter the line you are supposed to see. Put a newline before and after it (use Ctrl+Enter to do that).
#7
Awesome! Learning new things every day with mushclient. On a sidenote, I managed to get the last item I needed for the wish and it worked like its supposed to!
#8
Working on adding other things to it, I want to add

|>Intellect: *

but it shares a line with something else, but I want to cut it off after that asterik...I know its something simple but I can't seem to figure it out. I thought the $ symbol ended the line so it wouldn't copy the rest of the line, therefore:

|>Intellect: *$

but that doesn't seem to work :(
Australia Forum Administrator #9
Remember that asterisk is a "magic" character. If you want to go up to an asterisk put a backslash before it.
#10
Hrm....I cant get it to work right, its either showing the entire line, or nothing.


<triggers>
  <trigger
   enabled="y"
   expand_variables="y"
   match="|>Intellect:           \*"
   send_to="9"
   sequence="100"
   variable="CharInt"
  >
  <send>%1</send>
  </trigger>
</triggers>



That version leaves the variable blank, if I take out the "\" it reproduces the entire line.
Amended on Tue 16 Jan 2018 03:55 AM by Nick Gammon
Australia Forum Administrator #11
That's not a regexp, so you don't need the backslash. Can you show what you are trying to match on?

Does the asterisk represent the intelligence? And there is other stuff after it? Try using a script then you can choose what goes into the variable, like this:


<triggers>
  <trigger
   enabled="y"
   match="|&gt;Intellect:           * *"
   send_to="12"
   sequence="100"
   variable="CharInt"
  >
  <send>

SetVariable ("CharInt", "%1")

</send>
  </trigger>
</triggers>


Template:pasting
For advice on how to copy the above, and paste it into MUSHclient, please see Pasting XML.


That sets the variable CharInt to the first wildcard, and ignores the other stuff.
#12
|>Intellect: 100 Rage: 100

Just trying to match the Intellect, and yes there's more after it, and yes, the wildcard is the number, but what you had posted works just fine, not sure if there's a better workaround for that or not.
Australia Forum Administrator #13
Since you know the line looks like that it is clearer to match on it:


|>Intellect:           *      Rage:             *


Now, you don't actually need to use the second wildcard. Also you may have an issue with spaces. Since you have a fixed number of spaces the matching will be out with an intellect of 3 digits compared to 1 digit. A regular expression would be better:


^\|\>Intellect:\s+(\d+)\s+Rage:\s+(\d+)$


(Check the "regular expression" box)

The \s+ stuff matches one or more spaces, so the exact number isn't critical any more. Also the \d+ matches one or more digits so it wouldn't match on (say) letters.

See here for more details: http://www.gammon.com.au/regexp