Discuss Scratch

turkey3_test
Scratcher
1000+ posts

How many if statements can be utilized before lagging?

I am creating a large RPG and plan on having probably about 200 scrolling plates in the world. However, they'll be somewhat divided into sections, so make that about 50 each world. I know that having 50 sprites all scrolling at once can cause large lag, so what I did is this: I use if statements to determine if the scrolling sprite will be visible, and if it won't, it doesn't make any such movement. Here's what I mean:
when I receive [world v] //generates the world
forever
if <([abs v] of (((480) * (0)) + (scroll x))) < [961]> //if visible
set x to (((480) * (0)) + (scroll x)) //I have y scrolling too but was too lazy to write it
My question is, how many of these repetitive if statements can run before the world begins to lag?
ProdigyZeta7
Scratcher
1000+ posts

How many if statements can be utilized before lagging?

Wouldn't it be easier to just determine the necessary world plates from a list without having to check every single plate? Like this:
*hang on I'm fixing this…*
set [start x v] to ((((scroll x) + (480)) mod (960)) - (480))
set [plate v] to (([floor v] of ((scroll x) / (480))) + (1))
switch to costume (item (plate) of [world plates v])
go to x: ((0) - (start x)) y: (floor)
stamp
if <(start x) > [0]>
go to x: ((480) - (start x)) y: (floor)
switch to costume (item ((plate) + (1)) of [world plates v])
else
go to x: ((-480) - (start x)) y: (floor)
switch to costume (item ((plate) - (1)) of [world plates v])
end
stamp

If you were to use tons of if statements, it would only start lagging when you've reached the flops-per-frame limit of your computer.

Last edited by ProdigyZeta7 (Nov. 3, 2013 02:42:57)

ErnieParke
Scratcher
1000+ posts

How many if statements can be utilized before lagging?

It doesn't depend on the number of if's, but what you have the if's doing. From what I see, a lot of them should be able to be run. I also made a scroller using stamping, with tiles being 30x30, and that ran smoothly. Here, let me give you a shot:



I abandoned it awhile ago, due to how complex the math side of it was getting, but I'm thinking of picking it back up sometime in the future.

Enough intro. What I want to say is that in the project, I had tiles constantly being stamped to form the world, and stamping is more computationally heavy then what you're planning on doing. You should be fine. ;)

Also, I recommend you to look into @ProdigyZeta7's comment.

Assuring,

ErnieParke

Last edited by ErnieParke (Nov. 3, 2013 02:38:03)

turkey3_test
Scratcher
1000+ posts

How many if statements can be utilized before lagging?

ProdigyZeta7 wrote:

Wouldn't it be easier to just determine the necessary world plates from a list without having to check every single plate? Like this:
*hang on I'm fixing this…*
set [start x v] to ((((scroll x) + (480)) mod (960)) - (480))
set [plate v] to (([floor v] of ((scroll x) / (480))) + (1))
switch to costume (item (plate) of [world plates v])
go to x: ((0) - (start x)) y: (floor)
stamp
if <(start x) > [0]>
go to x: ((480) - (start x)) y: (floor)
switch to costume (item ((plate) + (1)) of [world plates v])
else
go to x: ((-480) - (start x)) y: (floor)
switch to costume (item ((plate) - (1)) of [world plates v])
end
stamp

If you were to use tons of if statements, it would only start lagging when you've reached the flops-per-frame limit of your computer.
Except I also have y scrolling, which would make different plates (I'll say backgrounds from now on) for each x interval. Since Ernie's project didn't lag, I think my method will work fine. But thanks for the suggestion and ideas! I may consider them for a future project.
jji7skyline
Scratcher
1000+ posts

How many if statements can be utilized before lagging?

Forever scrips usually induce more lag than if scripts.
scubajerry
Scratcher
1000+ posts

How many if statements can be utilized before lagging?

On my tired, old computer about 8500 simple IFs causes frames per second to drop below 30 when I am in the editor. It goes up to about 15,000 ini the player mode.

http://scratch.mit.edu/projects/13861289/#player

OBTW, I am not sure that answers anything useful to anyone.

Last edited by scubajerry (Nov. 3, 2013 08:44:59)

turkey3_test
Scratcher
1000+ posts

How many if statements can be utilized before lagging?

scubajerry wrote:

On my tired, old computer about 8500 simple IFs causes frames per second to drop below 30 when I am in the editor. It goes up to about 15,000 ini the player mode.

http://scratch.mit.edu/projects/13861289/#player

OBTW, I am not sure that answers anything useful to anyone.
That's a lot of if statements! I'm talking about 100.
jontmy00
Scratcher
500+ posts

How many if statements can be utilized before lagging?

scubajerry wrote:

On my tired, old computer about 8500 simple IFs causes frames per second to drop below 30 when I am in the editor. It goes up to about 15,000 ini the player mode.

http://scratch.mit.edu/projects/13861289/#player

OBTW, I am not sure that answers anything useful to anyone.
Mine (on average; without Turbo):

Player on project page: 253k (31 fps)
Player on project page while idle on another page: 103k (25 fps)
Player in online editor with no scripts in current sprite: 250k (30 fps)
Player in online editor with all the if scripts in current sprite: 240k (30 fps)
Player in online editor with all the if scripts in current sprite while idle on another page: 120k (13 fps)

Using Firefox, one notable thing is that every time without Turbo = 30 fps, with Turbo = 60 fps. Does Turbo really increase fps? It seems to make it more stuttery…
ErnieParke
Scratcher
1000+ posts

How many if statements can be utilized before lagging?

jontmy00 wrote:

scubajerry wrote:

On my tired, old computer about 8500 simple IFs causes frames per second to drop below 30 when I am in the editor. It goes up to about 15,000 ini the player mode.

http://scratch.mit.edu/projects/13861289/#player

OBTW, I am not sure that answers anything useful to anyone.
Mine (on average; without Turbo):

Player on project page: 253k (31 fps)
Player on project page while idle on another page: 103k (25 fps)
Player in online editor with no scripts in current sprite: 250k (30 fps)
Player in online editor with all the if scripts in current sprite: 240k (30 fps)
Player in online editor with all the if scripts in current sprite while idle on another page: 120k (13 fps)

Using Firefox, one notable thing is that every time without Turbo = 30 fps, with Turbo = 60 fps. Does Turbo really increase fps? It seems to make it more stuttery…
It does increase the FPS, for some scripts. I've found that Turbo Mode acts a bit oddly. Yes, it does run everything as fast as possible, but it has preferences in what it runs. So, for example:

when gf clicked
forever
change [variable v] by (1)

when gf clicked
forever
. . .//computationally heavy script

The first script is likely to run two times as often as the second script. That's probably what's happening with your project; the FPS script is being run more often then everything else, so it thinks it has a higher FPS.

By the way, interesting results.

Explaining,

ErnieParke

Last edited by ErnieParke (Nov. 6, 2013 12:12:51)

turkey3_test
Scratcher
1000+ posts

How many if statements can be utilized before lagging?

jontmy00 wrote:

scubajerry wrote:

On my tired, old computer about 8500 simple IFs causes frames per second to drop below 30 when I am in the editor. It goes up to about 15,000 ini the player mode.

http://scratch.mit.edu/projects/13861289/#player

OBTW, I am not sure that answers anything useful to anyone.
Mine (on average; without Turbo):

Player on project page: 253k (31 fps)
Player on project page while idle on another page: 103k (25 fps)
Player in online editor with no scripts in current sprite: 250k (30 fps)
Player in online editor with all the if scripts in current sprite: 240k (30 fps)
Player in online editor with all the if scripts in current sprite while idle on another page: 120k (13 fps)

Using Firefox, one notable thing is that every time without Turbo = 30 fps, with Turbo = 60 fps. Does Turbo really increase fps? It seems to make it more stuttery…
What exactly do you mean by “k”?
scubajerry
Scratcher
1000+ posts

How many if statements can be utilized before lagging?

turkey3_test wrote:

jontmy00 wrote:

scubajerry wrote:

On my tired, old computer about 8500 simple IFs causes frames per second to drop below 30 when I am in the editor. It goes up to about 15,000 ini the player mode.

http://scratch.mit.edu/projects/13861289/#player

OBTW, I am not sure that answers anything useful to anyone.
Mine (on average; without Turbo):

Player on project page: 253k (31 fps)
Player on project page while idle on another page: 103k (25 fps)
Player in online editor with no scripts in current sprite: 250k (30 fps)
Player in online editor with all the if scripts in current sprite: 240k (30 fps)
Player in online editor with all the if scripts in current sprite while idle on another page: 120k (13 fps)

Using Firefox, one notable thing is that every time without Turbo = 30 fps, with Turbo = 60 fps. Does Turbo really increase fps? It seems to make it more stuttery…
What exactly do you mean by “k”?
1,000
jontmy00
Scratcher
500+ posts

How many if statements can be utilized before lagging?

scubajerry wrote:

turkey3_test wrote:

jontmy00 wrote:

scubajerry wrote:

-delete-
-delete-
1,000
Yes, 1000.

Last edited by jontmy00 (Nov. 6, 2013 07:20:58)

Powered by DjangoBB