It is currently February 19th, 2020, 2:44 pm

Logic Help

Discuss the use of Lua in Script measures.
ms310
Posts: 148
Joined: April 1st, 2015, 7:16 am

Logic Help

Post by ms310 »

Hi - I am just getting into LUA scripting and I need some help with some logic. I would like the following to be a bit more clever:

Code: Select all

local function getMainString( temp, rain )	
    local negation = (temp > JACKET_LIMIT) and " don't" or ""
    local outerwear = (temp < COAT_LIMIT) and "coat" or "jacket"
    local unbrella = (rain=="Yes") and "An umbrella would be useful." or ""
    return string.format("You%s need a %s. %s", negation, outerwear, unbrella)
end
What I would really like is a structure that would give me sentences like:

"You don't need a jacket, but an umbrella would be useful"
"You need a jacket and an umbrella today"
"You need need a coat and an umbrella - YUCK!"

Basically the local <varname> = (condition) and "positive result" or "negative result" seems too limiting for what I need. I need a better structure so I can do more checks for temp and rain.

I appreciate any help real coders can provide!
ms310
Posts: 148
Joined: April 1st, 2015, 7:16 am

Re: Logic Help

Post by ms310 »

Well this works for me:

Code: Select all

local function getMainString( temp, rain )
    local negation = (temp > JACKET_LIMIT) and " don't" or ""
    local butand = ((temp > JACKET_LIMIT) and (rain=="Yes")) and " but an" or ((temp < JACKET_LIMIT) and (rain=="Yes")) and " and an" or "."
    local outerwear = (temp < COAT_LIMIT) and "coat" or "jacket"
    local umbrella = (rain=="Yes") and "umbrella would be useful." or ""
    return string.format("You%s need a %s%s %s", negation, outerwear, butand, umbrella)
end
Its the local = butand that just seems "hackey" to me - is this a "good" way to assign values or am I missing a better construct?

Thanks!
You do not have the required permissions to view the files attached to this post.
ms310
Posts: 148
Joined: April 1st, 2015, 7:16 am

Re: Logic Help

Post by ms310 »

Hi All!

Having a little trouble with LUA syntax I think. Here is my code:

Code: Select all

local butand = ((temp > JACKET_LIMIT) and (rain=="Yes")) and " but you do need an" or ((temp < JACKET_LIMIT)and (rain=="Yes") and " and an" or ".")
The variables are:

temp = 18
JACKET_LIMIT = 20
rain="Yes"


I EXPECT the following:

butand = " and an"

Instead it is falling through to:

butand = "."

Thanks for any help you can provide


UPDATE: DOH! The syntax was fine. Needed the following:

Code: Select all

temp <= JACKET_LIMIT
in the second half of the statement.

By the way - is this the best method of doing this in LUA? Is there a better, more concise method of doing this?

THANKS!
TGonZo
Posts: 58
Joined: April 25th, 2015, 8:19 pm
Location: Virginia

Re: Logic Help

Post by TGonZo »

I tested your lua script, and it seems to work for me.
I'm not sure if you are using this function as part of an Update lua script, or if you are using a !CommandMeasure. I chose to test it as an Update.
I was changing the Temp and Rain variables and refreshing the skin to test. I was getting the responses I thought it should give.
The logic seemed to work for me. I did change your "butand" language to make the sentence sound correct, but I did not change the logic.

I'm not sure if there is a "better way", but this seems to work.

Here the exact code of yours I tested.
Is this all correct, or am I missing something?

When I run it with 18 and Yes, it responds with:
You need a jacket and an umbrella would be useful.



test_weather.ini

Code: Select all

[Rainmeter]
Update=1000

[measureTempProcess]
Measure=Script
ScriptFile=test_weather.lua
Temp=18
Rain=Yes

[meterShowWxCondition]
Meter=String
MeasureName=measureTempProcess
X=10
Y=10
FontSize=12
FontColor=255,255,255,220
SolidColor=0,0,0,200
Padding=15,15,15,15
AntiAlias=1
Text=%1

test_weather.lua

Code: Select all

local function getMainString( temp, rain )
    local negation = (temp > JACKET_LIMIT) and " don't" or ""
    local outerwear = (temp < COAT_LIMIT) and "coat" or "jacket"
    local butand = ((temp > JACKET_LIMIT) and (rain=="Yes")) and " but an" or ((temp <= JACKET_LIMIT) and (rain=="Yes") and " and an" or ".")
    local umbrella = (rain=="Yes") and "umbrella would be useful." or ""
    return string.format("You%s need a %s%s %s", negation, outerwear, butand, umbrella)
end



function Update()

  JACKET_LIMIT = 20
  COAT_LIMIT = 10

  sTemp = SELF:GetOption('Temp')
  nTemp = tonumber(sTemp)
  sRain = SELF:GetOption('Rain')

  return getMainString(nTemp, sRain)

end