Page 1 of 1
Need help with function for changing boolean state
Posted: Mon Jan 30, 2023 6:34 pm
by MarsOdin
I wanted to create a function that can change any boolean variable, if true make it false and the other way around. So I came up with this:
Code: Select all
local test = true
function switch_boolean(var)
if var then
var = false
else
var = true
end
end
function love.keypressed(key)
if key == "<" then
switch_boolean(test)
end
end
function love.draw()
love.graphics.print(tostring(test),0,0)
end
But why does this not change my bolean variable? But if I change the function like this it obviously works:
Code: Select all
function switch_boolean(var)
if test then
test = false
else
test = true
end
end
I've tried everything I can think of and I don't understand why my original function doesn't work. I'm obviously missing something. Help would be appreciated. Thanks!
Re: Need help with function for changing boolean state
Posted: Mon Jan 30, 2023 6:43 pm
by Andlac028
You are just modifying reference of value, that wouldn't change the value itself. Return value and save it in function like:
Code: Select all
function something(var)
-- do work
return result
end
test = something(test)
Also, I think, using not is easier than using dedicated function for it:
This will effectively toggle boolean.
Re: Need help with function for changing boolean state
Posted: Mon Jan 30, 2023 6:44 pm
by GVovkiv
Because lua passes by reference only functions and tables (?) so when you pass boolean to function, "var" will be newly created variable inside function.
Also "not" exist:
Code: Select all
function switch_boolean(var)
return not var
end
Re: Need help with function for changing boolean state
Posted: Mon Jan 30, 2023 6:47 pm
by GVovkiv
Also, I think, using not is easier than using dedicated function for it:
Well, i mean, you could do function "switch(bool)" which should be more elegant, especially on longer names? Also since most ide complete functions for you, it probably will be even faster to type?
Re: Need help with function for changing boolean state
Posted: Mon Jan 30, 2023 6:50 pm
by Andlac028
GVovkiv wrote: ↑Mon Jan 30, 2023 6:44 pm
Because lua passes by reference only functions and tables (?)
If I remember correctly, tables are also partially "copied", but only the root reference, so you can not change table directly (only return new table), but you can change table values as they are just references (copying whole tables would be very inefficient)
Re: Need help with function for changing boolean state
Posted: Mon Jan 30, 2023 6:52 pm
by GVovkiv
Andlac028 wrote: ↑Mon Jan 30, 2023 6:50 pm
If I remember correctly, tables are also partially "copied", but only the root reference, so you can not change table directly (only return new table), but you can change table values as they are just references (copying whole tables would be very inefficient)
I don't know why, but this is most confusing part of lua for me, when it does pass by reference and when it's just copy of value...
Re: Need help with function for changing boolean state
Posted: Mon Jan 30, 2023 6:54 pm
by Andlac028
GVovkiv wrote: ↑Mon Jan 30, 2023 6:47 pm
Well, i mean, you could do function "switch(bool)" which should be more elegant, especially on longer names? Also since most ide complete functions for you, it probably will be even faster to type?
Most IDEs also completes keywords, so I don't think, that function would be faster to type. Also if you would call it a lot (for example in a big loop), the extra function can cause performance bottleneck, so I think, not is better.
GVovkiv wrote: ↑Mon Jan 30, 2023 6:52 pm
I don't know why, but this is most confusing part of lua for me, when it does pass by reference and when it's just copy of value...
From
Lua 5.1 manual:
Tables, functions, threads, and (full) userdata values are objects: variables do not actually contain these values, only references to them. Assignment, parameter passing, and function returns always manipulate references to such values; these operations do not imply any kind of copy.
Re: Need help with function for changing boolean state
Posted: Mon Jan 30, 2023 7:00 pm
by MarsOdin
Thanks to all of you for the help.
I wasn't aware of var = not var, so I'm using it.
Re: Need help with function for changing boolean state
Posted: Mon Jan 30, 2023 7:08 pm
by GVovkiv
Andlac028 wrote: ↑Mon Jan 30, 2023 6:54 pm
From
Lua 5.1 manual:
Tables, functions, threads, and (full) userdata values are objects: variables do not actually contain these values, only references to them. Assignment, parameter passing, and function returns always manipulate references to such values; these operations do not imply any kind of copy.
And yet, i still sometimes forget about it, hah
Re: Need help with function for changing boolean state
Posted: Sat Feb 11, 2023 7:41 am
by soulmata
Here's a simple function I use in the game I am developing now that will flip the bool you pass to it, and optionally does something based on what path it took (in my case, it will log a given message depending on which way the bool was flipped)
Code: Select all
function f_ed_toggle_bool(bool, msgFalse, msgTrue)
-- simply return the reverse of a bool you got, or false in any other case, if provided msg, log it
if (bool) then
if (msgFalse) then f_ed_log(msgFalse,"info") end
return false
else
if (msgTrue) then f_ed_log(msgTrue,"info") end
return true
end
end