Variables not updating

Posted by Kurapiira on Thu 11 Mar 2010 11:13 AM — 5 posts, 17,927 views.

#0
my hp prompt is catching the variables,
but when i go to display them, it shows the last value
of the variable...

i.e. HP: 208/208 Ftg: 61/61 MP: 22/22

( I cast a spell )

HP: 208/208 Ftg: 61/61 MP: 12/22

but the send function sends the first values..

any ideas?

trig value:
^HP\: (.*?)\/(.*?) Ftg\: (.*?)\/(.*?) MP\: (.*?)\/(.*?)$


script:
SetVariable ("hp","%1")
SetVariable ("maxhp","%2")
SetVariable ("ftg","%3")
SetVariable ("maxftg","%4")
SetVariable ("mana","%5")
SetVariable ("maxmana","%6")

Send ("'@hp @maxhp")
Send ("'@ftg @maxftg")
Send ("'@mana @maxmana")

should i be using setVariable for this?
p.s. the line I am capturing is hilighted in ansi from the mud...

Thanx for any help,
K.
Amended on Thu 11 Mar 2010 11:25 AM by Kurapiira
USA #1
Actually, the issue is a little more subtle. The @variable syntax isn't really part of Lua. Nick supports it by running your Send content through a preprocessor, which processes it before Lua does. It looks for the @variable and %1 things, looks up what their values are right at that moment, and then replaces them inline. So if you have this:

if "@variable" == "test" then

... then by the time Lua gets to it, it's actually (example):

if "test" == "test" then.

Got it? Now, what's going on is that it gets those values before you ever call SetVariable to change their values. You -are- changing the values, but it's not seeing that, because it already got the values beforehand. What you should do in this case is this:

SetVariable ("hp","%1")
SetVariable ("maxhp","%2")
SetVariable ("ftg","%3")
SetVariable ("maxftg","%4")
SetVariable ("mana","%5")
SetVariable ("maxmana","%6")

Send (string.format("'%s %s", GetVariable("hp"), GetVariable("maxhp")))
Send (string.format("'%s %s", GetVariable("ftg"), GetVariable("maxftg")))
Send (string.format("'%s %s", GetVariable("mana"), GetVariable("maxmana")))


I used string.format because it looks tidier than a bunch of concatenations. ;) And in case you were wondering, the %s things in the string.format call are not bothered by the preprocessor, since those are letters, not numbers.
Amended on Thu 11 Mar 2010 05:25 PM by Twisol
Australia Forum Administrator #2
Twisol said:

And in case you were wondering, the %s things in the string.format call are not bothered by the preprocessor, since those are letters, not numbers.


Although, safer to double them. The preprocessor turns %% into %.

i.e.


Send (string.format("'%%s %%s", GetVariable("hp"), GetVariable("maxhp")))
Send (string.format("'%%s %%s", GetVariable("ftg"), GetVariable("maxftg")))
Send (string.format("'%%s %%s", GetVariable("mana"), GetVariable("maxmana")))

USA #3
Never knew that, good point!
#4
many thanks amigos,
works like a charm.

And thanks for the info on the preprocessor
and the %% !

K.