ID:1948890
 
(See the best response by Ter13.)
Code:
mob/verb/
MOVEBG()
set category = "Test-commands"
set background = 1
var/obj/O = new
var/icon/I=new(icon='Clouds.png')
O.icon=I
src.Add2Screen(O,1,1)
spawn while(1)
I.Shift(EAST,1,1)
O.icon=I
sleep(1)


Problem description:
Hi, i don't why but when i use this code my CPU rise. Where i did issues?


It might just be how much resources it uses, although 1% isn't very much. Maybe the spawn is doing it?
Best response
var/icon/I=new(icon='Clouds.png')
O.icon=I
src.Add2Screen(O,1,1)
spawn while(1)
I.Shift(EAST,1,1)
O.icon=I
sleep(1)


You really want to avoid doing icon operations like this.

If you want to simply scroll the clouds by a single pixel every frame, this can be done without costly icon operations.

The below code is an example of how to do this parallax using two objects without having to use icon procs:

(Merely replace CLOUD_WIDTH with the width in pixels of your cloud image)

//create two objects to be added to the client's screen
var/obj/O1 = new()
O1.icon = 'Clouds.png'
O1.screen_loc = "1,1"
var/obj/O2 = new()
O2.icon = 'Clouds.png'
O2.screen_loc = "1,1"
//construct matrices for use in the looping animation
var/matrix/M1 = matrix(0,1,-CLOUD_WIDTH,0,1,0)
var/matrix/M2 = matrix()
var/matrix/M3 = matrix(0,1,CLOUD_WIDTH,0,1,0)
//perform the animation on both objects to parallax the image
animate(O1,transform=M1,time=0,loop=-1)
animate(transform=M2,time=100)
animate(O2,transform=M2,time=0,loop=-1)
animate(transform=M3,time=100)
//add the objects to the client's screen
client.screen.Add(O1,O2)
Anyway, the reason that it's consuming a lot of resources, is that changing /icon objects is resource intensive and if you do it too much, you will crash out your project very quickly. They should be absolutely avoided for dynamic icons in almost all cases, and client-side alternatives should be used in all cases where at all possible.