I’m trying to run the brilliant Mar I/O artificial intelligence written in Lua (more on this at https://youtu.be/qv6UVOQ0F44)
The AI runs successfully in the Lua (v.5.1) console of the BizHawk emulator (v.2.1.1), but I’m getting an error when trying to reload a previous state of the algorithm.
After opening the file, it seems like file:read(“*number”) will always return 0, whereas read(“*all”) and “*line” both read the content correctly. I’ve also tried “*n” with no luck.
Full script at: https://pastebin.com/ZZmSNaHX
function loadFile(filename)
local file = io.open(filename, "r")
pool = newPool()
pool.generation = file:read("*number")
pool.maxFitness = file:read("*number")
...
function writeFile(filename)
local file = io.open(filename, "w")
file:write(pool.generation .. "n")
file:write(pool.maxFitness .. "n")
...
The file generated starts with:
18[LF]
1938[LF]
...
But still, I only see 0s in the console:
console.writeline("Gen " .. pool.generation) --> "Gen 0"
console.writeline("Max Fitness " .. pool.maxFitness) --> "Max Fitness 0"
What’s also puzzling is that this script has been discussed in different forums and no one seems to report the same issue.
3
Answers
I found out that the BizHawk emulator is using a customized version of Lua. This issue appeared in version 2.1.1 of the emulator. Previous releases are working fine.
Thanks for your help community
The format to read a number is ‘n’, file:read(‘n’). See https://www.lua.org/manual/5.3/manual.html#pdf-file:read for details on read() format specifiers.
Around that time, BizHawk added an alternate c# lua implementation ‘kopilua’ in an effort to workaround deep crashy problems. One of kopilua’s several shortcomings is noncompliant string parsing and file IO. You must use config > customize > advanced > Lua+LuaInterface in order to use normal lua; it won’t have this problem.