How do I reduce lag with a scene full of actors that need collision checking?

Fayabella

  • Posts: 194
Heya,

I've got some randomly generated levels going on. Trees are included. All of the trees are actors, however, and when I have a bunch of trees on-screen, I get ridiculous lag.

I've read up and I see a lot of "change each of the actors to images and you won't have lag."
But I need them to be actors! One, for collision, and two, for interaction (chopping down).

They don't have any "when updating" events or anything like that, just a single "when created" statement  that kills itself with a 2 in 7 chance (to thin out the forest areas a bit).

I'm assuming it's the collision checking, but it could also be something else. What can I do to reduce the lag? And I'm gonna have other actors later as well, for blocks/furniture/workbenches/more plants, etc. so I'm going to need a way to reduce the lag for every type of place-able actor.

So, how do I reduce lag while retaining the functionality of actors?

« Last Edit: January 02, 2019, 08:37:20 pm by Fayabella »

RosalinaGalaxer

  • Posts: 239
Do the trees collide with each other? If they do, that can cause a huge amount of lag.
Ģ̷̓l̴̥̒͑̕͝ì̷̘͈̬͈̖̂͂̔̕t̷͔͇̯̥̬̀̽̓͜͝c̵͇̦̼̮̉̐̈́̕͝ͅͅḣ̵̡̫̞͚̐̅ͅë̶̗̦̪̖͚̜́͊̄͑s̵̺̹̖̼̥̃.̴̮̫͐ ̶̛̯͍̓̇̾̎Ť̵̳̻̙̦̈́̅̓͆̚h̴̗̩̫͖̍͑̋̋͗͜͜e̴̻̰̔̂ ̷̢͈̖̏́b̸̘̻͖̣̙̍͒̆́a̶̺̗̟̿͒n̵̡̼̘̺̚ḙ̸̗̹̥̜͐͘͝ ̸̨̮̭̳̓͜ō̸̧͙̜̮̌͑f̴̜̒̔̆͊ ̵̪̹̱̪̌͘͝Y̸͔̪̽͠u̷̱̳̟͒̂̊͒̕͜ḿ̸̡̤͚̬̪ë̵̪͍̹͝k̶̝̣̗̞̹̖̅̊̅y̶̧̞̟̤͚̋̀̔o̴̯̾'̷̠̐̋͂̇̿̕s̴̡̬̲̠̰̦͗͂ ̴̢̘̤̝͆̀͘b̸̛͖̲͒̅̊̏͝o̶̘̮͊͐͗̇͠͝r̷̰͓͍̣̀̄d̷̢̞̂̚e̶͖̲̯̰̫̅̈͗r̸̢̒̊͜ͅ

“I have never seen a more heated discussion about context, jazz, and cats.” - VanillaButterz

Fayabella

  • Posts: 194
No, they only collide with the player. The leaves overlap at some points; however, the collision box is at the very base of the tree, and no two trees have trunks right next to each other or inside of each other. The tree actor also has 'continuous collisions' disabled.

merrak

  • *
  • Posts: 2472
... when I have a bunch of trees on-screen, I get ridiculous lag...

How many is a "bunch"?

KGBlag

  • Posts: 35
Do you mind uploading the scene and linking it to this thread? It's a lot easier to get a feeling for what the problem is when we can actually see it in motion.

Fayabella

  • Posts: 194
How many is a "bunch"?

Like this.
What's strange is that it only happens some of the times I enter an area like this.

Also, the file size was too big to upload to Stencyl, and I couldn't figure out how to embed it in the post.

merrak

  • *
  • Posts: 2472
That seems like a reasonable number of trees to have on screen at one time. The other question is how many are in the scene? And are they set always active? Order of magnitude is all that matters--dozens, hundreds, thousands, tens of thousands.

I'd expect you to have problems once you get into the thousands, even if they don't collide with each other. Fortunately, there are a lot of ways to get around this.

Fayabella

  • Posts: 194
The scene is only 64x48 tiles. The trees take up 50-70%* of that scene, with the spacing you see above. Probably only in the hundreds. They're not set to always active.

However... I'm gonna need to add more actors similar to the trees later on, like plants, bushes, rocks, placeable objects, etc. and then it might get higher as I add more objects to the scene.
Fortunately, there are a lot of ways to get around this.
So, what kind of things can I do to prevent lag from scenes where a player has placed many objects, and the world has generated many trees/plants/rocks?

*Edit: The scene generates 50-70% trees, but each tree has a 2 in 7 chance of not killing itself.

merrak

  • *
  • Posts: 2472
Hundreds sounds about right to me. I'd suggest you run your game with telemetry enabled (use hxScout) and see exactly where the problem lies. Trying to solve lag problems using trial and error can be an exercise in frustration. There are a lot of things that can go wrong and each has its own good solution.

Common problems to look out for:

- High rendering time: Too many bitmap graphics on the screen. Unless your resolution is huge (yours doesn't look big) this is often the result of having too many bitmaps (actors included) drawing over each other. Try to skip drawing anything that will be covered entirely by other images. If you notice lag when entering an area with a lot of trees, this may be the problem you're having.

- High garbage collection time: Look at what is being allocated. Having a lot of collisions can cause a lot of garbage, but in general this is the result of moving an excessive amount of data in and out of memory. Look at the allocations to see what data is being moved around in excess.

- Bottlenecks in always events, drawing events, or other Stencyl events: These are relatively easy to spot if you expand the profiler hierarchy. There's just a lot to read through. Although the function names will be the internal ones, you should be able to see the name of the offending behavior in the hierarchy somewhere.

Fayabella

  • Posts: 194
This may be a stupid question, but what exactly is hxScout?

This may be a stupid question, but how exactly do I use hxScout?

« Last Edit: January 02, 2019, 09:18:09 pm by Fayabella »

merrak

  • *
  • Posts: 2472
This may be a stupid question, but what exactly is hxScout?

hxScout is a profiling tool. You can get it for free: https://hxscout.com/

Go into the game settings and enable telemetry. Run hxScout. Then test your game. hxScout should detect your game is running and then start feeding you information about what is going on. One of the most useful things it will tell you is exactly how long each function in the program takes to run. When it's tracking your game, it will give you a little timeline with FPS. Click and drag to highlight a part of the timeline and then get a glimpse of what was being taken in and out of memory, time to run different routines, and other info.

The learning curve may be a bit steep, depending on what your programming background is like. It's well worth learning how to use it, though

Fayabella

  • Posts: 194
I have hxScout running and telemetry enabled, but it won't pick up my game. What could be wrong?

I'm running in Flash.

merrak

  • *
  • Posts: 2472
I'm not sure whether or not it works in Flash. Try Desktop

Fayabella

  • Posts: 194
For some reason I can't run my game in Desktop. Anything else I can do?

merrak

  • *
  • Posts: 2472
I'm not sure. The hxScout website does mention Flash, so maybe there's a way to get it to work with it... I'm just not sure myself since I always develop on Desktop, even if Flash is the intended target.

All this aside, I have a hunch it's the number of trees that's the issue. If you can put a playable demo on itchio or Gamejolt then someone can play it and get a better idea of what's wrong.