ID:2161878
 
(See the best response by Popisfizzy.)
Can anyone help with a more efficient gps system? This looping kills cpu.
spawn()if(client){updateGPS();var/atom/O=target_atom;if(O){if(get_dist(src,O)<=gps_radius){target_atom=0;src<<gps_notification_entered}};O=target_atom;if(O){for(var/GPS/pointer/a in client.screen){a.icon='gps.dmi';animate(a, transform = turn(matrix(), set_angle(x,y,O.x,O.y)), time = 6, loop = 1)}};else{for(var/GPS/pointer/a in client.screen){a.icon=null}}}


Best response
Why the fuck would you put it in a single line like that. Make it readable before you ask for help.
In response to Popisfizzy
I love your passion
I spent a minute replacing the braces and semicolons with equivalent whitespace. FYI, it runs no less efficient since it's compiled into the same bytecode either way.
spawn
if(client)
updateGPS()
var/atom/O = target_atom
if(O)
if(get_dist(src, O) <= gps_radius)
target_atom = 0
src << gps_notification_entered
O = target_atom
if(O)
for(var/GPS/pointer/a in client.screen)
a.icon = 'gps.dmi'
animate(a, transform = turn(matrix(), set_angle(x,y,O.x,O.y)), time = 6, loop = 1)
else
for(var/GPS/pointer/a in client.screen)
a.icon = null

The only "looping" visible here is the two for() loops over every /GPS/pointer in client.screen. If there's only ever one of those, you should just keep a reference to it. Either way, it's not going to be the source of your CPU usage issues.

You could at least show the code you're actually talking about. There's nothing particularly CPU-heavy about the code you've shown, but if you're calling it infinite times without any time between them, it doesn't matter what code you have; it's going to kill the CPU. For example:
mob/verb/freeze_game() spawn .()
In response to Kaiochao
Sorry about that. This is what is called when a mob logs into the game. I'm getting a ridiculous amount of calls for updateGPS().
GPS()
if(src&&client&&!gps_on)
gps_on=1
var {GPS/pointer/b = new}; client.screen += b;updateGPS()
In response to Mav472
If you're getting a ridiculous amount of calls to updateGPS(), maybe you should look at all the places that call updateGPS().