View unanswered posts | View active topics It is currently Thu Oct 31, 2024 11:57 pm



Reply to topic  [ 15 posts ] 
 Can't get timers to work 
Author Message
User avatar

Joined: Sun Feb 21, 2010 10:40 pm
Posts: 97
Reply with quote
Post Can't get timers to work
Well, I've looked around at other lua's and I've looked around at some tutorials trying to figure out how to make timers, I was pretty sure I did it right, there was no errors or anything.

This script is currently applied to a MOSRotating that is shot out of a cannon that I made to counter reflective stuff.

Code:
function Create(self)
   self.timeVar = Timer()
end
function Update(self)
   if self.timeVar > 20 then
      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))
         if diff < 200 then
            actor.Vel = self.Vel
            actor.Health = actor.Health - 75
            self.GibThis()
         end
      end
   end
end


Without the timer, it works perfect... But my actor gibs immediately and goes flying across the screen when I use it. That's why I tried adding a timer, but when I add a timer, the whole thing just stops working. I've even tried setting it so that when self.timeVar is greater than zero, the script activates, but still nothing.

What am I doing wrong that prevents this timer from working correctly?


Sun Jun 27, 2010 9:58 pm
Profile
User avatar

Joined: Tue Jun 12, 2007 11:52 pm
Posts: 13143
Location: Here
Reply with quote
Post Re: Can't get timers to work
Some timer info:

To create a timer, make a variable and set it to the timer function:
Code:
self.animtimer = Timer()


To reset the timer's count-up, use the reset function on the timer.
Code:
self.animtimer:Reset()


To see if the timer is past a specific time (in MS)
Code:
if self.animtimer:IsPastSimMS(<time>) then


And to check the current time on the timer.
Code:
self.animtimer.ElapsedSimTimeMS


Sun Jun 27, 2010 10:00 pm
Profile
User avatar

Joined: Mon Jun 15, 2009 4:02 pm
Posts: 905
Reply with quote
Post Re: Can't get timers to work
Replace self.timeVar > 20 with self.timeVar:IsPastSimMS(20). It is in milliseconds, so you probably want to increase that a bit.
Also, self.GibThis() ought to be self:GibThis().

Edit:
Ninja'd, somewhat.


Sun Jun 27, 2010 10:02 pm
Profile WWW
User avatar

Joined: Sun Feb 21, 2010 10:40 pm
Posts: 97
Reply with quote
Post Re: Can't get timers to work
Wow, thanks for that fast reply!

I see what I did wrong now, I should have used IsPastSimMS instead of >.

@Petethegoat
Yah, but I have it at 20 because the bullet is shot extremely fast so it should be a good enough distance away from you by then.

And I have always used self.GibThis(), is it just improper to use a . in place of a : ?


Sun Jun 27, 2010 10:03 pm
Profile
User avatar

Joined: Mon Jun 15, 2009 4:02 pm
Posts: 905
Reply with quote
Post Re: Can't get timers to work
When you use GibThis(), you're calling a function, not a variable.
It has no arguments so the () should always be empty, and just gibs whatever is passed to it.

So self:GibThis() would gib self, logically enough.
The main point is that it is a function, and not a variable, so you should use a colon.


Sun Jun 27, 2010 10:19 pm
Profile WWW
REAL AMERICAN HERO
User avatar

Joined: Sat Jan 27, 2007 10:25 pm
Posts: 5655
Reply with quote
Post Re: Can't get timers to work
To clarify that a bit more:

A period indicates that something is either a property or a variable. In general, a period indicates either read only or read/write access to some kind of stored value.

EG: self.Mass is a property of self, stored as a number variable.
self.timer would be, technically, a pointer to the timer, stored as a reference from self.

Functions, however, do something, rather than just reading or writing to stored variables.

self:GibThis() is clearly differentiated as a function by the colon and the parenthesis (which house arguments on more complicated functions)

It's good form syntactically, but it's also explicitly necessary. Semicolons on the end of most code blocks are good form, but not necessary, in case you weren't sure.

anyways yeah you seem to have a pretty good grasp of lua already so, carry on!


Sun Jun 27, 2010 10:29 pm
Profile
User avatar

Joined: Sun Feb 21, 2010 10:40 pm
Posts: 97
Reply with quote
Post Re: Can't get timers to work
Yah, I know a lot of actionscript and they seem real similar, so that makes it easier.

The ;'s I already know about, however, . and : I did not. Using actor.GibThis() was literally working without error until I was told about it, now it gives me function errors. :P

I can't seem to get self:GibThis() to gib the MOSRotating, though. I am testing this particularly on the Reflective Robot and I am trying to make it so that it gibs before it has a chance to reflect it, exploding, sending it flying. But instead I just goes flying and the shell comes right back at me and explodes, sending my actor rocketing around the map until it hits something and gibs.

Are MOSRotatings not able to be gibbed? Do I have to change it to something else like a TDExplosive? I was hoping to not have to use a TDExplosive.


Sun Jun 27, 2010 10:48 pm
Profile
User avatar

Joined: Mon Jun 15, 2009 4:02 pm
Posts: 905
Reply with quote
Post Re: Can't get timers to work
MOSRotating should gib fine.
Are you getting any errors in the Lua console?


Sun Jun 27, 2010 10:54 pm
Profile WWW
User avatar

Joined: Sun Feb 21, 2010 10:40 pm
Posts: 97
Reply with quote
Post Re: Can't get timers to work
Nope, and I checked the Reflective Robots reflection radius... It's 10 whole pixels. My scripts radius is (now) 50 pixels. That doesn't make any sense.

I have even tried lowering the MOSRotatings velocity to 0 in both vectors when the script runs, but the same problem occurs and it gets reflected back at the same lightning speed it was shot with.


Sun Jun 27, 2010 11:05 pm
Profile
User avatar

Joined: Mon Jun 15, 2009 4:02 pm
Posts: 905
Reply with quote
Post Re: Can't get timers to work
Can we see the script?


Sun Jun 27, 2010 11:07 pm
Profile WWW
User avatar

Joined: Sun Feb 21, 2010 10:40 pm
Posts: 97
Reply with quote
Post Re: Can't get timers to work
Code:
function Create(self)
   self.timeVar = Timer()
end
function Update(self)
   if self.timeVar:IsPastSimMS(20) then
      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))
         if diff < 50 then
            actor.Vel = self.Vel
            actor.Health = actor.Health - 20
            self:GibThis()
         end
      end
   end
end


Pretty much the same as last time. Just made a few minor changes.


Sun Jun 27, 2010 11:16 pm
Profile
User avatar

Joined: Mon Jun 15, 2009 4:02 pm
Posts: 905
Reply with quote
Post Re: Can't get timers to work
Try commenting out the diff check and see if it works.
If not, add prints everywhere to see exactly what it is stopping at.


Sun Jun 27, 2010 11:37 pm
Profile WWW
User avatar

Joined: Sun Feb 21, 2010 10:40 pm
Posts: 97
Reply with quote
Post Re: Can't get timers to work
Nope, doesn't work. I also don't know how to add a trace function in lua. Would it be trace("message"), or echo("message"), or something like that?


Mon Jun 28, 2010 1:42 am
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: Can't get timers to work
print("message") is what you're looking for.


Mon Jun 28, 2010 1:56 am
Profile WWW
User avatar

Joined: Sun Feb 21, 2010 10:40 pm
Posts: 97
Reply with quote
Post Re: Can't get timers to work
OK, thanks.

Well, I did that, and added a print("") under each line in the script. This is what I got:

Image

The MOSRotating should be gibbing.

EDIT: Actually, it does. I just tested it on a normal actor for the first time and it works as it should. It's a Reflective Robot problem. I guess I'll just have to learn to live with it, although I sure wish I knew how to fix it.


Mon Jun 28, 2010 2:38 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 15 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.076s | 15 Queries | GZIP : Off ]