Author |
Message |
MaximDude
Joined: Wed Nov 22, 2006 3:19 pm Posts: 2073
|
Multiple 'else' in one 'if'?
Well, lua is very similar to C, so naturally, I should be able to do this: Code: if blah blah blah == blah then blah blah blah; else blah blah blah; else if blah blah blah and blah blah blah ~= blah blah blah then blah blah blah; end end
But... I can't. I'm getting an error saying: 'end' expected (to close 'if' at line blah) near 'else' So, i'm asking if it's possible or not... Doing one 'if+else' and another 'if' does not seem to work like I want it to (More like, it doesn't at all.).
|
Fri Aug 07, 2009 4:52 pm |
|
|
Roon3
Joined: Sun May 11, 2008 12:50 pm Posts: 899
|
Re: Multiple 'else' in one 'if'?
Those would be elseif, you can't have two elses. Else is triggered when none of the ifs are true.
|
Fri Aug 07, 2009 4:57 pm |
|
|
MaximDude
Joined: Wed Nov 22, 2006 3:19 pm Posts: 2073
|
Re: Multiple 'else' in one 'if'?
Roon3 wrote: Those would be elseif, you can't have two elses. Else is triggered when none of the ifs are true. Well, I have a proximity check that returns 'Enemy = 1' when an actor of the opposite team if found, and 'Enemy = 0' when an actor from it's own team if found. I need the extra 'else' so it will return 'Enemy = -1' if there are no actors found (when Enemy = 1 and Enemy = 0 are false).
|
Fri Aug 07, 2009 5:23 pm |
|
|
Mind
Joined: Thu Mar 06, 2008 10:54 pm Posts: 1360 Location: USA
|
Re: Multiple 'else' in one 'if'?
Code: if actor.Team ~= self.Team then Enemy = 1 elseif actor.Team == self.Team then Enemy = 0 else Enemy = -1 end If that's what you mean. :3 Basically, it's saying if the actor's team is its own team set enemy to 1, and if that's not true and the actor's team is NOT it's own team set enemy to 0. If none of that is true, set enemy to -1. I think.
Last edited by Mind on Fri Aug 07, 2009 6:18 pm, edited 1 time in total.
|
Fri Aug 07, 2009 5:54 pm |
|
|
MaximDude
Joined: Wed Nov 22, 2006 3:19 pm Posts: 2073
|
Re: Multiple 'else' in one 'if'?
Mind wrote: Code: if actor.Team ~= self.Team then Enemy = 1 elseif actor.Team == self.Team then Enemy = 0 else Enemy = -1 end If that's what you mean. :3 That works, but not quite. There are no errors, the emission gets enabled when an enemy is present, disabled when a friendly is present, but it doesn't get enabled if there is no one around. Like, if a friendly passes through it, it will stay off untill an enemy gets in range. That is the problem here.
|
Fri Aug 07, 2009 6:16 pm |
|
|
Mind
Joined: Thu Mar 06, 2008 10:54 pm Posts: 1360 Location: USA
|
Re: Multiple 'else' in one 'if'?
Um so you want it to always be enabled UNLESS there is an ally present, correct? If so, maybe something like Code: if Enemy == 1 then self:EnableEmission(true); end if Enemy == 0 then self:EnableEmission(false); end if Enemy == -1 then self:EnableEmission(true); end
That should work in essence. :3
|
Fri Aug 07, 2009 6:23 pm |
|
|
MaximDude
Joined: Wed Nov 22, 2006 3:19 pm Posts: 2073
|
Re: Multiple 'else' in one 'if'?
Mind wrote: Um so you want it to always be enabled UNLESS there is an ally present, correct? If so, maybe something like Code: if Enemy == 1 then self:EnableEmission(true); end if Enemy == 0 then self:EnableEmission(false); end if Enemy == -1 then self:EnableEmission(true); end
That should work in essence. :3 Yes, and I already tried it that way. Its no go.
|
Fri Aug 07, 2009 6:42 pm |
|
|
Mind
Joined: Thu Mar 06, 2008 10:54 pm Posts: 1360 Location: USA
|
Re: Multiple 'else' in one 'if'?
Code: if Enemy == 1 then self:EnableEmission(true); elseif Enemy == 0 then self:EnableEmission(false); else self:EnableEmission(true); end
Maybe maybe that. Idk Code: if Enemy == 0 then self:EnableEmission(false); else self:EnableEmission(true); end Or prolly that
|
Fri Aug 07, 2009 6:45 pm |
|
|
Grif
REAL AMERICAN HERO
Joined: Sat Jan 27, 2007 10:25 pm Posts: 5655
|
Re: Multiple 'else' in one 'if'?
ITT people fail at logic 101. Also, if you can do that in C (which I doubt) then you've got a very idiot-proof compiler.
to break it down, according to your description: if enemy then if enemy.Team ~= self.Team then hasenemy = 1; else hasfriendly = 1; end else hasenemy = 0; hasfriendly = 0; end
The first line checks whether enemy exists; assuming you've got a for loop proximity check setting a pointer as the last statement (likely) then that pointer will be nil, which will lead to the last else being done.
|
Fri Aug 07, 2009 9:44 pm |
|
|
MaximDude
Joined: Wed Nov 22, 2006 3:19 pm Posts: 2073
|
Re: Multiple 'else' in one 'if'?
Grif wrote: ITT people fail at logic 101. Also, if you can do that in C (which I doubt) then you've got a very idiot-proof compiler.
to break it down, according to your description: if enemy then if enemy.Team ~= self.Team then hasenemy = 1; else hasfriendly = 1; end else hasenemy = 0; hasfriendly = 0; end
The first line checks whether enemy exists; assuming you've got a for loop proximity check setting a pointer as the last statement (likely) then that pointer will be nil, which will lead to the last else being done. I haven't used C for a while, so i'm not quite sure I got it right, but yeah, I am in fact using a VERY idiot proof compiler. >____________>' I'll try that. Edit: OH! I see, I forgot the structure Its like Code: if (blah blah blah) then blah { else blah; else blah; }
And so on. Or something like that... If not, then there is no wonder I failed my final exam >______________>' Edit 2: I tried in several different ways. Doesn't work. This is what I got now, see if you can find anything wrong.
|
Fri Aug 07, 2009 10:13 pm |
|
|
Geti
Joined: Sun Jul 13, 2008 9:57 am Posts: 4886 Location: some compy
|
Re: Multiple 'else' in one 'if'?
one thing that sucks is your proximity checks. Code: if (actor.Pos.X >= self.Pos.X - 50) and (actor.Pos.X <= self.Pos.X + 50) and (actor.Pos.Y >= self.Pos.Y - 45) and (actor.Pos.Y <= self.Pos.Y + 45) then can be replaced with Code: if (actor.Pos - self.Pos).Magnitude < 50 then also why are you checking "if actor" at some point. you're iterating through MovableMan.Actors, there arent going to be particles in there. and that check is wrong anyway, it should be if actor:IsActor(), or else you're just checking if "actor" equates to not false and not nil. you are also declaring like a million global variables. Ally = 1, for example, will be read by all instances checking for it, not just this laser thing. self.Ally = 1 or local Ally = 1, please. fix this up so the code is like a billion times more logical and independent, and then come back.
|
Fri Aug 07, 2009 11:28 pm |
|
|
MaximDude
Joined: Wed Nov 22, 2006 3:19 pm Posts: 2073
|
Re: Multiple 'else' in one 'if'?
Geti wrote: one thing that sucks is your proximity checks. Code: if (actor.Pos.X >= self.Pos.X - 50) and (actor.Pos.X <= self.Pos.X + 50) and (actor.Pos.Y >= self.Pos.Y - 45) and (actor.Pos.Y <= self.Pos.Y + 45) then can be replaced with Code: if (actor.Pos - self.Pos).Magnitude < 50 then also why are you checking "if actor" at some point. you're iterating through MovableMan.Actors, there arent going to be particles in there. and that check is wrong anyway, it should be if actor:IsActor(), or else you're just checking if "actor" equates to not false and not nil. you are also declaring like a million global variables. Ally = 1, for example, will be read by all instances checking for it, not just this laser thing. self.Ally = 1 or local Ally = 1, please. fix this up so the code is like a billion times more logical and independent, and then come back. Why am I using that? Cause I need a goddamn box, and not a circle. This thing is a bunker module, a laser door/barrier to be precise. Also, I have not fully mastered the basics of lua, so i'm having troubles with this kind of more 'complex' stuff... Will fix though. Edit: Moar leik dis?
|
Fri Aug 07, 2009 11:41 pm |
|
|
Grif
REAL AMERICAN HERO
Joined: Sat Jan 27, 2007 10:25 pm Posts: 5655
|
Re: Multiple 'else' in one 'if'?
self.box = Box(Vector(self.Pos.X - whateverx,self.Pos.Y - whatevery),Vector(self.Pos.X + whateverx,self.Pos.Y + whatevery); for actor in MovableMan.Actors do if self.box:WithinBox(actor.Pos) == true then do ♥♥♥♥
|
Sat Aug 08, 2009 12:04 am |
|
|
MaximDude
Joined: Wed Nov 22, 2006 3:19 pm Posts: 2073
|
Re: Multiple 'else' in one 'if'?
Grif wrote: self.box = Box(Vector(self.Pos.X - whateverx,self.Pos.Y - whatevery),Vector(self.Pos.X + whateverx,self.Pos.Y + whatevery); for actor in MovableMan.Actors do if self.box:WithinBox(actor.Pos) == true then do ♥♥♥♥ ♥♥♥♥ yeah! It works! Thanks for all the help guys. :D Now, I have nother question. (I don't feel like making another thread...)The little thing you see between the lasers is the sensor. What do I use to set a certain frame on for every proximity check? Like, if there is a friendly, I want it to have 'frame000' and when there is an enemy, 'frame001'. Edit: FFFFFFFUUU--- Stupid PhotoBucket resized my gif! D:< Well, you can still see it works...
|
Sat Aug 08, 2009 12:27 am |
|
|
Grif
REAL AMERICAN HERO
Joined: Sat Jan 27, 2007 10:25 pm Posts: 5655
|
Re: Multiple 'else' in one 'if'?
self.Frame = 1; else self.Frame = 0;
|
Sat Aug 08, 2009 12:55 am |
|
|
|