Page 1 of 1
True False Statments
Posted: Thu Aug 14, 2014 6:28 pm
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.
Re: True False Statments
Posted: Thu Aug 14, 2014 6:56 pm
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.
Re: True False Statments
Posted: Thu Aug 14, 2014 8:36 pm
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.
Re: True False Statments
Posted: Thu Aug 14, 2014 8:41 pm
by Advert
If you mean:
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
...
Re: True False Statments
Posted: Thu Aug 14, 2014 9:03 pm
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
Re: True False Statments
Posted: Fri Aug 15, 2014 4:08 am
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.
Re: True False Statments
Posted: Sat Aug 16, 2014 7:44 am
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.