Page 1 of 1

Simple if-then-else statement not working as intended

Posted: May 30th, 2016, 4:33 am
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

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

Posted: May 30th, 2016, 7:06 pm
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.

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

Posted: June 1st, 2016, 8:28 pm
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!