Data Realms Fan Forums
http://45.55.195.193/

Lua Timers
http://45.55.195.193/viewtopic.php?f=73&t=15585
Page 1 of 1

Author:  Mind [ Sat Jun 27, 2009 3:24 am ]
Post subject:  Lua Timers

Just a quick question: I'm trying to create a script that creates something at an interval (ignore diff) if the distance is less than a certain number. Would this work theoretically?

Code:
   if (diff < 100) then
      self.timer2 = Timer();
      if self.timer2:IsPastRealMS(300) then
         blah blah
         self.timer2:Reset();
      end
   end

Author:  mail2345 [ Sat Jun 27, 2009 3:26 am ]
Post subject:  Re: Lua Timers

Assuming the context is right, yes.

Author:  Mind [ Sat Jun 27, 2009 3:30 am ]
Post subject:  Re: Lua Timers

Thanks, mail :) One last thing: How do make something happen only if that object is being selected as an actor ( How do you make an action happen only when you're actually controlling that actor)

Author:  mail2345 [ Sat Jun 27, 2009 3:31 am ]
Post subject:  Re: Lua Timers

Code:
actor:IsPlayerControlled()

Works.

Author:  Mind [ Sat Jun 27, 2009 3:35 am ]
Post subject:  Re: Lua Timers

Okay, and if i wanted to make something happen only when the player isn't controlled then i would do:

Code:
if actor:IsPlayerControlled() == false then
?

Edit: I'm trying this code and it won't work:

Code:
      if self:IsPlayerControlled() == false then
         self.Vel.Y = 0;
         self.Vel.X = 0;
      end

Author:  mail2345 [ Sat Jun 27, 2009 3:41 am ]
Post subject:  Re: Lua Timers

Try:
Code:
if not(actor:IsPlayerControlled()) then

Anyway, what is this script running on?

Author:  Mind [ Sat Jun 27, 2009 3:44 am ]
Post subject:  Re: Lua Timers

I'm really dumb, and got it working, but i hope this is the last thing: for some reason, i have this script:

Code:
   for actor in MovableMan.Actors do   
      local diff = math.sqrt(math.pow((actor.Pos.X-self.Pos.X),2) + math.pow((actor.Pos.Y - self.Pos.Y),2))
      local diffx = self.Pos.X - actor.Pos.X;
      local diffy = self.Pos.Y - actor.Pos.Y;
      local ang = math.atan2(diffy,diffx);
      if (diff < 100) then
         self.timer2 = Timer();
           if self.timer2:IsPastRealMS(300) then
            blah blah            
             self.timer2:Reset();
         end
      end
   end


For some reason, the blah blah is not working :P (I know nothing is wrong with it) I don't know if this is my fault or what, but it is running on an ACDropship.


Edit: Don't worry about tabbing.

Edit2: I tried even printing when it happened, and it still won't work :|

Author:  piipu [ Sat Jun 27, 2009 11:16 am ]
Post subject:  Re: Lua Timers

Code:
self.timer2 = Timer();
if self.timer2:IsPastRealMS(300) then
This is not a good plan. You first reset the timer, then check if 300 ms has passed. Move the self.timer2 = Timer() to the create function.
Also, to make a bit of optimisation you'll want to replace
Code:
      local diff = math.sqrt(math.pow((actor.Pos.X-self.Pos.X),2) + math.pow((actor.Pos.Y - self.Pos.Y),2))
      local diffx = self.Pos.X - actor.Pos.X;
      local diffy = self.Pos.Y - actor.Pos.Y;
      local ang = math.atan2(diffy,diffx);
      if (diff < 100) then
with
Code:
      local diff = actor.Pos - self.Pos
      local ang = diff.AbsRadAngle
      if (diff.Magnitude < 100) then
Alsoalso, if you don't use the angle, you'll want to drop the local ang = diff.AbsRadAngle line. If you need diffx or diffy, use diff.X and diff.Y.

Page 1 of 1 All times are UTC [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/