Incrementing Variables

Post Reply
Yooper
Posts: 4
Joined: Mon Mar 07, 2016 4:59 pm

Incrementing Variables

Post by Yooper »

I'm trying to use a trigger to increment a variable. The pattern I'm trying to count looks like this.
* You think your sewing skill has improved. *
* You think your cooking skill has improved. *
So I trigger off of that and use the following :
Code: [show] | [select all] lua
^\* You think your (.*) skill has improved. \*$
Right now it's not returning anything from inside of the if statement. Eventually I hope to save this as a table and be able to keep my variables from one session to the next.
Code: [show] | [select all] lua
echo("matched is " .. matches[2])

MatchedName = matches[2]

echo("matchedName is " .. MatchedName)

if MatchedName == nil then
	MatchedName = 1
	echo("count created for " .. MatchedName)
else
MatchedName = MatchedName + 1
echo("count is now " .. MatchedName)
end

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

Re: Incrementing Variables

Post by SlySven »

Might be worth echoing higher matches values just in case something else is being captured (perhaps that '*' is not being escaped correctly?) - Or, and I am pushing the limits of my lua-foo here: doesn't braces '(' ')' around an expression mean "expand it as if it was a regular expression - but don't bother to report it" used to discard variable but unwanted input?

Yooper
Posts: 4
Joined: Mon Mar 07, 2016 4:59 pm

Re: Incrementing Variables

Post by Yooper »

So far the captured text is displayed properly on both the "matched is " and also the "matchedName is " portion. Once it goes into the If Else loop that I don't get any response.

For example if it sees * You think your sewing skill has improved. * The trigger will output sewing properly, but not do anything inside of the If statement.

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

Re: Incrementing Variables

Post by SlySven »

What does the Error screen (or the Debug console) show up when the code runs?

hogarius
Posts: 35
Joined: Mon Nov 21, 2011 8:35 pm

Re: Incrementing Variables

Post by hogarius »

Your trigger currently assigns a word ("sewing" or "cooking" or some other skill) to the MatchedName variable, and then tries to numerically add 1 to that string. This is not going to work.

Yooper
Posts: 4
Joined: Mon Mar 07, 2016 4:59 pm

Re: Incrementing Variables

Post by Yooper »

Hmm, I see what you mean. Would a table.insert be more in line for this sort of thing?

I believe the error I see is exactly what hogarius is mentioning.
Code: [show] | [select all] lua
LUA: ERROR running script Test Announce with Tables (Trigger3) ERROR:[string "function Trigger3()...
"]:12: attempt to perform arithmetic on global 'MatchedName' (a string value)

Yooper
Posts: 4
Joined: Mon Mar 07, 2016 4:59 pm

Re: Incrementing Variables

Post by Yooper »

I've made some progress! I think...

The trigger successfully adds the proper variable into t[SkillName] but I believe I'm still trying to fill it improperly.

The trigger returns this right now :
matched is controlskillname is control
So it's filling the name properly, but I'm missing something as to set the value to the proper key.
Code: [show] | [select all] lua
echo("matched is " .. matches[2])

t = {}

t[SkillName] = matches[2]

echo("skillname is " ..t[SkillName])

if t[SkillName] == nil then
	t[SkillName] = 1
	echo("count created for " .. t[SkillName])
else
t[SkillName] = t[SkillName] + 1
echo("count is now " .. t[SkillName])

end

nobody
Posts: 6
Joined: Thu Mar 24, 2016 1:57 am

Re: Incrementing Variables

Post by nobody »

Not sure if this helps, but I have had a lot of luck using tonumber(VARIABLE_NAME) when things are unresponsive.

Also I often test expressions and trigger scripts manually from the command line using

lua t(SkillName) = tonumber(t(SkillName)) + 1

or

lua name_of_function()

On the other hand I have about 3 days or so exposure to lua and mudlet....

>;)

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

Re: Incrementing Variables

Post by SlySven »

Yeah the whole is it a string or is it a number thing is a bit confusing to a hard-core C/C++ coder - actually "it can be either" is how the lua fans would respond - but to me it seems a bit like Schrodinger's Cat - except that when I want a "live" number I open the box to find a string that has killed my script. ;)

That tonumber(...) function is probably the way to go. :)

Post Reply