New to Lua

Posted by BlueEyes on Mon 13 Apr 2009 08:48 PM — 17 posts, 70,584 views.

#0
I'm new to Lua and trying to figure it all out. I can code easily on Nexus, but its power is hardly anything. So here is my starting question. I'm trying to get it to copy my prompt and throw up variables of my current health and etc. I can make this work through using the SetVariable command in the send box on the trigger, but I'm trying to figure out how to make the trigger activate the scripting for it, which I can do. Now when I put the SetVariable ("current_h", "%1") in the scripting it just changes my variable to %1 how woud I go about fixing this.

Second question is how would you go about noticing variables in scripting? Would you put them in qoutes or what.
Example:
if "current_h" < "max_h" then
drink health
Australia Forum Administrator #1
For the question about %1, can you paste the trigger please?

http://mushclient.com/copying

As for the compare, no I would not quote them, that is comparing two literal strings. Leave the quotes out.
#2
The main thing is...
4788h, 4841m, 4395e, 10p, 21330en, 22695w esSilrx-

What I have put in place is...
^(\d+)h, (\d+)m\, (\d+)e\, (\d+)p\, (\d+)en\, (\d+)w\ (.*?)$

Now with that trigger I can do
SetVariable ("current_h" "%1")
SetVariable ("current_m" "%2")
SetVariable ("current_e" "%3")
etc...

But instead of that I want it to use scripting since it is faster. But I don't know how to go about doing that.
Australia Forum Administrator #3
That *is* scripting. SetVariable is a script function.

However if you want to use a script file, see http://mushclient.com/scripting
#4
But when I do the SetVariable %1 command in the scripting it changes my variabe to %1 rather than the actual number.
USA #5
I think I know what you mean. You want to write a function in your scripting file, and have the trigger send to that instead? You put the function name in the Script box, which I assume you did, and the function itself should look like this in the script file:


function PromptFire (name, line, matches, styles)
  -- your SetVariable stuff goes here
end


The 'line' parameter is the same as %0, and 'matches' contains the rest of the wildcards. You'd do this to put the first wildcard (%1) in current_h:


function PromptFire (name, line, matches, styles)
  SetVariable("current_h", matches[1])
end



Edited by Nick to add a comma after "name" in the argument list.
Amended on Mon 13 Apr 2009 11:46 PM by Nick Gammon
#6
Awesome that seems to work, here's what it looks like. Can you tell me if you see anything odd or intrigueing/stupid).

<triggers>
<trigger
custom_colour="14"
enabled="y"
match="^(\d+)h, (\d+)m\, (\d+)e\, (\d+)p\, (\d+)en\, (\d+)w\ (.*?)$"
regexp="y"
script="prompt"
sequence="100"
>
</trigger>
</triggers>


and the scripting

function prompt (name, line, matches, styles)
SetVariable ("current_h", "%1")
SetVariable ("current_m", "%2")
SetVariable ("current_e", "%3")

end -- function

function prompt (name, line, matches, styles)
SetVariable ("current_h", matches [1])
SetVariable ("current_m", matches [2])
SetVariable ("current_e", matches [3])

end -- function


I'm guessing I can just put those all together, but it seems to set it and everything like I want.

Now the next question is how about would I go about doing math equations. What I want it to do is I type something along the lines of


h 75

and it changes when my health gets below the 75% mark. Or h 50 and it changes to half.

Thank you so much for the help.
Amended on Tue 14 Apr 2009 01:29 AM by BlueEyes
USA #7
You don't need to define the function twice. In fact, the first one won't work, because you're trying to assign %1, exactly what you -don't- want in the script file. It only works because you're creating a new function right after that with the same name, that DOES do the right thing, so the first one never gets used.

As for the second question, I assume you want to replace your prompt with "h:50%, m:38%, e:99%, p:64%, en:10%, w:83%"? It's a little more in-depth. You'd need to match on the prompt and catch the health values - you're doing this already - but set the Omit From Output checkbox. Or if you want to exit the <trigger> tag directly, add omit_from_output="y" into that list of options.

Then you'd need to have stored your maximum values somewhere. The info on the prompt only tells you how much you -have-, not how much you could have maximum. And we need that to produce a percentage. For now, you could do that yourself by setting variables called "max_h", "max_m", etc. in MUSHclient, and then referring to those from the script.

Here's an example, assuming you've set those max values:


function prompt (name, line, matches, styles)
  SetVariable ("current_h", matches [1])
  SetVariable ("current_m", matches [2])
  SetVariable ("current_e", matches [3])

  max = {} -- create an empty table
  max.h = GetVariable("max_h")
  max.m = GetVariable("max_m")
  -- et cetera

  percents = {} -- create an empty table

  -- calculate the percentage value
  percents.h = (matches[1] / max.h) * 100
  percents.m = (matches[2] / max.m) * 100
  -- et cetera

  -- finally, echo your new prompt
  -- it's not coloured, but it's not a
  -- terribly basic thing to do
  newprompt = "h:" .. percents.h
          .. " m:" .. percents.m
  -- et cetera
  Note(newprompt)
end -- function


The code is un-tested, and I think there might be a problem with decimal values you don't want (like "h:66.66666666" instead of "h:66"). Still, that's how you might do it.
#8
No no, You see I want to be able to type h 50 and it cures when my health is at 50% of max health, or I do 75% and it cures when its below 75% of max.
USA #9
Well, it would be the same basic idea, except you'd create aliases to set those percentages as variables. Then in the prompt trigger script, instead of creating a new prompt, just compare the percent you calculated against the percent you want to cure at, and if the former is less than or equal to the latter, you cure.
#10
I have figured out how to the the percentage bit with the help of Twisol and here is what I got

function prompt (name, line, matches, styles)
SetVariable ("current_h", matches [1])
SetVariable ("current_m", matches [2])
SetVariable ("current_e", matches [3])

end -- function

function set_max (name, line, matches, styles)
SetVariable ("max_h", matches [2])
SetVariable ("max_m", matches [4])
SetVariable ("max_e", matches [6])
end -- function

function percent_h (name, line, matches, styles)

max = {}
max_h = GetVariable ("max_h")

percents = {}
SetVariable ("perc_h", max_h / ( 100 / matches [1] ))
end -- function

function percent_m (name, line, matches, styles)

max = {}
max_m = GetVariable ("max_m")

percents = {}
SetVariable ("perc_m", max_m / ( 100 / matches [1] ))
end -- function

function percent_e (name, line, matches, styles)

max = {}
max_e = GetVariable ("max_e")

percents = {}
SetVariable ("perc_e", max_e / ( 100 / matches [1] ))
end -- function

Now the next problem is how can I put multiple triggers under one scripting chunk. I'm trying to make an autosipper and when I sip something I need it to change the sip_balance to 0 then when I regain balance set it to 10, so I can do this with two seperate scripting chunks, but after awhile I am guessing that would get messy.
#11
I don't see whats wrong with this, but I have triggers to set the sip_balance off and on, but it will spam sipping health until it gets above the desired point then it stops, rather than stopping till I can drink again.


function sip_off (name, line, matches, styles)
SetVariable ("sip_balance", "false")
end -- function

function sip_on(name, line, matches, styles)
SetVariable ("sip_balance", "true")
end -- function

function sipper ()

sip = {}
base_sip = GetVariable ("base_sip")
current_h = GetVariable ("current_h")
perc_h = GetVariable ("perc_h")
sip_balance = GetVariable ("sip_balance")

if current_h < perc_h and sip_balance then
Send ("sip health")
SetVariable ("sip_balance", "false")
end -- if
end -- function
USA #12
If you ctrl+click each related trigger in the Triggers dialog, click Copy, and ctrl+v it to here, it would go a long way towards helping you.

Also, you should consider using [code] tags around your code when posting (like the triggers XML, and the lua script itself). It preserves spacing, and sets the text in a monospaced font. Compare:

function foo()
Note("bar")
end


function foo()
  Note("bar")
end

Amended on Wed 15 Apr 2009 06:55 AM by Twisol
#13
Alrighty, this is everything I have for it.

<triggers>
<trigger
enabled="y"
match="^(\d+)h, (\d+)m\, (\d+)e\, (\d+)p\, (\d+)en\, (\d+)w\ (.*?)$"
regexp="y"
script="prompt"
send_to="12"
sequence="100"
>
<send>sipper ()</send>
</trigger>
<trigger
enabled="y"
match="^\| Health \: (.*?)\/(.*?) Mana \: (.*?)\/(.*?) Ego \: (.*?)\/(.*?) (.*?) \|$"
regexp="y"
script="set_max"
sequence="100"
>
</trigger>
<trigger
enabled="y"
match="^The potion heals and soothes you\.$"
regexp="y"
script="sip_balance_off"
sequence="100"
>
</trigger>
<trigger
enabled="y"
match="^You may drink another health\, mana\, or bromide potion\.$"
regexp="y"
script="sip_balance_on"
sequence="100"
>
</trigger>
</triggers>




function prompt (name, line, matches, styles)
	SetVariable ("current_h", matches [1])
	SetVariable ("current_m", matches [2])
	SetVariable ("current_e", matches [3])
end -- function

function set_max (name, line, matches, styles)
	SetVariable ("max_h", matches [2])
	SetVariable ("max_m", matches [4])
	SetVariable ("max_e", matches [6])
end -- function

function percent_h (name, line, matches, styles)

	max = {}	
	max_h = GetVariable ("max_h")

	percents = {}
	SetVariable ("perc_h", max_h / ( 100 / matches [1] ))
end -- function

function percent_m (name, line, matches, styles)

	max = {}	
	max_m = GetVariable ("max_m")

	percents = {}
	SetVariable ("perc_m", max_m / ( 100 / matches [1] ))
end -- function

function percent_e (name, line, matches, styles)

	max = {}	
	max_e = GetVariable ("max_e")

	percents = {}
	SetVariable ("perc_e", max_e / ( 100 / matches [1] ))
end -- function

function sip_off (name, line, matches, styles)
	SetVariable ("sip_balance", "false")
end -- function

function sip_on(name, line, matches, styles)
	SetVariable ("sip_balance", "true")
end -- function
	
function sipper ()
	
	sip = {}
	base_sip = GetVariable ("base_sip")	
	current_h = GetVariable ("current_h")
	perc_h = GetVariable ("perc_h")
	sip_balance = GetVariable ("sip_balance")

if current_h < perc_h and sip_balance then
		Send ("sip health")
		SetVariable ("sip_balance", "false")
		end -- if
end -- function

#14
Not that I'm trying to beat a dead horse, but if anyone finds this relevant anymore I believe the problem was in:


function sipper ()
	
	sip = {}
	base_sip = GetVariable ("base_sip")	
	current_h = GetVariable ("current_h")
	perc_h = GetVariable ("perc_h")
	sip_balance = GetVariable ("sip_balance")

if current_h < perc_h and sip_balance then
		Send ("sip health")
		SetVariable ("sip_balance", "false")
		end -- if
end -- function


Where it says "and sip_balance" I believe it should be checking if sip_balance is true, not if it simply exists. Other than that you seem to have come pretty far from your first post.
USA #15
Faedara said:
Where it says "and sip_balance" I believe it should be checking if sip_balance is true, not if it simply exists. Other than that you seem to have come pretty far from your first post.


More like "true", not true, because MUSHclient variables are always strings, and he didn't do any conversion after retrieving that value. That said, that was definitely a well-spotted problem.
#16
Thanks, I'm just taking practice swings at finding errors in Lua so that I can figure out what the errors in my coding are.