Page 1 of 1

Function that switch a boolean value

Posted: Mon Dec 28, 2020 10:22 am
by jefolo
Hi,
another newbie question.
I'm trying to make a function that switch a boolean value. In particular I would like to pass a parameter to this function (the parameter is the boolean variable) and the function change it to true or false.

I wrote this, but nothing happen, could somebody help me?

Code: Select all

test = true

function love.draw()
  if test then
    love.graphics.print("Test true")
  else
    love.graphics.print("Test false")
  end
end

function love.mousepressed( x, y, button, istouch, presses )
  if button==1  then
      switchParameter(test)
    end
  end


  function switchParameter(param)
    param = param
    if param == false
      then
        param = true
      else
        param = false
    end
  end
Thank you!

Re: Function that switch a boolean value

Posted: Mon Dec 28, 2020 1:28 pm
by darkfrei

Code: Select all

function love.mousepressed( x, y, button, istouch, presses )
	if button==1  then
		test = not test -- turns false to true and true to false
	end
end

Re: Function that switch a boolean value

Posted: Mon Dec 28, 2020 1:33 pm
by darkfrei
Another way: return the new value and set it to the test value

Code: Select all

test = true

function love.draw()
  if test then
    love.graphics.print("Test true")
  else
    love.graphics.print("Test false")
  end
end

function love.mousepressed( x, y, button, istouch, presses )
  if button==1  then
    test = switchParameter(test)
  end
end

function switchParameter(param)
  if param then
    return false
  else
    return true
  end
end

Re: Function that switch a boolean value

Posted: Mon Dec 28, 2020 1:47 pm
by sphyrth
Here's another take:

Code: Select all

function switchParameter(param)
  if param and param == true then
    return false
  else
    return true
  end
end

Re: Function that switch a boolean value

Posted: Mon Dec 28, 2020 2:21 pm
by jefolo
darkfrei wrote: Mon Dec 28, 2020 1:33 pm Another way: return the new value and set it to the test value

Code: Select all

test = true

function love.draw()
  if test then
    love.graphics.print("Test true")
  else
    love.graphics.print("Test false")
  end
end

function love.mousepressed( x, y, button, istouch, presses )
  if button==1  then
    test = switchParameter(test)
  end
end

function switchParameter(param)
  if param then
    return false
  else
    return true
  end
end
That was what I've been looking for, thank you.
I should study better the way return work :-)

Re: Function that switch a boolean value

Posted: Mon Dec 28, 2020 7:44 pm
by pgimeno
@sphyrth why the double comparison? If param is true, the first check will always be true, therefore the whole condition will always be true. If it is anything else, the second condition will always be false, making the whole condition always false. Consequently, the first check is redundant.

If you know that param will always be a boolean, you can use just 'if param then...'; if you want to ensure that it's a boolean, you can use 'assert(type(param) == "boolean")' before the comparison.

Re: Function that switch a boolean value

Posted: Mon Dec 28, 2020 9:18 pm
by sphyrth
pgimeno wrote: Mon Dec 28, 2020 7:44 pm @sphyrth why the double comparison?
I have this a habit of thinking "What if param == nil?", and that slipped into my style. Now that I think about it, it IS unnecessary given the context.