I've been lurking around various websites and reading up on many different threads over the past few days and I noticed that a few people were saying not to use Thread.sleep for various reasons that I can't currently remember.

I was wondering if it's alright to be using Thread.sleep() in this circumstance:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

classMessageDisplay{publicMessageDisplay() { }

//NOTE: You may need to add a way to decide where to print the message or else this wont work!publicvoidtypeWriterDisplay(Stringx, inty) //Requires a string to display and an int to decide how long to wait between displaying letters. {for (inti = 0; i < x.length(); i++) {System.out.print( x.charAt(i) );try {Thread.sleep(y); }catch (Exceptione) {e.printStackTrace(); } } }}

I'm using it to display strings character-by-character with a small delay between characters.

The only reason people have been saying not to use Thread.sleep(), because it is unreliable when it comes to tracking fixed time steps. Each OS has a different way of interpreting Thread.sleep() which will end up in a game running a lot slower on one PC than the other.

However, just because it is unreliable doesn't mean that you shouldn't use it. I think the only real weakness is that you make your game slightly less professional. Actually, even with variable time step, it still will fall weakness to lag spikes and your game will end up sputtering instead. (Though these things are barely noticeable with LWJGL because the frame rate is so much better anyway.)

Ah, that makes sense, thanks! Do you know of any other ways to get this sort of character-by-character effect using the regular java libraries and without thread.sleep(), I'd rather avoid the laggy look that you were describing.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org