Update HighScore

I can't get my HighScore to update when it is shown! Maybe someone here has an idea!



I put the HighScore into an own State and build it in the Start. Then I start my game and write the time and playerName into the txt file of the HighScore! When I deactivate my Game and activate my HighScoreState it does not have the new time and playerName in it, because it did not refresh!



Is it possible to refresh / rebuild the HighScoreState somehow? I also tried building a new one, after the data was written, but the codes crashes really badly…



Thanks for any ideas :wink:

Sounds like you are "trapped in states". :wink:



I would suggest to strip you highscore-data and the saving/loading-logic of the highscore out of HighScoreState and move it to a new class, for example HighscoreList.



HighscoreList then holds the data of your highscore table and gives other classes access to it, but has nothing to do with the representation.

HighScoreState reads the data of the HighscoreList for representation.

And your ingame-state will write data to HighscoreList.

With this design you are also free to decide when you want to save/load the data from harddrive and don

But this still can't fix the problem!



I now have a HighScoreState (only graphics) saying:



//platz1
        JLabel platz1 = new JLabel();
        platz1.setSize(240, 40);
        platz1.setLocation(420, 200);
        platz1.setForeground(Color.black);
        platz1.setFont(new Font("Dialog", 0, 20));
        platz1.setText(HighScoreList.getPlayer1());



so I will get the Player1 from my HighScoreList!  Problem is that the HighScoreState need to be created after the player and time is written! When I just attach the "old" HighScoreState is does not include the new data!

When I create it in the Game my game freezes big times :(


GameStateManager.getInstance().attachChild(new HighScoreState("HighScoreState"));           
GameStateManager.getInstance().deactivateChildNamed("InGame");
GameStateManager.getInstance().activateChildNamed("HighScoreState");



You know what I mean?

Write the new highscore-data to the HighScoreList before you leave your InGame-State.



What do you mean with "freezes"? There is now error-message?



Are you sure your HighScoreState is working right?

Try to start you HighScoreState with some sample data at first, before you even activate your InGame-State.

The HighStoreState and List works fine! double checked it!



It freezes… Have to use ALT+F4 all the time :frowning:



I am writing it into the HighScoreListe before realving the InGameState:



 if (player.getWorldBound().intersects(finishZone.getWorldBound())){
           
           HighScoreList.saveScore(Timer.getTimer().getTimeInSeconds(), SavePlayerName.getplayerName());
           HighScoreList.initHighScore();
           SoundManager.stopIngameSound();
           SoundManager.playHighScoreSound();
           
          GameStateManager.getInstance().deactivateChildNamed("InGame");
          GameStateManager.getInstance().deactivateChildNamed("StatisticsGameState");
          GameStateManager.getInstance().deactivateChildNamed("HUDGameState");
            GameStateManager.getInstance().activateChildNamed("HighScoreState");
           
        }


Have you tried to insert some debug-outputs in your code to localize the freeze?



I would think your whole programe is idling because it has no active/working state.

did not debug yet! will do that later!



The file (HighScoreState) shouldn't be the problem, because when I create it in normaly (Start.java) it works!



Maybe it's because the function is in the update ?? Can that be?



if (player.getWorldBound().intersects(finishZone.getWorldBound())){
           
           HighScoreList.saveScore(Timer.getTimer().getTimeInSeconds(), SavePlayerName.getplayerName());
           HighScoreList.initHighScore();
           SoundManager.stopIngameSound();
           SoundManager.playHighScoreSound();
           
          GameStateManager.getInstance().deactivateChildNamed("InGame");
          GameStateManager.getInstance().deactivateChildNamed("StatisticsGameState");
          GameStateManager.getInstance().deactivateChildNamed("HUDGameState");
            GameStateManager.getInstance().activateChildNamed("HighScoreState");
           
        }

Use debug-outputs to understand what is happening. :slight_smile:



Generally I would suggest to handle state-transitions in the states.

That means: The IngameStates disables itself and enables the HighScoreState, for example.

This makes it much easier to understand, because states are this way more like modules and it is better design.

there is no error when I use:



 public WinnerState(final String name) throws InterruptedException, ExecutionException {
        super(true);
        setName(name);
        UncaughtExceptionHandler handler = new SDExceptionHandler();
        Thread.setDefaultUncaughtExceptionHandler(handler);
        GameStateManager.getInstance().attachChild(new HighScoreState("HighScoreState"));
        GameTaskQueueManager.getManager().update(new Callable<Object>() {
            public Object call() throws Exception {
                return null;
            }
        }).get();
    }

I debugged my game and got this:







It occurs on a JMEDesktopState, when I press the button highscore:



highscore.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
              SoundManager.stopWinnerSound();   
              HighScoreState.initHighScore();

              GameStateManager.getInstance().attachChild(new HighScoreState("HighScoreState"));             
             
              GameStateManager.getInstance().activateChildNamed("HighScoreState");
              GameStateManager.getInstance().deactivateChildNamed("WinnerState");
              MouseInput.get().setCursorVisible(true); 
            }
        });



It seems like it has problems to create the new HighScoreState (which also extends JMEDesktopState).

Is there a reason why my game freezes when I press this button?

is this maybe because you are executing the code for action performed in the awt event thread and not in the gl thread??


public class WinnerState extends JMEDesktopState {
    /**
     * Constructor.
     * @param name name of the Gamestate.
     * @throws ExecutionException
     * @throws InterruptedException
     */
   

    public WinnerState(final String name) {
        super(true);
        setName(name);
       
    }

    /**
     * create the Menu.
     */
    @Override
    protected void buildUI() {
        int width = DisplaySystem.getDisplaySystem().getWidth();
        int height = DisplaySystem.getDisplaySystem().getHeight();


       
        //main box, which is centered
        JPanel main = new JPanel();
        main.setSize(1024, 768);
        main.setOpaque(false);
        main.setLocation(width/2 - main.getWidth()/2, height/2 - main.getHeight()/2);
       
        getDesktop().getJDesktop().add(main);

       
        ImagePanel panel2 = new ImagePanel(new ImageIcon(ResourceLocatorTool.locateResource(ResourceLocatorTool.TYPE_TEXTURE, "winner_bg.png")).getImage());
        panel2.setSize(1024, 768);
        panel2.setLocation(width/2 - main.getWidth()/2, height/2 - main.getHeight()/2);
       
        getDesktop().getJDesktop().add(panel2);
        panel2.setVisible(true);
 
        JButton highscore = new JButton();
        highscore.setIcon(new ImageIcon(ResourceLocatorTool.locateResource(ResourceLocatorTool.TYPE_TEXTURE, "highscore_normal.png").getFile()));
        highscore.setSize(270, 55);
        highscore.setLocation(360,570);         
        highscore.setBorderPainted(false);
        highscore.setOpaque(false);
        highscore.setRolloverIcon(new ImageIcon(ResourceLocatorTool.locateResource(ResourceLocatorTool.TYPE_TEXTURE, "highscore_over.png").getFile()));
        highscore.setPressedIcon(new ImageIcon(ResourceLocatorTool.locateResource(ResourceLocatorTool.TYPE_TEXTURE, "highscore_over.png").getFile()));
        highscore.setRolloverEnabled(true);
                    
        getDesktop().getJDesktop().revalidate();
       
      //attach all to main
        main.setLayout(null);
        main.add(highscore);
        getDesktop().getJDesktop().updateUI();
        main.setVisible(true); 
              
           highscore.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            
              GameStateManager.getInstance().attachChild(new HighScoreState("HighScoreState"));             
             
              GameStateManager.getInstance().activateChildNamed("HighScoreState");
              GameStateManager.getInstance().deactivateChildNamed("WinnerState");
              MouseInput.get().setCursorVisible(true); 
            }
        });
       
    
    }



This is the code...

i know absolutely nothing about game states but try this in the action performed method


GameTaskQueueManager.getManager().update(new Callable<Void>() {
         public Void call() throws Exception {
            GameStateManager.getInstance().attachChild(new HighScoreState("HighScoreState"));             

            GameStateManager.getInstance().activateChildNamed("HighScoreState");
            GameStateManager.getInstance().deactivateChildNamed("WinnerState");
            MouseInput.get().setCursorVisible(true); 
            return null;
         }
      });


sadly it does not help :frowning: It still crashed my game…

I think I got it fixed! I just created my HighScoreState in the beginning and then got a function to build the Score!



 protected void buildUI() {
      
              
        int width = DisplaySystem.getDisplaySystem().getWidth();
        int height = DisplaySystem.getDisplaySystem().getHeight();
       

       
        //main box, which is centered
        JPanel main = new JPanel();
        main.setSize(1024, 768);
        main.setOpaque(false);
        main.setLocation(width/2 - main.getWidth()/2, height/2 - main.getHeight()/2);
       
        getDesktop().getJDesktop().add(main);

       
        ImagePanel panel2 = new ImagePanel(new ImageIcon(ResourceLocatorTool.locateResource(ResourceLocatorTool.TYPE_TEXTURE, "highscore1.png")).getImage());
        panel2.setSize(1024, 768);
        panel2.setLocation(width/2 - main.getWidth()/2, height/2 - main.getHeight()/2);
       
        getDesktop().getJDesktop().add(panel2);
        panel2.setVisible(true);
 
       
       
    
        //Buttons
        JButton back = new JButton();
        back.setIcon(new ImageIcon(ResourceLocatorTool.locateResource(ResourceLocatorTool.TYPE_TEXTURE, "zurueck_normal.png").getFile()));
        back.setSize(270, 55);
        back.setLocation(200, 600);
        back.setBorderPainted(false);
        back.setOpaque(false);
        back.setRolloverIcon(new ImageIcon(ResourceLocatorTool.locateResource(ResourceLocatorTool.TYPE_TEXTURE, "zurueck_over.png").getFile()));
        back.setPressedIcon(new ImageIcon(ResourceLocatorTool.locateResource(ResourceLocatorTool.TYPE_TEXTURE, "zurueck_over.png").getFile()));
        back.setRolloverEnabled(true);

        JButton quit = new JButton();
        quit.setIcon(new ImageIcon(ResourceLocatorTool.locateResource(ResourceLocatorTool.TYPE_TEXTURE, "spiel_beenden_normal.png").getFile()));
        quit.setSize(270, 55);
        quit.setLocation(580, 600);
        quit.setBorderPainted(false);
        quit.setOpaque(false);
        quit.setRolloverIcon(new ImageIcon(ResourceLocatorTool.locateResource(ResourceLocatorTool.TYPE_TEXTURE, "spiel_beenden_over.png").getFile()));
        quit.setRolloverEnabled(true);
        quit.setPressedIcon(new ImageIcon(ResourceLocatorTool.locateResource(ResourceLocatorTool.TYPE_TEXTURE, "spiel_beenden_over.png").getFile()));

            
       
        //attach all to main
        main.setLayout(null);
        main.add(back);
        main.add(quit);
        getDesktop().getJDesktop().updateUI();
        main.setVisible(true); 
        }




 public static void rebuildHighScore(){
         
                  
          JPanel main = new JPanel();
          main.setSize(1024, 768);
          main.setOpaque(false);
          main.setLocation(DisplaySystem.getDisplaySystem().getWidth()/2 - main.getWidth()/2, DisplaySystem.getDisplaySystem().getHeight()/2 - main.getHeight()/2);
         
                  
        //platz1
          JLabel platz1 = new JLabel();
          platz1.setSize(240, 40);
          platz1.setLocation(420, 200);
          platz1.setForeground(Color.black);
          platz1.setFont(new Font("Dialog", 0, 20));
          platz1.setText("test");
          platz1.setVisible(true);
         

          //platz2
          JLabel platz2 = new JLabel();
          platz2.setSize(240, 40);
          platz2.setLocation(230, 315);
          platz2.setForeground(Color.black);
          platz2.setFont(new Font("Dialog", 0, 15));
          platz2.setText("test2");
      
          //platz3
          JLabel platz3 = new JLabel();
          platz3.setSize(700, 40);
          platz3.setLocation(620, 315);
          platz3.setForeground(Color.black);
          platz3.setFont(new Font("Dialog", 0, 15));
          platz3.setText("test3");   
         
          //platz4
          JLabel platz4 = new JLabel();
          platz4.setSize(240, 40);
          platz4.setLocation(230, 410);
          platz4.setForeground(Color.black);
          platz4.setFont(new Font("Dialog", 0, 15));
          platz4.setText("test4");
         
          //platz5
          JLabel platz5 = new JLabel();
          platz5.setSize(700, 40);
          platz5.setLocation(620, 410);
          platz5.setForeground(Color.black);
          platz5.setFont(new Font("Dialog", 0, 15));
          platz5.setText("test5");
         
          main.setLayout(null);
          main.add(platz1);
          main.add(platz2); 
          main.add(platz3);
          main.add(platz4);
          main.add(platz5);
          main.setVisible(true); 
      
          main.repaint();
         
         

          System.out.println(HighScoreList.getPlayer1() + HighScoreList.getPlayer2() + HighScoreList.getPlayer3() + HighScoreList.getPlayer4() + HighScoreList.getPlayer5());
       }



It prints me the right HighScore on my console! How can I add the new main (or platz1...platz5..) to be printed on the screen?

Nevermind… got it working ;)  wuhuhu



Thanks for all the help!