Skip to content

How can we have a console in a separate thread? #1150

Answered by DJMcNab
visualcode-t asked this question in Q&A
Discussion options

You must be logged in to vote

One simple way would probably be to read from stdin, which normally gets flushed every time you press enter. E.g. you can use https://doc.rust-lang.org/std/io/trait.BufRead.html#method.read_line on std::io::stdin().
For that you'd need to create a seperate thread (before calling App::run), and then use a channel to send the data into bevy, via putting the channel in a resource. See https://stackoverflow.com/questions/30012995/how-can-i-read-non-blocking-from-stdin

Edit: Added code from #1150 (reply in thread) to accepted answer

Code hidden to save space
use std::io::stdin;

use bevy::prelude::*;
use crossbeam_channel::{bounded, Receiver};

fn main() {
    let (tx, rx) = bounded::<String>(1)

Replies: 3 comments 5 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by visualcode-t
Comment options

You must be logged in to vote
2 replies
@DJMcNab
Comment options

@dominikj111
Comment options

Comment options

You must be logged in to vote
3 replies
@DJMcNab
Comment options

@visualcode-t
Comment options

@DJMcNab
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants