Help with Basic math

Post Reply
Arania
Posts: 7
Joined: Tue Jun 12, 2012 8:51 pm

Help with Basic math

Post by Arania »

Ok, so I'm trying to create a script to do basic math for me, Unfortunately I'm a complete coding noob :D

If anyone could give me some idea if it's possible and how to do It I'd appreciate it.
As an example. I used to have a set of triggers for the Achaea Bopolopia cows that looked like this:

#TRIGGER {If (%1) cows jumped into Runaway River and (%2) } {#math answer %1-%2;say @answer}


How would these kinds of things be done in Mudlet?

Xarikins
Posts: 3
Joined: Thu Sep 13, 2012 3:31 am

Re: Help with Basic math

Post by Xarikins »

Code: [show] | [select all] lua
Regex trigger match pattern:
^If (\d+) cows jumped into Runaway River and (\d+)

Trigger body:
answer = matches[2]-matches[3]
send("say " .. answer)
The \d+ matches numbers, which goes into 'matches[2]' and 'matches[3]' similar to %1 and %2. You don't need to do #math, because Lua understands how to do math if you just do "variable = a + b"

Then you send what you want to say.

User avatar
demonnic
Posts: 886
Joined: Sat Dec 05, 2009 3:19 pm

Re: Help with Basic math

Post by demonnic »

Trigger matches are stored as strings. The code posted before needs to be modified slightly to work.
Code: [show] | [select all] lua
Regex trigger match pattern:
^If (\d+) cows jumped into Runaway River and (\d+)

Trigger body:
answer = tonumber(matches[2])-tonumber(matches[3])
send("say " .. answer)

Annwyl
Posts: 5
Joined: Fri Mar 14, 2014 10:57 pm

Re: Help with Basic math

Post by Annwyl »

I'm virtually inept at coding and I can't type numbers fast enough in-game to answer the questions within 2 seconds but the trigger help, above, also doesn't work for me. What am I missing?

^If (\d+) cows jumped into Runaway River and (\d+)
answer = tonumber(matches[2])-tonumber(matches[3])
send("say " .. answer)

^If (\d+) beavers and (\d+) beavers got together with (\d+)
answer = tonumber(matches[2])+tonumber(matches[3])+tonumber(matches[4])
send("say " .. answer)

And so on. I've tried the trigger phrases as substrings, as perl regex, and as exact matches but nothing happens when the phrase is triggered.

Post Reply