2015/07/24(金)FlashAirのLuaで、数値が入ったファイルの値を上下させる。
count.txtというファイルを作ってそこに記載された数値"0"~"99"の値を上下させるだけの簡単なLuaを作ったので自分用メモ。
- up.lua
-- ------------------------------------------------------ DATA_FILE = "/count.txt" local count = 0 local buf = 0 local next_count = 0 -- ------------------------------------------------------ print("up.lua") fp, msg = io.open(DATA_FILE,"r+") if fp then print("count.txt found.") fp:seek(set) buf = fp:read("*a") count = tonumber(buf) fp:close() else print("count.txt not found.") print(msg) end if count < 99 then next_count = count + 1 else next_count = count end print(string.format("count : %02d -> %02d",count, next_count)) fp = io.open(DATA_FILE,"w+") fp:seek(set) fp:write(next_count) fp:flush() sleep(100) fp:close() return
- down.lua
-- ------------------------------------------------------ DATA_FILE = "/count.txt" local count = 0 local buf = 0 -- ------------------------------------------------------ print("down.lua") fp, msg = io.open(DATA_FILE,"r+") if fp then print("count.txt found.") fp:seek(set) buf = fp:read("*a") count = tonumber(buf) fp:close() else print("count.txt not found.") print(msg) end if (0 < count) then print(string.format("count : %02d -> %02d",count, count-1)) count = count - 1 end fp = io.open(DATA_FILE,"w+") fp:seek(set) fp:write(count) fp:flush() sleep(100) fp:close() return
結局tonumber()を使って文字列を数値に変換することで解決した。