if self.enter and (self.scriptno==1 and (not self.w or not self.a) or (self.s or self.d)) then
This code will only run if
self.enter = true,
and
self.scriptno = 1,
and
self.w = false OR self. a = false OR self.s = true OR self.d = true
Right?? Sorry, it is very difficult for me to check this in-game and I need another human to help me!
What you probably mean is that if statement is true when all those conditions are true. I am not sure if there are good reasons for not splitting that up, maybe there would be a difference, but I would think it could be written as:
if self.enter then
if self.scriptno == 1 then
if (not self.w or not self.a) or (self.s or self.d) then
-- do stuff
end
end
end
1 Like
Yes, that does makes more sense. I am just curious to know if both are the same
1 Like
Actually, @psychotropicdog, that is not correct. In Lua, and has precedence over or, so instead of:
self.scriptno == 1 and ((not self.w or not self.a) or (self.s or self.d))
(which is the equivalent of your above code), what @88.josh posted is rather evaluated as:
(self.scriptno == 1 and (not self.w or not self.a)) or (self.s or self.d)
1 Like
@88.josh if you intended that self.scriptno == 1 and stuff1 or stuff2
structure to be a ternary, it will not work as intended if stuff1
evaluates as false
(that is the caveat of using cond and a or b
as a ternary), which in your case is a boolean expression (not self.w or not self.a
), which might as well evaluate as false. You should break that up and use ifs. It should also be clearer what you meant this way.
2 Likes
This! The Lua ternary thing with and
and or
is only good for short and easily readable pieces of code. The code in this post is in my opinion too complex and much better suited for a couple of nested conditionals.
1 Like
yes, I have nested it now. It was also so confusing 11 hours ago. It is crunch time.
You can also do:
local first_script = self.scriptno == 1
local my_condition = (not self.w or not self.a) or (self.s or self.d)
if self.enter and first_script and my_condition then
-- do stuff
end
2 Likes
I want highlight this too, since when an expression becomes too confusing, it’s usually good to break it down in to smaller chunks, so it’s easier to reason about.
And sometimes, if the expression is just a variable, maybe the actual name of the variable is incorrect!
3 Likes