read textfile.lua

来自「制作二维平面游戏中编辑题图的好工具」· LUA 代码 · 共 46 行

LUA
46
字号
-- Read textfile
-- Reads a texfile of ASCII digits (0 to 9) into current layer
-- columns must be smaller than map width

function main ()

  local w = mappy.getValue(mappy.MAPWIDTH)
  local h = mappy.getValue(mappy.MAPHEIGHT)

  if (w == 0) then
   mappy.msgBox ("Read textfile", "You need to load or create a map first", mappy.MMB_OK, mappy.MMB_ICONINFO)
  else
 
   local isok,asname = mappy.fileRequester (".", "Textfile (*.txt)", "*.txt", mappy.MMB_OPEN)
   if isok == mappy.MMB_OK then

    mappy.copyLayer(mappy.getValue(mappy.CURLAYER),mappy.MPY_UNDO)

    infile = io.open (asname, "r")
    local y = 0
    while y < h do
     local x = 0
     while x < w do
      local blk = tonumber (infile:read (1))
      if (blk ~= nil) then
       mappy.setBlock (x, y, blk)
       x = x + 1
      else
       x = w
      end
     end
     y = y + 1
    end
    infile:close ()

    mappy.updateScreen()

   end
  end
end

test, errormsg = pcall( main )
if not test then
    mappy.msgBox("Error ...", errormsg, mappy.MMB_OK, mappy.MMB_ICONEXCLAMATION)
end

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?