Problems with adding Ashley entities to the engine #254
-
In the below example of coroutineContext, i've launch a creation of entity from engine. But sometimes it does not reflect on update. If there's an efficient way of checking if entity was added or not then retry can you share? Thanks 🗡 Language:
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
Unless I'm misunderstanding the problem, it looks like you're running into concurrency issues. Entity not being properly added shouldn't be a problem in the first place. Is there a reason you're adding entities asynchronously? If there is some part of your application that's time consuming (e.g. waiting for an asset to be loaded), you might want to do that first in a coroutine and then create the entity back on the rendering thread. For example: // Assumming that worldServer has its own executor, e.g. AsyncExecutorDispatcher:
worldServer.launch {
// Do that expensive operation here. Perhaps some of that createPlayer logic?
onRenderingThread {
// Create your entity back on the rendering thread:
gameEngine.createPlayer(character, client)
}
} As long as you're accessing a resource from a single thread, you should avoid most of the issues. In this case, if you're both reading (during render/update) and writing (via rendering thread coroutine) entities only from the main LibGDX rendering thread, you shouldn't need to check if the entity was added successfully in the first place. |
Beta Was this translation helpful? Give feedback.
-
You're right I'm running on async, I suspect the problem was in client, because client was obtain by server socket, but the game engine continuously running even the socket is in process of listening for client, character creation. |
Beta Was this translation helpful? Give feedback.
-
That's what I figured. You should use the client to communicate with the server via a standard coroutine, and then switch to the rendering thread to create your entity once you have all necessary data. Your updated code could look roughly like this: worldServer.launch {
val serverData = client.getServerResponse()
onRenderingThread {
gameEngine.createPlayer(character, serverData)
}
} Handling client-server communication on a separate thread with coroutines is a good idea, but you have to be careful about updating game state - remember to switch to the rendering thread before doing anything that could cause concurrency bugs. Let me know if that solves your issue. |
Beta Was this translation helpful? Give feedback.
-
You're right. I solved this by using same context! |
Beta Was this translation helpful? Give feedback.
Unless I'm misunderstanding the problem, it looks like you're running into concurrency issues. Entity not being properly added shouldn't be a problem in the first place.
Is there a reason you're adding entities asynchronously? If there is some part of your application that's time consuming (e.g. waiting for an asset to be loaded), you might want to do that first in a coroutine and then create the entity back on the rendering thread. For example: