View unanswered posts | View active topics It is currently Thu Dec 26, 2024 4:57 pm



Reply to topic  [ 8 posts ] 
 Lua Timers 
Author Message
User avatar

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


Sat Jun 27, 2009 3:24 am
Profile
User avatar

Joined: Tue Nov 06, 2007 6:58 am
Posts: 2054
Reply with quote
Post Re: Lua Timers
Assuming the context is right, yes.


Sat Jun 27, 2009 3:26 am
Profile
User avatar

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


Sat Jun 27, 2009 3:30 am
Profile
User avatar

Joined: Tue Nov 06, 2007 6:58 am
Posts: 2054
Reply with quote
Post Re: Lua Timers
Code:
actor:IsPlayerControlled()

Works.


Sat Jun 27, 2009 3:31 am
Profile
User avatar

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


Sat Jun 27, 2009 3:35 am
Profile
User avatar

Joined: Tue Nov 06, 2007 6:58 am
Posts: 2054
Reply with quote
Post Re: Lua Timers
Try:
Code:
if not(actor:IsPlayerControlled()) then

Anyway, what is this script running on?


Sat Jun 27, 2009 3:41 am
Profile
User avatar

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


Sat Jun 27, 2009 3:44 am
Profile
User avatar

Joined: Mon Jun 30, 2008 9:13 pm
Posts: 499
Location: Finland
Reply with quote
Post 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.


Sat Jun 27, 2009 11:16 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 8 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.051s | 13 Queries | GZIP : Off ]