help with scripting

Posted by Caeser on Tue 09 Aug 2005 12:04 AM — 13 posts, 52,648 views.

#0
Hello. I play the mud called Achaea and I am trying to make a trigger that runs a script for it.
The trigger is supposed to make on "You bleed * health."
It then sends the wildcard into the variable "bleeding"
I want a script after that to check if the variable bleeding is greater than or equal to 100, and if so it should use CLOT. Now, I don't know how to do different testing signs like that, so a brief tutorial on those types of test conditions would be great valued.
#1
no body knows how to make an if check if a variable is "greater then or equal to" a number? or less then for that matter?
#2
Have you looked at the Lua documentation? This is really a very, very simple thing you're asking, which makes me believe that you haven't looked for an answer yet.

http://www.lua.org
http://www.lua.org/pil (Programming in Lua, which I prefer)
#3
I tried that already, but it doesn't work. I want it to be >=, but apparently I have it wrong since any number it recieves it performs CLOT. Here is my full script (in a trigger, set to Send to Script... not reg expression):

SetVariable ("bleeding", "%1")

local bleeding

bleeding = GetVariable("bleeding")

if bleeding >= "100" then
Send ("CLOT")
end
USA #4
You don't want to compare it to a string. You have to first convert it to a number, and then compare it.

See, e.g.:
local bleeding = "500"

if tonumber(bleeding) >= 100 then
    print("woot");
else
    print(":(");
end
#5
thanks, that worked perfectly :)
#6
I'm sorry. I was assuming that you did the comparison directly in the trigger and that you didn't convert it into a string and save it in a variable. I don't see the need to save it, since you're just going to clot from this trigger and never use that bleeding value again, right?

You bleed * health.

if %1 >= 100 then
Send("clot")
end

No quotes around the %1 allows Lua to implicitly convert the value into a number when MUSHclient expands the value, basically. Should work, but I wouldn't bet my life on it.
USA #7
I thought it would coerce it to a number, it is supposed to after all, but look at this:
local bleeding = "500"

if bleeding >= 100 then
    print("woot");
else
    print(":(");
end

Ran it:
$ lua test.lua 
C:\cygwin\usr\local\bin\lua.exe: test.lua:4: attempt to compare number with string
stack traceback:
        test.lua:4: in main chunk
        [C]: ?
However...
local bleeding = "500" + 0
 
if bleeding >= 100 then 
    print("woot");
else
    print(":(");
end
Running it:
$ lua test.lua 
woot


So, I guess that it coerces for arithmetic but not for comparison.
USA #8
Comparisons first check the types (if different, returns false) then goes on to the value.

However, even if he doesnt want to save it for later:
SetVariable ("bleeding", %1)

local bleeding

bleeding = %1

if bleeding >= 100 then
Send ("CLOT")
end

will work just fine. (stores bleeding as a number, and sets the variable, for later use. No need to set it, only to get it again two lines later.

Of course, if you don't need to save it, then you might as well get rid of the setvariable as well. (and if you dont need your local variable for other things, then do what Larkin suggested, and just use %1 straight in the comparison.

The %1 %2 etc comparisons are basically text-replaces, it happens before the script sees it, so you can get creative and even include functions or whole lines of scripts, or whatever else.
#9
Right. That's why I suggested not saving it with SetVariable, because the GetVariable will return it as a string, no matter how you think you stored it. Doing the comparison directly or using a Lua local variable will type it as a number.
#10
Hey, im trying to do a comparison in lua, im running it in a game though with something to interpret new commands that have been put in; the suggested comparison method doesnt work for me


1       t = 0
2	c = 1
3	while turtle.detectUp() do
4	turtle.placeDown()
5	turtle.forward()
6	t = t+1
7	if turtle.GetItemCount(tonumber(c)) == 0 then
8		c = c+1
9		end
10	end
11	print(""..t.." tracks laid.")


line 7 where it says "turtle.GetItemCount(tonumber(slot)) == 0 then"
im trying to compare the c variable obviously, but it returns
"Attempted to call nil"
Any ideas?

Thanks
USA Global Moderator #11
Venhip said:

Hey, im trying to do a comparison in lua, im running it in a game though with something to interpret new commands that have been put in; the suggested comparison method doesnt work for me


1       t = 0
2	c = 1
3	while turtle.detectUp() do
4	turtle.placeDown()
5	turtle.forward()
6	t = t+1
7	if turtle.GetItemCount(tonumber(c)) == 0 then
8		c = c+1
9		end
10	end
11	print(""..t.." tracks laid.")


line 7 where it says "turtle.GetItemCount(tonumber(slot)) == 0 then"
im trying to compare the c variable obviously, but it returns
"Attempted to call nil"
Any ideas?

Thanks


c is already a number so the call to tonumber() does nothing useful.

To know what caused the error we probably need to know what turtle.GetItemCount() does. Either you set tonumber to nil somewhere (silly) or turtle.GetItemCount doesn't exist. Or something inside turtle.GetItemCount doesn't exist. Or maybe something else doesn't exist. We don't know because you haven't shown enough of the error message or enough of the code for us to reach any conclusions.
Amended on Thu 10 May 2012 12:06 AM by Fiendish
USA #12
Caeser said:

Hello. I play the mud called Achaea and I am trying to make a trigger that runs a script for it.
The trigger is supposed to make on "You bleed * health."
It then sends the wildcard into the variable "bleeding"
I want a script after that to check if the variable bleeding is greater than or equal to 100, and if so it should use CLOT. Now, I don't know how to do different testing signs like that, so a brief tutorial on those types of test conditions would be great valued.




if %1 >= 100 then

    Send ("clot")

end


it's kinda that simple. And why aren't you using regex?

^You bleed (\d+) health.$

..would guarantee that it's a number and keep people from illusioning you a bleed symptom.