Delete Objects Off Screen & Creating Them Back

In my game, objects are stored in files like this.

Objects in a level are stored in “tileInfo”. Below is the code that spawns them in if they are close to the camera, and deletes them if they are not in the cameras bounds. The only problem being that objects with data that’s within the camera bounds, are being created thousands of times. How do I fix this issue? (Trigger once doesn’t work, I’ve already tried…)

What 's the reason for you to do this? With GDevelop, if an object is far enough off screen, it doesn’t get drawn or processed.

the levels in my game has thousands of objects, sometimes many of these objects can be next to each other. there is still some performance issues when there is 2 thousand objects in a level

This seems like a lot of objects and processing. Think about a way to either reduce objects or use separate scenes.

Anyways. If you add an additional boolean variable then you could track the objects state. If false and near, add object and set to true. If true and far the set to false and delete.

You could also check if an object has that ID. You might need to use inverted or Not or a simple ≠. I’m not at my PC to check it.

You could also add and remove the objects from the array as they’re added and removed from the scene. I might add a delay so they can’t flip back and forth on every other frame if they’re near the off screen limit. Maybe a timer or a tolerance or rounding. If there’s a chance they might change directions give them a few seconds to “change their minds”

Either way, you might want to add a timer and check the array less frequently.

While I don’t understand your process. I would recommend you look for every possible way to reduce the object count in another way such as using a single larger object instead of multiple smaller ones. Using timers to reduce the frequency of computations. Optimizing operations. Limit things like entire groups of events to only run when the main condition or timer is met.

Few things to note:

  1. People have done tests with 30000 objects in a level without performance impact, and that’s while being rendered
  2. Objects are not rendered when off the screen.
  3. “For each object” events are much more intensive than most other event types, as object selection is one of the most intensive things in code. There’s a good chance if you have a lot of those, that is your performance bottleneck, not the number of objects. Unless each instance of an object has to have unique actions done on it (rather than the same actions done on each object when the conditiosn are met), you should look into using standard events with things like “Pick all X objects” or variables to select the objects you need.
  4. Deleting and creating objects is going to be more intensive than anything you’d normally do with most other standard events. You’re likely giving yourself a worse performance issue this way. If the “pick all x objects” and standard events mentioned above don’t meet your needs, you’re probably best just using a boolean variable that you toggle off when you don’t want to address that object, then just add a condition to your other events that checks that variable to be true to only target the objects you care about.
2 Likes

I agree with Silver-Streak , I have seen scenes with a lot of objects have problems with lagging but it wasn’t a simple object count issue. It was a for each object and custom behaviors that ran on every frame instead of just when needed. Or a lot of collision testing.

1 Like

I’ll look into this. Lemme use the debugger.

Edit: Not even sure why I was lagging earlier, but it’s gone now. Also, if the objects (post-events) ms is high, is that an object count related issue?