Reputation: 233
I have a bunch of variables in scala:
triangleCount = 2
lives = 5
triangleScore = 1
curPlayer.getX
curPlayer.getY
When I press "s" in my match case:
case "s" =>
I want the variables to be written to an external file.
Then when I press "l" in my match case:
case "l" =>
I want the variables to be loaded back into my program and replace the default ones.
How would I go about doing this? I know it has something to do with scala.io.Source or java Scanner but i'm not sure how to do it.
Thanks
Upvotes: 0
Views: 1118
Reputation: 51
case "s" =>
val pw = new java.io.PrintWriter("myVars")
pw.println(List(triangleCount, lives, triangleScore, curPlayer.getX, curPlayer.getY).mkString("\t"))
pw.close
case "l" =>
val myVarsFromFile = io.Source.fromFile("myVars").getLines.toList
if (!myVarsFromFile.isEmpty) {
val toks = myVarsFromFile.head.split("\t")
var (triangleCount,lives, triangleScore) = (toks(0).toInt, toks(1).toInt, toks(2).toInt)
curPlayer.setX(toks(3).toDouble)
curPlayer.setY(toks(4).toDouble)
}
Upvotes: 1