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



Reply to topic  [ 6 posts ] 
 Short circuiting in Lua 
Author Message
User avatar

Joined: Mon Jun 15, 2009 4:02 pm
Posts: 905
Reply with quote
Post Short circuiting in Lua
Hey all.
I have a script as follows:
Code:
for actor in MovableMan.Actors do
   if self.box:WithinBox(actor.Pos) == true then
      self.Health= 0
   else
      self.Health = 100
   end
end

How would I have the script end (for that frame) if the check is found true?

Edit:
I'm not 100% sure that short circuiting is the correct term.
Please inform me if it isn't.


Tue Aug 17, 2010 11:13 pm
Profile WWW
DRL Developer
DRL Developer
User avatar

Joined: Wed Dec 13, 2006 5:27 am
Posts: 3138
Location: A little south and a lot west of Moscow
Reply with quote
Post Re: Short circuiting in Lua
You're looking for the term "break".
Code:
for actor in MovableMan.Actors do
   if self.box:WithinBox(actor.Pos) == true then
      self.Health= 0
      break;
   else
      self.Health = 100
   end
end


Tue Aug 17, 2010 11:52 pm
Profile WWW
User avatar

Joined: Mon Jun 15, 2009 4:02 pm
Posts: 905
Reply with quote
Post Re: Short circuiting in Lua
I'm genuinely shocked that I didn't think of that, thanks!
Also, thanks for reminding me to semicolon properly.

Image

It now works when more than one actor exists, joyfully.


Wed Aug 18, 2010 12:15 am
Profile WWW
User avatar

Joined: Wed May 20, 2009 3:10 pm
Posts: 366
Location: Ã…rhus, Denmark
Reply with quote
Post Re: Short circuiting in Lua
Don't know if it would work better or not, but i would just write the code like this

Code:
for actor in MovableMan.Actors do
   if self.box:WithinBox(actor.Pos) == true then
      self.Health= 0
   end
end


It is shorter and i think your previous code would make everyone's health stay at 100 when they are outside the area


Thu Aug 19, 2010 3:15 pm
Profile
DRL Developer
DRL Developer
User avatar

Joined: Wed Dec 13, 2006 5:27 am
Posts: 3138
Location: A little south and a lot west of Moscow
Reply with quote
Post Re: Short circuiting in Lua
Actually, that is more efficient. However, to have the same effect as his code, don't forget to include this line first:
Code:
self.Health = 100;


Thu Aug 19, 2010 7:18 pm
Profile WWW
User avatar

Joined: Mon Jun 15, 2009 4:02 pm
Posts: 905
Reply with quote
Post Re: Short circuiting in Lua
Code:
function Create(self)
   self.box = Box(Vector(self.Pos.X - 85, self.Pos.Y - 65), Vector(self.Pos.X + 85, self.Pos.Y + 70));
end

function Update(self)
   self.Scale = 1;
   for actor in MovableMan.Actors do
      if self.box:WithinBox(actor.Pos) == true then
         self.Scale = 0.001;
      end
   end
end

Works perfectly.

The self.Health was just because I wasn't sure if I'd want to publicly demonstrate the actual purpose I had in mind.
As it happens, I'm not really bothered. It's an interesting effect, but of questionable usefulness.


Thu Aug 19, 2010 7:29 pm
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 6 posts ] 

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.078s | 14 Queries | GZIP : Off ]