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



Reply to topic  [ 8 posts ] 
 How should I optimize my scripts 
Author Message
User avatar

Joined: Sun Dec 25, 2011 7:23 am
Posts: 269
Reply with quote
Post 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?


Last edited by ryry1237 on Sun Jan 08, 2012 4:21 am, edited 3 times in total.



Sat Jan 07, 2012 8:35 am
Profile
DRL Developer
DRL Developer

Joined: Tue Aug 11, 2009 5:09 am
Posts: 395
Reply with quote
Post 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.


Sat Jan 07, 2012 3:50 pm
Profile
User avatar

Joined: Sun Dec 25, 2011 7:23 am
Posts: 269
Reply with quote
Post 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


Sun Jan 08, 2012 4:16 am
Profile
DRL Developer
DRL Developer

Joined: Tue Aug 11, 2009 5:09 am
Posts: 395
Reply with quote
Post 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


Sun Jan 08, 2012 10:45 am
Profile
User avatar

Joined: Sun Dec 25, 2011 7:23 am
Posts: 269
Reply with quote
Post 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).


Sun Jan 08, 2012 2:17 pm
Profile
DRL Developer
DRL Developer

Joined: Tue Aug 11, 2009 5:09 am
Posts: 395
Reply with quote
Post 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.


Mon Jan 09, 2012 12:29 am
Profile

Joined: Sun Jan 22, 2012 6:39 am
Posts: 12
Reply with quote
Post 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;


Sun Jan 22, 2012 9:36 pm
Profile
DRL Developer
DRL Developer

Joined: Tue Aug 11, 2009 5:09 am
Posts: 395
Reply with quote
Post 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.


Mon Jan 23, 2012 2:55 pm
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.128s | 15 Queries | GZIP : Off ]