True False Statments

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
User avatar
tochy97
Prole
Posts: 24
Joined: Sat May 24, 2014 5:06 pm
Location: Dallas, Texas

True False Statments

Post by tochy97 »

is there a way say when this variable is true then these variables can never be true during the time this one is true. Sorry if that is confusing. and sorry that this may seem like a simple question but i cant seem to think up a way to right it. any help would be appreciated.
User avatar
Jasoco
Inner party member
Posts: 3726
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: True False Statments

Post by Jasoco »

Not sure what you mean, but I'll guess:

Maybe something like this?

Code: Select all

if var1 == true and var2 == false and var3 == false and var4 == false then
Or even simpler:

Code: Select all

if var1 and not var2 and not var3 and not var4 then
Maybe?

You can also use or instead of and and parentheses around parts of that for finer control, like so:

Code: Select all

if var1 and var2 or (var3 or var4) then
In this example, either var3 and var4 would have to be true, either one can be true as long as one of them is, but var1 and var2 must be true. But both sections are separate so this if statement will execute as long as var1 and var2 are true, OR if var3 or var4 are true. So basically if only var1 or var2 are true, it won't work unless var3 or var4 are true.

Experiment.
User avatar
tochy97
Prole
Posts: 24
Joined: Sat May 24, 2014 5:06 pm
Location: Dallas, Texas

Re: True False Statments

Post by tochy97 »

well like thats close but like i want it to like make everything else false when its right. not just like happen when everything else is false and its right. like its hard for me to describe.
Advert
Prole
Posts: 3
Joined: Fri Mar 09, 2012 3:45 pm

Re: True False Statments

Post by Advert »

If you mean:

Code: Select all

local a, b, c;
If a is true, b/c can't be true;
If b is true, a/c can't be true;
If c is true, a/b can't be true.

That's not possible automatically, unless you're using a table with metamethods:

Code: Select all

local abc = {}
setmetatable(abc, {
	__index = {
		a = false,
		b = false,
		c = false
	},
	__newindex = function(self, k, v)
		local proxy = getmetatable(self).__index
		print("newindex: ", k, v)
		proxy[k] = v
		if not v then 
			return -- only switch others off
		end
		if k ~= "a" then
			proxy["a"] = false
		end
		if k ~= "b" then
			proxy["b"] = false
		end
		if k ~= "c" then
			proxy["c"] = false
		end
	end
})
local function f()
	print("a", abc.a, "b", abc.b, "c", abc.c)
end

abc.a = true
f()
abc.b = false
f()
abc.a = false
f()
abc.b = true
f()
abc.c = true
f()
Output:

Code: Select all

Success	time: 0.01 memory: 2540 signal:0
newindex: 	a	true
a	true	b	false	c	false
newindex: 	b	false
a	true	b	false	c	false
newindex: 	a	false
a	false	b	false	c	false
newindex: 	b	true
a	false	b	true	c	false
newindex: 	c	true
a	false	b	false	c	true
But, depending on what you're trying to do, you might just want to use a function to set the value, instead of messing around with metamethods.

Code: Select all

local a, b, c = false, false, false

function toggleA()
 a = not a
 if a then
  b, c = false, false
 end
 return a
end
...
User avatar
tochy97
Prole
Posts: 24
Joined: Sat May 24, 2014 5:06 pm
Location: Dallas, Texas

Re: True False Statments

Post by tochy97 »

well metal methods looks to confusing and complicated. i dont have mcuh time to learn this since schools starrting up again so ill try the function. thanks for the help everyone
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: True False Statments

Post by ivan »

tochy97 wrote:well like thats close but like i want it to like make everything else false when its right. not just like happen when everything else is false and its right. like its hard for me to describe.
Generally speaking, when you have a variable that changes when another variable changes then you want to combine them together.
So instead of booleans you may want to use numbers or string. For example:

Code: Select all

tall = false
medium = false
short = true
becomes:

Code: Select all

height = 'short'
-- tall = (height == 'tall')
Again, it depends on what you are trying to do.
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: True False Statments

Post by Plu »

Go with the setter function(s) if these are unrelated things that happen to change in sync.

But if, as ivan suggests, you are using 3 booleans to track a single thing (such as difficulty level) you'd be better off making a single variable and storing different values in it. It'll save you a lot of headache later on.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 2 guests