How do I load variables from a file back into Scala?
I have variables in scala that have their values written to an external file in this format:
1 2 0 0.5 0.62
This is my code that puts them in the file called "myVars.txt":
val pw = new java.io.PrintWriter("myVars.txt") pw.println(beginGameCounter) pw.println(triangleCount) pw.println(triangleScore) pw.println(lives) pw.println(curPlayer.getX) pw.println(curPlayer.getY) pw.close
How would I load these variables back into scala and replace the old variables with these new variables (for example the old variable for beginGameCounter is 0, then when I load the file, I want it to become 1)?
You could try something like this:
val values = Source.fromFile(pathToFile).getLines.toList beginGameCounter = values(0) triangleCount = values(1) triangleScore = values(2) lives = values(3) currPlayer.setX(values(4)) currPlayer.setY(values(5))
This of course assumes that the what variable is on what line is consistent.