Fixing slight delay due to many threads being created

Hello, so I’ve been working on my game lately and I just witnessed a slight delay that occurs for some of the game objects I have. The objects are rotators, so when X seconds pass, these objects rotate 90 degrees. There are two of these objects I’m watching, and they’re set to rotate at the same interval (every 0.5 seconds) However, I’m noticing at random occurrences they’re actually out of sync quite a bit unfortunately.

I figure this is due to the core game loop, which iterates over all the game objects, checks to see if it isn’t currently updating that game object, and if so, call an Update method for that object in a new coroutine to not interrupt the current thread for other object updates.

This is a bit puzzling. I worry the delay could get to the point of actually affecting the rotations I set up for these objects before the game even started. (larger the delay, the less and less accurate they’ll be?)

One idea I thought of doing was separating these specific objects into another table, then iterating over that specific table before all the other game objects, in hopes of creating more instantaneous updates for those objects to reduce this delay between them. Haven’t tried this though.

Another question
And since I decided to make a post, I figured I’d throw in another question regarding the yield for my core game loop. I’m currently using a wait(), mainly because it runs at most 30 times a second, which I feel is more appropriate for updating all these objects. If I migrate over to a more event-based way of yielding, such as Stepped:Wait(), that should run at most 60 times a second, and this could potentially cause performance issues as to everything running twice as frequently…really not sure as to what the ideal method for yielding the core game loop would be; as it’d crash obviously with no yield in there at all.

Thanks for reading :slight_smile:

if they are each on their own script it could be that the scripts are loading in at different times. So while they may both be waiting the same time. one began before the other.

If they are on the same script then this could be due to multiple parts being moved.

1 Like

I’m using an OOP approach, I should’ve clarified that in the original post. The game objects are being updated from the core game module, and there are OOP instances (tables) created for each game object stored in memory, which the game module accesses to update each object.

Hope that makes sense a bit.

Also, do you have any ideas as to what I could try to negate the delay?

If the parts are in a table and you are iterating through it and changing them, even without a delay a delay is still possible as it wont move onto the next part until the last one has finished. This means that if you look at the first part it moves, and the last. There might be a noticeable gap.

You could create coroutines for each of them, but make sure the coroutines cant start the movement timer until all of them have a coroutine created.

1 Like

In the core game loop, each time an object can be updated a new coroutine is created and then the Update method for that object is called in the coroutine.