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:
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!
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:
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?
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)
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...
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...
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.
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.
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)
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
Endless Dark: An existential horror game written in LOVE in which you are tasked with keeping a sleeper colony ship intact.