It is currently March 28th, 2024, 6:51 pm

Simple if-then-else statement not working as intended

Discuss the use of Lua in Script measures.
User avatar
raiguard
Posts: 660
Joined: June 25th, 2015, 7:02 pm
Location: The Sky, USA

Simple if-then-else statement not working as intended

Post by raiguard »

EDIT: The full code this is included in can be found here (in the 'structure-refactor' branch, under Skins/ModernGadgets/@Resources/Scripts/SkinSettings/CpuSettings.lua)

I have spent a good 45 minutes trying to solve this puzzle, but to no avail. Here I have a few simple if-then-else statements, which ought to work as intended. However, try as I might, the 'isHwinfoAvailable == 1' if-then-else ALWAYS executes the code in the else section, even when isHwinfoAvailable is 1 (as proven by the !Log command in the else section).

So what is going on!?

(This code is for a CPU Meter settings skin. The ToggleCoreTemps function toggles the core temperatures on and off by calling a !CommandMeasure bang in the CPU Meter skin itself. That part itself works fine, the only part that isn't working is the 'isHwinfoAvailable == 1' if-then-else statement that the !CommandMeasure is contained in. It always executes the code in the else section, even when the condition should be true).

Code: Select all

function Initialize()

  cpuSettingsPath = SKIN:GetVariable('cpuSettingsPath')
  cpuMeterPath = SKIN:GetVariable('cpuMeterPath')
  cpuMeterConfig = SKIN:GetVariable('cpuMeterConfig')

end

function ToggleCoreTemps(currentValue)

  local isHwinfoAvailable = SKIN:GetVariable('isHwinfoAvailable')
  local cpuCores = SKIN:GetVariable('cpuCores')

  if currentValue == 0 then
    SKIN:Bang('!SetVariable', 'showCoreTemps', '1')
    SKIN:Bang('!WriteKeyValue', 'Variables', 'showCoreTemps', '1', cpuSettingsPath)

    if isHwinfoAvailable == 1 then
      SKIN:Bang('!CommandMeasure', 'MeasureCpuConfigScript', 'ToggleTemps(' .. cpuCores .. ', true)', cpuMeterConfig)
    else
      SKIN:Bang('!Log', isHwinfoAvailable .. ' | Cannot display core temperatures, for HWiNFO is not running!', 'Error')
      SKIN:Bang('!ShowMeter', 'CoreTempsErrorImage')
    end

  else
    SKIN:Bang('!SetVariable', 'showCoreTemps', '0')
    SKIN:Bang('!WriteKeyValue', 'Variables', 'showCoreTemps', '0', cpuSettingsPath)
    SKIN:Bang('!CommandMeasure', 'MeasureCpuConfigScript', 'ToggleTemps(' .. cpuCores .. ', false)', cpuMeterConfig)
    SKIN:Bang('!HideMeter', 'CoreTempsErrorImage')
  end
  
  SKIN:Bang('!UpdateMeterGroup', 'ShowCoreTemps')
  SKIN:Bang('!Redraw')

end
”We are pretty sure that r2922 resolves the regression in resolution caused by a reversion to a revision.” - jsmorley, 2017
User avatar
ikarus1969
Posts: 571
Joined: February 28th, 2011, 3:20 pm
Location: Vienna, Austria

Re: Simple if-then-else statement not working as intended

Post by ikarus1969 »

I would add a "tonumber" when retrieving "isHwinfoAvailable" in the "ToggleCoreTemps" function, so please try:

Code: Select all

local isHwinfoAvailable = tonumber(SKIN:GetVariable('isHwinfoAvailable'))
you can check type of a variable with the "type"-function: see here from the lua-docs.
User avatar
raiguard
Posts: 660
Joined: June 25th, 2015, 7:02 pm
Location: The Sky, USA

Re: Simple if-then-else statement not working as intended

Post by raiguard »

ikarus1969 wrote:I would add a "tonumber" when retrieving "isHwinfoAvailable" in the "ToggleCoreTemps" function, so please try:

Code: Select all

local isHwinfoAvailable = tonumber(SKIN:GetVariable('isHwinfoAvailable'))
you can check type of a variable with the "type"-function: see here from the lua-docs.
That makes sense. I tried passing the isHwinfoAvailable variable as an argument for the function, instead of using GetVariable(), and that also worked. I guess Rainmeter variables as numbers don't play nice with LUA. Thanks for the suggestion!
”We are pretty sure that r2922 resolves the regression in resolution caused by a reversion to a revision.” - jsmorley, 2017