[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  New to Lua

New to Lua

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Pages: 1 2  

Posted by BlueEyes   (28 posts)  [Biography] bio
Date Mon 13 Apr 2009 08:48 PM (UTC)
Message
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
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #1 on Mon 13 Apr 2009 09:46 PM (UTC)
Message
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.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by BlueEyes   (28 posts)  [Biography] bio
Date Reply #2 on Mon 13 Apr 2009 10:04 PM (UTC)
Message
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.
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #3 on Mon 13 Apr 2009 10:45 PM (UTC)
Message
That *is* scripting. SetVariable is a script function.

However if you want to use a script file, see http://mushclient.com/scripting

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by BlueEyes   (28 posts)  [Biography] bio
Date Reply #4 on Mon 13 Apr 2009 10:59 PM (UTC)
Message
But when I do the SetVariable %1 command in the scripting it changes my variabe to %1 rather than the actual number.
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #5 on Mon 13 Apr 2009 11:32 PM (UTC)

Amended on Mon 13 Apr 2009 11:46 PM (UTC) by Nick Gammon

Message
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.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by BlueEyes   (28 posts)  [Biography] bio
Date Reply #6 on Tue 14 Apr 2009 01:23 AM (UTC)

Amended on Tue 14 Apr 2009 01:29 AM (UTC) by BlueEyes

Message
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.
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #7 on Tue 14 Apr 2009 03:02 AM (UTC)
Message
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.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by BlueEyes   (28 posts)  [Biography] bio
Date Reply #8 on Tue 14 Apr 2009 06:13 PM (UTC)
Message
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.
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #9 on Tue 14 Apr 2009 06:45 PM (UTC)
Message
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.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by BlueEyes   (28 posts)  [Biography] bio
Date Reply #10 on Tue 14 Apr 2009 06:59 PM (UTC)
Message
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.
[Go to top] top

Posted by BlueEyes   (28 posts)  [Biography] bio
Date Reply #11 on Wed 15 Apr 2009 05:47 AM (UTC)
Message
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
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #12 on Wed 15 Apr 2009 06:53 AM (UTC)

Amended on Wed 15 Apr 2009 06:55 AM (UTC) by Twisol

Message
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


'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by BlueEyes   (28 posts)  [Biography] bio
Date Reply #13 on Wed 15 Apr 2009 05:31 PM (UTC)
Message
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
[Go to top] top

Posted by Faedara   (106 posts)  [Biography] bio
Date Reply #14 on Mon 22 Nov 2010 09:14 AM (UTC)
Message
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.

The eternally clueless <3
Currently looking for a mentor/tutor to help me learn everything about Lua from the ground up. I can't understand 28*41 until I learn what causes 2+2.
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


43,570 views.

This is page 1, subject is 2 pages long: 1 2  [Next page]

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]