View unanswered posts | View active topics It is currently Fri Dec 27, 2024 10:01 am



Reply to topic  [ 19 posts ]  Go to page 1, 2  Next
 Multiple 'else' in one 'if'? 
Author Message
User avatar

Joined: Wed Nov 22, 2006 3:19 pm
Posts: 2073
Reply with quote
Post 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
Profile
User avatar

Joined: Sun May 11, 2008 12:50 pm
Posts: 899
Reply with quote
Post 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
Profile WWW
User avatar

Joined: Wed Nov 22, 2006 3:19 pm
Posts: 2073
Reply with quote
Post 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
Profile
User avatar

Joined: Thu Mar 06, 2008 10:54 pm
Posts: 1360
Location: USA
Reply with quote
Post 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
Profile
User avatar

Joined: Wed Nov 22, 2006 3:19 pm
Posts: 2073
Reply with quote
Post 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
Profile
User avatar

Joined: Thu Mar 06, 2008 10:54 pm
Posts: 1360
Location: USA
Reply with quote
Post 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
Profile
User avatar

Joined: Wed Nov 22, 2006 3:19 pm
Posts: 2073
Reply with quote
Post 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
Profile
User avatar

Joined: Thu Mar 06, 2008 10:54 pm
Posts: 1360
Location: USA
Reply with quote
Post 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
Profile
REAL AMERICAN HERO
User avatar

Joined: Sat Jan 27, 2007 10:25 pm
Posts: 5655
Reply with quote
Post 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
Profile
User avatar

Joined: Wed Nov 22, 2006 3:19 pm
Posts: 2073
Reply with quote
Post 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
Profile
User avatar

Joined: Sun Jul 13, 2008 9:57 am
Posts: 4886
Location: some compy
Reply with quote
Post 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
Profile WWW
User avatar

Joined: Wed Nov 22, 2006 3:19 pm
Posts: 2073
Reply with quote
Post 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
Profile
REAL AMERICAN HERO
User avatar

Joined: Sat Jan 27, 2007 10:25 pm
Posts: 5655
Reply with quote
Post 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
Profile
User avatar

Joined: Wed Nov 22, 2006 3:19 pm
Posts: 2073
Reply with quote
Post 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!
Image

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
Profile
REAL AMERICAN HERO
User avatar

Joined: Sat Jan 27, 2007 10:25 pm
Posts: 5655
Reply with quote
Post Re: Multiple 'else' in one 'if'?
self.Frame = 1;
else
self.Frame = 0;


Sat Aug 08, 2009 12:55 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 19 posts ]  Go to page 1, 2  Next

Who is online

Users browsing this forum: No registered users


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.
[ Time : 0.104s | 14 Queries | GZIP : Off ]