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?
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
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
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 :-)
@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.
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.