New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
About the new node API: How do I keep control ? #69
Comments
Hi @Uriopass, I knew this issue would show up sooner or later 😄 I could not reach this feature (although I would have liked) because of technical restrictions with the OS Poll.
For that, is not recommended to mix the network thread along with your application thread. If the network saturates, it would stop all your business logic. The "network" is something outside of the "application control zone" and we can not trust it because it handles things that happen out. A solution? Put your related logic network into the I hope I have helped you in some way with the problem. |
Thinking about it... Another feasible solution could be using a timed signal with a time of 1/60 to control the time loop. |
It's just a bit annoying since being in sync can be really useful. For example, when the local server needs to send the world to a newly connected client, I just pass a reference to the world to the |
Being "sync" in this context (understanding it as reading from the network directly in the game loop) is useful in the same way a global variable is useful and easier to write/read throughout the application. But it has the downside of losing organization and probably losing safety too. If you need that behavior because it fits well in your current architecture. You can continue using something like the following that behaves in the same way as enum DeserializedNetEvent<T> {
Connected(Endpoint, ResourceId),
Message(Endpoint, T),
Disconnected(Endpoint),
}
impl<T> From<NetEvent<'_>> for StoredNetEvent<T> { /* ... */} //Deserialize here to T
//....
listener.for_each(|net_event| event_sender.send(StoredNetEvent::from(net_event)));
loop {
let stored_net_event = event_receiver.try_receive();
// rest of game iteration
} Now, if you want to reach all the potential of the new API. you probably need to use a mutex and write your let world = Arc<Mutex<World>>;
let task = listener.for_each(|net_event| {
//....
*world.lock().unwrap() = world_from_event;
);
//...
loop { //game loop
let world = world.lock.unwrap();
// rest of game iteration
}
task.wait(); // Wait until node_hadler.stop() be called. Does this last scheme fit well with your game? |
I understand very clearly the advantages to be had being async, I understand the previous API was kind of a "smoke screen" as this is actually a bad practice. I just didn't mind the extra copies, and I didn't want to bother with thread communication stuff, even if Rust makes it safe. |
Nice! I'll take a look when I have time. Thank you for adding this. Having the choice between flexibility and performance is always welcome. :-) |
I'm using message-io as part of my game's networking. Using the previous API I was doing something like this (pseudo code)
However, it looks like using the new API I cannot do this as the
for_each
loop completely takes control of the thread used.My guess is that I now have to communicate between my game state and the networking code using channels.. I liked that it was abstracted away.
The text was updated successfully, but these errors were encountered: