My code is too slow [SOLVED]

A

Ash Ketchum

Guest
If i have too many of the turrets with this code in, i get serious lag.
But i want to have lots of turrets.

Step
Code:
direction = image_angle
/////
if rounds < 1
{
reloading = 1
}
if reloading = 1
{
reloading_time += reload_speed/50;
if reloading_time > 100
{
rounds = clip_size;
reloading = 0
reloading_time = 0
}
}


///////////////

if (!collision_line(x,y,target.x,target.y,obj_solid,0,0))and distance_to_object(target) < view and ammunition > 0
{
if fuel > 0 and ammunition > 0
   { if system = 1 or 2   
    dir = point_direction(x,y,target.x,target.y)
    image_angle += median(-pivot/350, pivot/350, ((((dir - image_angle) mod 360) + 540) mod 360) - 180);
   }
  
/////
if system = 1 or 2
{
playerDir = point_direction(x,y,target.x,target.y)
}
else
{
playerDir = point_direction(x,y,mytarget.x,mytarget.y)
}
if    (playerDir>direction-FOV && playerDir<direction+FOV)^^(playerDir+360>direction-FOV && playerDir+360<direction+FOV)
      ^^(playerDir-360>direction-FOV && playerDir-360<direction+FOV)
{
if rounds > 0 and reloading = 0 and ammunition > 0
{
time+=ROF
if time >1000
{
barrel.sprite_index = BarrelS_spr image_speed = 0.01
time = 0
var length,;
length = barrel_length

 
 //////////////ONE
for (i = 0; i < gauge; i += 1)
 {
   lead = instance_create(x+lengthdir_x(length,direction),y+lengthdir_y(length,direction),ammo) 
   lead.direction = direction+ random(spread)-spread/2;
   lead.image_angle = direction
   lead.damage = damage
   lead.speed = bulletspeed
   lead.friendly = 1
   lead.parent = id
   rounds -=1
   ammunition -=1
   }
}}}}
else
{
   if time > 0
   {
   time -= ROF/4
   }
   if fuel > 0
           { 
           dir = point_direction(x,y,mytarget.x,mytarget.y)
           image_angle += median(-pivot/350, pivot/350, ((((dir - image_angle) mod 360) + 540) mod 360) - 180);
           }
}
if direction <> point_direction(x,y,mytarget.x,mytarget.y)
{
fuel-=fuel_consumption/500
}
Any thoughts on making it faster?
 
A

anomalous

Guest
Yes. Start by trying out the built in profiler (debugger).
Use "average time" (the t with an underscore, click it), and see what's causing you slow downs in that script. It should be able to help you pinpoint where to spend your energy, and where not spend it.
You also have to defined "many turrets". 20? 100? 1000?

Also, if you use objects for bullets, correct me if I'm wrong but the number of bullet objects may far exceed the number of turrets, so should you be optimizing the bullet or the turret, or both, etc.

It could just be draw call for too many bullets, for example, and not your "code" at all.
 
A

Ash Ketchum

Guest
Thank you. you were right.
I forgot that my bullets had step events to fade the sound out equal to distance. very heavy on memory.
runs smooth now thx :)
 
Top