| Author | Message | 
        
			| ryry1237 
					Joined: Sun Dec 25, 2011 7:23 am
 Posts: 269
   |   How should I optimize my scriptsI'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 | 
					
					   | 
	
	
		|  | 
	
			| Abdul Alhazred DRL Developer 
					Joined: Tue Aug 11, 2009 5:09 am
 Posts: 395
   |   Re: How should I optimize my scriptsNot 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 | 
					
					   | 
	
	
		|  | 
	
			| ryry1237 
					Joined: Sun Dec 25, 2011 7:23 am
 Posts: 269
   |   Re: How should I optimize my scriptsThanks! 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 doif 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 | 
					
					   | 
	
	
		|  | 
	
			| Abdul Alhazred DRL Developer 
					Joined: Tue Aug 11, 2009 5:09 am
 Posts: 395
   |   Re: How should I optimize my scriptsSceneMan: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 | 
					
					   | 
	
	
		|  | 
	
			| ryry1237 
					Joined: Sun Dec 25, 2011 7:23 am
 Posts: 269
   |   Re: How should I optimize my scriptsCode 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 thenWhen 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 | 
					
					   | 
	
	
		|  | 
	
			| Abdul Alhazred DRL Developer 
					Joined: Tue Aug 11, 2009 5:09 am
 Posts: 395
   |   Re: How should I optimize my scriptsThats 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 | 
					
					   | 
	
	
		|  | 
	
			| Zaggy1024 
					Joined: Sun Jan 22, 2012 6:39 am
 Posts: 12
   |   Re: How should I optimize my scriptsI'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 | 
					
					   | 
	
	
		|  | 
	
			| Abdul Alhazred DRL Developer 
					Joined: Tue Aug 11, 2009 5:09 am
 Posts: 395
   |   Re: How should I optimize my scriptsThis is your problem: SceneManagerIn the code all Managers are called just "Man", so it should be SceneMan.
 
 
 | 
		
			| Mon Jan 23, 2012 2:55 pm | 
					
					   | 
	
	
		|  | 
	
	
		|  |