help with script

Post Reply
holypickle
Posts: 10
Joined: Sun Feb 14, 2016 1:06 pm

help with script

Post by holypickle »

I need to write a very simple script that will beep when my hp is below a certain amount. What I'm stuck on is how to "read" the screen in my script, so when it says "HP: X", my script can take in that information.

EulersIdentity
Posts: 27
Joined: Fri Jun 26, 2015 8:52 am

Re: help with script

Post by EulersIdentity »

The trigger line will depend on the prompt.You'll want to brush up on your regular expressions if you haven't already. A simple example is that if your prompt reads thus: HP: 125 MP: 98 Then you'll make your trigger expression
Code: [show] | [select all] lua
^HP: (\d+) MP: (\d+)$
The (\d+) part captures any number which you can get back with matches[2], the MP value is matches[3], if you had more items in parenthesis after that, the pattern continues. If you want to do something when your hp is below a certain value, say 50, you would use the script.
Code: [show] | [select all] lua
if matches[2] < 50 then
    --[lua code to execute]--
end
Though I'd recommend setting up a a variable, say 'hp_alarm = 50', and use 'matches[2] < hp_alarm'.

holypickle
Posts: 10
Joined: Sun Feb 14, 2016 1:06 pm

Re: help with script

Post by holypickle »

brilliant, thank you my friend.

holypickle
Posts: 10
Joined: Sun Feb 14, 2016 1:06 pm

Re: help with script

Post by holypickle »

So wait what does the ^ do? Does that prompt mudlet to read the line?

User avatar
SlySven
Posts: 1023
Joined: Mon Mar 04, 2013 3:40 pm
Location: Deepest Wiltshire, UK
Discord: SlySven#2703

Re: help with script

Post by SlySven »

The '^' (and the '$' !) are meta-characters that mean in this case the start of the line (and the end of the line) respectively. If there is anything else before (or after) in the line then this rexexp will NOT match on that line so it will not be processed...!

EulersIdentity
Posts: 27
Joined: Fri Jun 26, 2015 8:52 am

Re: help with script

Post by EulersIdentity »

What SlySven said. Here's a list of metacharacters. And HERE is an tool that'll let you check if your regular expressions are right. Using the start of line and end of line characters isn't necessary, but is generally recommended, as it prevents your trigger from matching unintended things. Like, if someone sent you a message containing "blah blah HP: 123 blah," you want the trigger to not fire.

Buck
Posts: 19
Joined: Tue Aug 30, 2016 8:11 pm

Re: help with script

Post by Buck »

Also, since matches[2] is a string, you'll have to convert it to a number with tonumber( matches[2]) before comparing.

Post Reply