Function that switch a boolean value

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
jefolo
Prole
Posts: 12
Joined: Wed Dec 23, 2020 3:20 pm

Function that switch a boolean value

Post 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!
User avatar
darkfrei
Party member
Posts: 1204
Joined: Sat Feb 08, 2020 11:09 pm

Re: Function that switch a boolean value

Post 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
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
darkfrei
Party member
Posts: 1204
Joined: Sat Feb 08, 2020 11:09 pm

Re: Function that switch a boolean value

Post 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
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
sphyrth
Party member
Posts: 260
Joined: Mon Jul 07, 2014 11:04 am
Contact:

Re: Function that switch a boolean value

Post 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
jefolo
Prole
Posts: 12
Joined: Wed Dec 23, 2020 3:20 pm

Re: Function that switch a boolean value

Post 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 :-)
User avatar
pgimeno
Party member
Posts: 3674
Joined: Sun Oct 18, 2015 2:58 pm

Re: Function that switch a boolean value

Post 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.
sphyrth
Party member
Posts: 260
Joined: Mon Jul 07, 2014 11:04 am
Contact:

Re: Function that switch a boolean value

Post 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.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 5 guests