1

I want to read from a file in a game I'm making in Haskell using Gloss. The file is in JSON format, and the JSON is correctly loaded, but then it seems to get removed or something. In the code below, putStr (show highScores2) is ignored, and so are both of the putStr in the cases in the second code segment.

Why isn't the code in the cases executed, does this have to do with lazy evalutation? (or is it executed?) I don't see how, since this code is necessary for the remainder of the program.

My JSON: [{"score":345,"date":"timeString"}]

My code:

main :: IO ()
main = do
        gen <- getStdGen
        highScores <- loadHighScores
        let highScores2 = highScores ++ [Entry 895 "testingtesting"]
        putStr (show highScores2)
        playIO
              FullScreen         -- Fullscreen
              black              -- Background color
              60                 -- Frames per second
              (initialState gen highScores2) -- Initial state
              view               -- View function
              input              -- Event function
              step               -- Step function

File handling code, in this segment B.putStr json does print something to the screen, so I know it reads the JSON, but both the putStrs are ignored in the case segment:

  loadHighScores :: IO [HighScoreEntry]
  loadHighScores = do
                    json <- B.readFile highScorePath --Does not check for file does not exist
                    B.putStr json
                    -- let d = eitherDecode json :: Either String [HighScoreEntry]
                    -- case d of
                    --       Left err -> return []
                    --       Right hS  -> putStr "EUI" >> return hS
                    let d = decode json
                    case d of
                      Just hS -> (putStr $ "second - " ++ show hS) >> return hS
                      otherwise -> (putStr "whyyy") >> return []

Output:

[{"score":345,"date":"timeString"}]
The Coding Wombat
  • 805
  • 1
  • 10
  • 29
  • 2
    Probably relevant: [*IO happens out of order when using getLine and putStr*](https://stackoverflow.com/q/13190314/2751851) – duplode Nov 10 '18 at 18:35
  • @duplode code still doesn't work, but that fixed not printing anything. Thanks, now I can debug – The Coding Wombat Nov 10 '18 at 18:43
  • 1
    You likely want `putStrLn`, or need to use `hSetBuffering` to disable line buffering. I'm sure there's a dup question to link to but can't find one atm – jberryman Nov 10 '18 at 18:49
  • 1
    @duplode Meta question: So duplode's comment fixed what I asked for, but the code still isn't working, but that does not seem to have anything to do with this question per se. Should I answer it myself with duplode's suggestion? – The Coding Wombat Nov 10 '18 at 18:55
  • 1
    @TheCodingWombat I took a slightly different route and closed it as a duplicate of that question. Weren't it a duplicate, your idea of answering it yourself would have been entirely appropriate. And, as you have guessed, further problems with the code are best posted as separate questions. – duplode Nov 10 '18 at 19:24

0 Answers0