Data Realms Fan Forums
http://45.55.195.193/

How should I optimize my scripts
http://45.55.195.193/viewtopic.php?f=73&t=29104
Page 1 of 1

Author:  ryry1237 [ Sat Jan 07, 2012 8:35 am ]
Post subject:  How should I optimize my scripts

I'm kind of a nooby guy when it comes to coding, but I've learned a lot recently and I'm trying to create a script which slows down the velocity of all nearby bullets.
I've got the script to work and I like the effects, but it tends to lag quite a bit whenever I throw it into the middle of a firefight and I was wondering if there were any ways/tricks/shortcuts to reduce lag while keeping the general effect?

Here's the script:
Code:
function Create(self)
   self.alterdist = 105 --How far the effect can reach
   self.slowitem = 0.6 --How much bullets are slowed
end

function Update(self)

-- for bullets and grenade fragments

   for target in MovableMan.Particles do
     local tavgx = (target.Pos.X - self.Pos.X)
     local tavgy = (target.Pos.Y - self.Pos.Y)
     local tdist = (math.sqrt(tavgx ^ 2 + tavgy ^ 2))
      if tdist <= self.alterdist then
         target.Vel = target.Vel * (1 - self.slowbullet)
         if (target.Vel.Y <= 1) and (target.Vel.Y >= -1)

then
            target.Vel.Y = (SceneMan.GlobalAcc.Y *

TimerMan.DeltaTimeSecs)*-1
         end
      end
   end
end


Also, I've seen some people add ";" to the end of their script lines and others do not.
The script seems to work either way for me, so does anyone know what that ";" symbol does?

Author:  Abdul Alhazred [ Sat Jan 07, 2012 3:50 pm ]
Post subject:  Re: How should I optimize my scripts

Not all that much that can be done to reduce lag since you have to check every particle to find all bullets and the number of particles easily reaches thousands at times. Here are a few things though:

  • Only check every second update, or once every x ms.
  • Only calculate the distance for particles where HitsMOs is true.
  • Use SceneMan:ShortestDistance to calculate distance instead of Pythagoras' theorem, otherwise your code will fail at the map seam.

Semicolons are optional for Lua code, e.i. redundant.

Author:  ryry1237 [ Sun Jan 08, 2012 4:16 am ]
Post subject:  Re: How should I optimize my scripts

Thanks!
Not very sure how to make the code skip update checks without making the effects look weird, but I guess I can implement everything else.
What kind of code syntax should I add so that it only applies to HitMOs = true?

Code:
   for target in MovableMan.Particles do
      if target.HitMOs = true then
carry out the rest of the code

Pretty sure this won't work though

edit: for SceneMan:ShortestDistance, should the syntax be like this?


Code:
The original code

     local avgx = (item.Pos.X - self.Pos.X)
     local avgy = (item.Pos.Y - self.Pos.Y)
     local dist = (math.sqrt(avgx ^ 2 + avgy ^ 2))
      if dist <= self.alterdist then


Code:
SceneMan:ShortestDistance code

     if SceneMan:ShortestDistance(item.Pos,self.Pos) <= self.alterdist then

Author:  Abdul Alhazred [ Sun Jan 08, 2012 10:45 am ]
Post subject:  Re: How should I optimize my scripts

SceneMan:ShortestDistance returns a vector that describe the xy relationship between the two points, so if you want to the scalar distance between these points you simply use the Magnitude property from the Vector class:
Code:
if SceneMan:ShortestDistance(item.Pos, self.Pos, false).Magnitude <= self.alterdist then


Code:
if target.HitMOs = true then
Is a syntax error since you cannot assign values in an if-statement in lua. HitMOs is a boolean so you can check it like this:
Code:
if target.HitMOs then

Author:  ryry1237 [ Sun Jan 08, 2012 2:17 pm ]
Post subject:  Re: How should I optimize my scripts

Code for distance checking works great!
I still have a bit of trouble getting the target.HitMOs to work though
This is what I currently have
Code:
      if SceneMan:ShortestDistance (target.Pos, self.Pos, false).Magnitude <= self.alterdist and target.HitMOs then

When I add in target.HitMOs, the script fails, plus it does funny stuff to the script holding object itself (namely making it disappear before it's supposed to).

Author:  Abdul Alhazred [ Mon Jan 09, 2012 12:29 am ]
Post subject:  Re: How should I optimize my scripts

Thats because I spelled target.HitsMOs wrong! Sorry about that. It should be:
Code:
if target.HitsMOs and SceneMan:ShortestDistance(target.Pos, self.Pos, false).Magnitude <= self.alterdist then

Note that the HitsMOs check is first, otherwise the distance check will be performed on the harmless particles too.

Author:  Zaggy1024 [ Sun Jan 22, 2012 9:36 pm ]
Post subject:  Re: How should I optimize my scripts

I'm trying to modify code in the missile script to use that function (and also lock only onto rockets/dropships), but it keeps giving an error that the parameters I'm giving it are wrong...

Code:
         local curdist = 1000;

         for actor in MovableMan.Actors do
            if (actor.ClassName == "ACDropShip" or actor.ClassName == "ACRocket") and actor.Team ~= self.Team then
               local dist = SceneManager:ShortestDistance(self.Pos, actor.Pos, self.WrapsX).Magnitude;

               if dist < curdist then
                  curdist = dist;
                  self.target = actor;
               end
            end
         end


Any idea what the problem is?

Edit: I forgot to mention that I did define self.WrapsX in Create(self):

Code:
   self.WrapsX = SceneMan.SceneWrapsX;

Author:  Abdul Alhazred [ Mon Jan 23, 2012 2:55 pm ]
Post subject:  Re: How should I optimize my scripts

This is your problem: SceneManager
In the code all Managers are called just "Man", so it should be SceneMan.

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