trying to implement a daemon to write to web in my game ... failing

I have a game that works and I want to write any high scores to a central server so people can compare their scores. The idea is just to have a thread that loops once a second and either does nothing (most of the time) or reads the current high scores or writes the players high score. The program on the web works fine but I’m having issues getting my daemon code to compile with static non static errors:

neptune.java:3951: error: cannot find symbol
                                try { this.sleep(2000); } catch (InterruptedException e) { ; }
                                          ^
  symbol: method sleep(int)
neptune.java:3962: error: non-static method writeHighScores() cannot be referenced from a static context
                                        neptune.writeHighScores( );
                                               ^
neptune.java:3966: error: non-static method isGameStillPlaying() cannot be referenced from a static context
                        } while( neptune.isGameStillPlaying() );
                                        ^
3 errors

My code is arranged like this:

public class neptune extends Panel implements Runnable, KeyListener {
	int Score = 0;
        ...
	public static void main(String[] args) {
		neptune w = new neptune();
                ...
		try { 
                	WebIO webIoThread = w.new WebIO();
		} catch (Exception e) { ; }
        }

        ...

	class WebIO implements Runnable{
		public void run( ){
			long timeLastAttempt = 0;

			do {
				try { this.sleep(2000); } catch (InterruptedException e) { ; }

				// if failed to load high scores then try again
				if (	neptune.ThreadAction == neptune.READ_SCORES
					||	neptune.HighScoreScores[0] == 0 
						&& System.currentTimeMillis() - timeLastAttempt > 300000L ) {

					timeLastAttempt = System.currentTimeMillis();
					neptune.readHighScores( 0 ); 
					neptune.ThreadAction = neptune.NO_ACTION;
				} else if ( neptune.ThreadAction == neptune.WRITE_SCORES ) {
					neptune.writeHighScores( );
					neptune.ThreadAction = neptune.READ_SCORES;
				}

			} while( neptune.isGameStillPlaying() );
		}
	}
}


My apologies if I’ve included too much/too little. The problem is, if I start to change variables to static, then this soon snowballs into more static/non-static errors. I assume I’m missing something simple and just wondered if anyone could have a quick peek.

Many thanks

Mike

Thread.sleep() is itself a static method, and operates on the current thread.

Cas :slight_smile:

I think I’m going to need more clues :slight_smile:

Does it mean I can’t use sleep in the run bit? is there something else I should use?

Might need a bit more source to deduce what you’re trying to do here.

Cas :slight_smile:

How about showing/downloading the highscores only at the end of a game / in a special screen. There is really no need to download them every second.

Also, please don’t use static if you don’t know what it does. After you have made sure what it does, and you actually want something to be static, code it. If you make existing methods static, there is something wrong with the design for your code. If you haven’t made any design (looks that way to me) I really recommend making one in UML. It is not only handy to gain insight in bigger projects, but it also makes clear what your code is doing. For starters, if you take those designs seriously, this means that you will write much better code that actually does something in a straight-forward manner. Well that’s what I would recommend beginners anyway, professionals use this already…

[quote=“princec,post:4,topic:55648”]
My problem was having a separate thread (that appears to be static) trying to access the variables in the main thread that aren’t static. It then seems to throw the errors. I’ve managed to get it working by changing certain key variables to static.

[quote=“Herjan,post:5,topic:55648”]
The thread does nothing unless it needs to read or write scores to the web. It only writes a score if your current score is higher than the lowest score held for that game. I feel it should read the high scores periodically simply because other players may make it onto the table while the game is inactive. The intention is to put the game in arcade cabinets and this sort of feature is worthwhile. For testing the period is 5 mins but it will likely change to 30 mins or an hour in the future. It certainly doesn’t read the scores every second.

[quote=“Herjan,post:5,topic:55648”]
Out of mild interest I clicked on your one project to see what type of “big projects” you work on. Quite frankly it amazed me that you feel you can talk down to other developers but thank you for your input anyway :wink:

I’m a little late to the party, but if you still don’t have a solution, here’s at least two things you did wrong:

[icode]this.sleep[/icode] should be [icode]Thread.sleep[/icode] (as princec said)

You call methods like

neptune.isGameStillPlaying()

as if the methods are static in the neptune class, when they aren’t.

I would honestly get a better grasp on the static keyword and how you plan to pass around instances like the neptune w instance. Also, please use Java naming conventions (ie. make neptune uppercase). That’s likely about half of why you called non-static methods with a class name instead of an instance.

Thanks guys. It’s all working at the moment but I will improve the code going forwards. It’s a bit of a shame you can’t have an “asynchronous void” declaration on a method which would make life a bit easier.

public class MyGame {
   ...
   private asynchronous void myDaemon() {
   }
}