Need help with function for changing boolean state

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
MarsOdin
Prole
Posts: 12
Joined: Tue Apr 02, 2019 11:46 pm

Need help with function for changing boolean state

Post 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!
Andlac028
Party member
Posts: 174
Joined: Fri Dec 14, 2018 2:27 pm
Location: Slovakia

Re: Need help with function for changing boolean state

Post 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:

Code: Select all

var = not var
This will effectively toggle boolean.
User avatar
GVovkiv
Party member
Posts: 685
Joined: Fri Jan 15, 2021 7:29 am

Re: Need help with function for changing boolean state

Post 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
User avatar
GVovkiv
Party member
Posts: 685
Joined: Fri Jan 15, 2021 7:29 am

Re: Need help with function for changing boolean state

Post by GVovkiv »

Also, I think, using not is easier than using dedicated function for it:

Code: Select all

var = not var
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?
Andlac028
Party member
Posts: 174
Joined: Fri Dec 14, 2018 2:27 pm
Location: Slovakia

Re: Need help with function for changing boolean state

Post 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)
User avatar
GVovkiv
Party member
Posts: 685
Joined: Fri Jan 15, 2021 7:29 am

Re: Need help with function for changing boolean state

Post 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...
Andlac028
Party member
Posts: 174
Joined: Fri Dec 14, 2018 2:27 pm
Location: Slovakia

Re: Need help with function for changing boolean state

Post 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.
MarsOdin
Prole
Posts: 12
Joined: Tue Apr 02, 2019 11:46 pm

Re: Need help with function for changing boolean state

Post by MarsOdin »

Thanks to all of you for the help.
I wasn't aware of var = not var, so I'm using it.
User avatar
GVovkiv
Party member
Posts: 685
Joined: Fri Jan 15, 2021 7:29 am

Re: Need help with function for changing boolean state

Post 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
User avatar
soulmata
Prole
Posts: 31
Joined: Sat Jan 26, 2013 8:14 am
Contact:

Re: Need help with function for changing boolean state

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

Who is online

Users browsing this forum: Ahrefs [Bot] and 1 guest