True False Statments
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
True False Statments
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.
- Jasoco
- Inner party member
- Posts: 3726
- Joined: Mon Jun 22, 2009 9:35 am
- Location: Pennsylvania, USA
- Contact:
Re: True False Statments
Not sure what you mean, but I'll guess:
Maybe something like this?
Or even simpler:
Maybe?
You can also use or instead of and and parentheses around parts of that for finer control, like so:
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.
Maybe something like this?
Code: Select all
if var1 == true and var2 == false and var3 == false and var4 == false then
Code: Select all
if var1 and not var2 and not var3 and not var4 then
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
Experiment.
Re: True False Statments
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
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:
Output:
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;
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()
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
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
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
Generally speaking, when you have a variable that changes when another variable changes then you want to combine them together.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.
So instead of booleans you may want to use numbers or string. For example:
Code: Select all
tall = false
medium = false
short = true
Code: Select all
height = 'short'
-- tall = (height == 'tall')
Re: True False Statments
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.
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.
Who is online
Users browsing this forum: Amazon [Bot], Bing [Bot], Google [Bot] and 2 guests