How to reduce lag? Resolved


#1

Hey everyone! I’m creating a new game with physics and all kinds of complicated stuff. It’s modeled after the game Klask. However, sometimes the physics don’t work and the ball glitches and won’t move. I need some tips for reducing lag. What should I do? Here’s the teaser thing for the project:


#2

Teaser:
https://c.gethopscotch.com/p/zz8wcjdg1


#3

I might have to see the WIP for me to help…Is this Arctan ball physics?


#4

Yes it is arctan physics.


#5

Like @TheDeliciousOrange, it would be helpful if you posted your code. You can publish the project, copy a link to it (you can find the link by tapping on the “share” icon once your project is published) and then unpublish it. That will only make it accessible to people that have a link to the project.

It would really help us to help you! :slight_smile:


#6

Ok. I will do it when I have a chance. Thanks!


#7

Why was I notified of this?

Edit I mean like a notification?
image


#8

Because I invited you to this.


#9

@William04GamerA here’s the link. Have any tips?
https://c.gethopscotch.com/p/zxvqj6dbh


#10

What device do you have?
If it’s an older device it may just be lag due to your device.


#11

Well, the game layout looks really good and it’s not laggy for me. I don’t know how you can fix the physics, unfortunately, since I’m not really into arctan physics. But I hope that someone else can help! I would personally have asked @ThinBuffalo, @CreationsOfaNoob or anyone else that is really good with physics.


#12

Ok, thanks for all your help!


#13

To reduce “lag” and timing problems in this type of project requires a coding paradigm change.

Hopscotch was not architected for this type of project. Only one block from each rule is executed per frame (at 60 FPS that’s every 16.7ms). So sequentially for each object (in the order of creation), the player executes 1 block from each rule (in the order the rules are organized in). Then the player waits for the next frame. If there’s too many objects and rules, the blocks may take more than 16.7ms to execute and the frame rate slows down. That’s lag.

Now take for example this rule from your project:

Each Set block is executed during subsequent frames so there is latency in how long it takes to execute all of the Set blocks. But this is not truely lag (as the player is still at 60 FPS). To get the player to execute the code faster, all of the blocks needed for each set of physics calculations have to be executed during the least amount of frames possible. So in keeping with the above example, if each Set block were in it’s own When 7=7 rule, the code will all execute in 1 frame. That will also help with timing bugs that aren’t readily apparent.

Does that all make sense? Now extend the concept to all objects, rules, and blocks that are needed for each set of physics calculations.

Note: conditionals (If blocks) and nested conditionals don’t count for the 1 block per frame rule. The entire conditional or nested conditional is evaluated and the 1st block from resulting code path is then executed in the same frame.


#14

Thanks you so much! That is very helpful for the physics! I was wondering… how do you control the order of which the lines are executed if they are in diffrent blocks? Thanks!


#15

Two more questions: First, which works better, “repeat forever” or “when 7=7”? Second, does drawing slow it down? (Example: writing the word “Clask”?)


#16

how do you control the order of which the lines are executed if they are in diffrent blocks?

The order the rules are in

which works better, “repeat forever” or “when 7=7”?

They’re the same

Second, does drawing slow it down? (Example: writing the word “Clask”?)

The drawing is slow because you coded the movement like this:


If it’s not clear why this is slow, go back and re-read my prior explanation. That repeat block of code takes 30*2=60 frames (or 1 full second) to execute.

Instead, using 1 Set Position block will take only 16.7ms

Set Position x [(self)x_position + 30] y [(self)y_position + 90]

#17

Ok. Thanks! I will make sure to add all of that.


#18

@ThinBuffalo @William04GamerA @tankt2016 Here’s the beta version. Thanks for all you help! The set position instead of the repeats worked way better! Thanks ThinBuffalo! Here it is:


#19

Great! I can’t wait for the finished version.