FS API ã使ãããªããï¼8ï¼: ãè£è¶³ãé¢æ°ãå®ç¾©ããé¢æ°ãå®ç¾©ããï¼å¿ç¨ç·¨ï¼
ååã®ã話
- é¢æ°åããã°ã©ãã³ã°ï¼åºæ¬ç·¨ï¼ï¼
ååã¯é¢æ°åããã°ã©ãã³ã°ã®åºæ¬ã¨ãããã¨ã§ãLuaã®é¢æ°ããã¡ã¼ã¹ãã¯ã©ã¹ãªãã¸ã§ã¯ãã§ãããã¨ã説æãã¾ããã
ã»ã»ã»ãã¼ã¤ã¾ããLuaã®ãé¢æ°åãã¯ãæ°å¤åãããæåååãã¨åã¬ãã«ã®åºæ¬çãªåã§ããã
æ°å¤ãå¤æ°ã«ä»£å ¥ããããæ°å¤ãé¢æ°ã®å¼æ°ã¨ãã¦ä¸ããããé¢æ°ã®è¿å¤ã¨ãã¦æ°å¤ãè¿ã£ã¦ããã®ã¨åæ§ã«ã
é¢æ°ãå¤æ°ã«ä»£å ¥ããããé¢æ°ãé¢æ°ã®å¼æ°ã¨ãã¦ä¸ããããé¢æ°ã®è¾ºå°ã¨ãã¦é¢æ°ãè¿ãããã«ã§ããã®ã§ãã
ãã®ãããã§ãé·ãé¢æ°åãçãé¢æ°åã«å¤ããããå¾ããæè»ã«æåãå¤ãããåçãªé¢æ°ããä½ãããããããã§ãã
便å©ã§ãããã
ãããã使ãããªããã¨ãã§ããã°ãããªããç«æ´¾ãªé¢æ°åããã°ã©ãï¼ï¼ï¼
ã»ã»ã»ã»ã»ã»ããããããã§ãããããã¯ã¾ã åºæ¬ã«éããªãã®ã§ããã
ä¼¼ããããªé¢æ°ãããããå®ç¾©ããã¨ããè¦é£
APIãä½ãã¨ãã«æ©ã¾ãããã®ããä¼¼ããããªé¢æ°ãããããå®ç¾©ããªããã°ãªããªããã¨ã§ãã
ããã°ã©ãã¯åé·ãªããã°ã©ã ãå«ãã¾ãã
ãåããããªå¦çãåããããªè¨è¿°ãé·ã ã¨ç¶ãããã°ã©ã ã¯ãã¢ã¤ï¼ãã¨çççãªã¬ãã«ã§å«æªãæ±ãããã«ãªãã°ããªããç«æ´¾ãªããã°ã©ãã§ããï¼P
ãã®ãããªåé·ãªå¦çãåé·ãªè¨è¿°ã¯ãå ±éé¨åãã¾ã¨ãã¦ãµãé¢æ°ã«ãã¾ãããã
æ§é åããã°ã©ãã³ã°ã§ããããæ§é åï¼æ§é åï¼
ãã¦ä»åã®ãé¡ãZAP!ZAP!ããåé·ãªããã°ã©ã ã¯ãååç´¹ä»ãããlogging APIãã§ãã
forward()ãback()ãup()ãdown()ãturnRight()ãturnLeft()ã®6ã¤ã®é¢æ°ãå®ç¾©ãã¦ããã®ã§ãããã»ã¨ãã©åãè¨è¿°ã¨ãªã£ã¦ãã¾ãã
èªåã®æ¸ããããã°ã©ã ãªãããããããã
é¢æ°åããã°ã©ãã³ã°ã®ææ³ãå©ç¨ãã¦ãæ§é åãã¾ãããã
ã¾ããå ±éé¨åãè¦ã¤ãåºãã
ã¾ãã¯ãã£ããã¨è¦æ¯ã¹ã¦ã¿ã¾ãããã
function forward() local status, error_msg = turtle.forward() if status then -- succeeded write("turtle.forward()") else -- failed write("-- turtle.forward()") end return status, error_msg end function back() local status, error_msg = turtle.back() if status then -- succeeded write("turtle.back()") else -- failed write("-- turtle.back()") end return status, error_msg end
éãã®ã¯ä»¥ä¸ã®3ç®æã®ããã§ãã
- åé¢æ°ã®2è¡ç®ï¼ãé¢æ°ï¼
local status, error_msg = turtle.forward()
local status, error_msg = turtle.back()
- åé¢æ°ã®4è¡ç®ï¼ãæååï¼
write("turtle.forward()")
write("turtle.back()")
- åé¢æ°ã®6è¡ç®ï¼ãæååï¼
write("-- turtle.forward()")
write("-- turtle.forward()")
ã¨ãããã¨ã¯ã3ã¤ã®å¼æ°ãæã¤ãµãé¢æ°ãä½ãã°ãã¾ãè¡ãããã
function subDef(func, succeeded_msg, failed_msg) local status, error_msg = func() if status then -- succeeded write(succeeded_msg) else -- failed write(failed_msg) end return status, error_msg end forward() subDef(turtle.forward, "turtle.forward()", "-- turtle.forward()") end back() subDef(turtle.back, "turtle.back()", "-- turtle.back()") end
ååã®ããã°ã©ã ã¨æ¯ã¹ãã¨ããªããã£ããã¨ãã¾ãããã
ãã¤ã³ãã¯ããµãé¢æ°subDef()ã®ç¬¬1å¼æ°ãé¢æ°ã§ãããã¨ã§ãã
é¢æ°ããèªä½ãå¼æ°ã¨ãã¦ä¸ããããã«ãturtle.forward
ã¨æ«å°¾ã®ãã£ã()ã¯å¿
è¦ãªãã®ã§ãããããä¸å¿å¿µã®ããã
ãã®ãããªãµãé¢æ°ã使ã£ãæ§é åã¯ãã°ãæ¸ããå¹æãããã¾ãããä½ããæ¸ãã®ã楽ã«ãªãã¾ãã
æ®ã£ã4ã¤ã®é¢æ°ã«ã¤ãã¦ãæ¸ãæããããã°ã©ã å ¨ä½ã¯ã¨ä»¥ä¸ã®ããã«ãªãã¾ãã
-- ########################### -- config local LOGFILE = "mylog" -- ########################### -- functions function renameLogfile(name) LOGFILE = name end function write(message) local fh = fs.open(LOGFILE, "a") fh.writeLine(message) fh.close() end local function subDef(func, succeeded_msg, failed_msg) local status, error_msg = func() if status then -- succeeded write(succeeded_msg) else -- failed write(failed_msg) end return status, error_msg end function forward() subDef(turtle.forward, "turtle.forward()", "-- turtle.forward()") end function back() subDef(turtle.back, "turtle.back()", "-- turtle.back()") end function up() subDef(turtle.up, "turtle.up()", "-- turtle.up()") end function down() subDef(turtle.down, "turtle.down()", "-- turtle.down()") end function turnRight() subDef(turtle.turnRight, "turtle.turnRight()", "-- turtle.turnRight()") end function turnLeft() subDef(turtle.turnLeft, "turtle.turnLeft()", "-- turtle.turnLeft()") end
ãªããå¤æ°ãé¢æ°å®ç¾©ã®é ã«local
ã¨ã¤ããã®ã¯ããããããã¼ã«ã«å¤æ°ããã¼ã«ã«é¢æ°ã«ããããã§ãã
APIãä½ãã¨ãã«ã¯ãå¤æ°ã»é¢æ°ã®ãã¼ã«ã«ï¼ã°ãã¼ãã«ãæèãã¾ãããã
APIãã¡ã¤ã«ã®ä¸ã§ï¼local
ãã¤ãã¦ããªãï¼ã°ãã¼ãã«ãªå¤æ°ã»é¢æ°ãããã®APIãæä¾ããå¤æ°ã»é¢æ°ã«ãªãã¾ãã
éã«ãAPIãã¡ã¤ã«ã®ä¸ã§ãã¼ã«ã«å®£è¨ããå¤æ°ã»é¢æ°ã¯ä»ã®ããã°ã©ã ããï¼ååã¨ãã¦ï¼å©ç¨ãããã¨ã¯ã§ãã¾ããã
ã¤ã¾ãããã®APIãå©ç¨ããå´ã®ããã°ã©ã ããã¯ãå¤æ°LOGFILEãsubDef()é¢æ°ãç´æ¥è§¦ããªãããã«ãã¦ããããã§ãã
ã¤ã¾ããã¼ã¿é è½ï¼ãã«ãã»ã«åï¼ãã«ãã»ã«åï¼
ååå¦ãã ãã¨ãå¿ç¨ãã¦ããã«ç°¡ç¥å
ååããç¡åé¢æ°ãã¯ãã¡ã¼ã¹ãã¯ã©ã¹ãªãã¸ã§ã¯ããªã®ã§å¥½ããªå¤æ°ã«ä»£å ¥ãããã¨ãã§ããã¨å¦ã³ã¾ããã
ãã®ãããä¸è¨ã®é¢æ°å®ç¾©ã¯ä»¥ä¸ã®ããã«æ¸ãæãããã¨ãå¯è½ã§ãã
forward = function() subDef(turtle.forward, "turtle.forward()", "-- turtle.forward()") end back = function() subDef(turtle.back, "turtle.back()", "-- turtle.back()") end up = function up() subDef(turtle.up, "turtle.up()", "-- turtle.up()") end down = function() subDef(turtle.down, "turtle.down()", "-- turtle.down()") end turnRight = function() subDef(turtle.turnRight, "turtle.turnRight()", "-- turtle.turnRight()") end turnLeft = function() subDef(turtle.turnLeft, "turtle.turnLeft()", "-- turtle.turnLeft()") end
function() ï½ end
ã¨ããå¶å¾¡æã§ãç¡åé¢æ°ãä½æãããã¨ãã§ãã¾ãã
forward = function() ï½ end
ã¨ã¯ããªãã¡ãforward = (ç¡åé¢æ°)
ã®ããã«å¤æ°forwardã«æ°ãã«ä½ã£ãç¡åé¢æ°ã代å
¥ããã¨ããå¦çã«ãªãã¾ãã
ã¤ã¾ãã以ä¸ã®2ã¤ã¯å ¨ãåãæå³ã§ãã
function hoge() ã»ã»ã» end
hoge = function() ã»ã»ã» end
å®ã¯ã¶ã£ã¡ããã¦è¨ãã¾ãããã
Luaã§ã¯ãåè ã®è¨è¿°ãããã°ã©ã ä¸ã«ãã£ãããå¾è ã®è¨è¿°ã«ç½®ãæãã¦ããå®è¡ããã¾ãã
ã¤ã¾ããå¾è ã®å¤æ°ã«ç¡åé¢æ°ãä»£å ¥ããã¨ããå¦çã®æ¹ãLuaçã«ã¯æ£ããã®ã§ãï¼
ããã¼ï¼ãªãã ã£ã¦ã¼ï¼ï¼ï¼AAç¥
ã¾ã çµããã§ã¯ãªãã®ã§ãã
ããªããã£ããã¨ãã¾ããããå®ã¯ãã£ã¨ç°¡ç¥åã§ãã¾ãããï¼ãããã以ä¸ã¯ç¡çã£ã¦ï¼
ãããå ±éé¨åã¯ã¾ã ããã®ã§ãããè¦æ¯ã¹ã¦æ¢ãã¦ã¿ã¾ãããã
forward = function() subDef(turtle.forward, "turtle.forward()", "-- turtle.forward()") end back = function() subDef(turtle.back, "turtle.back()", "-- turtle.back()") end
- 1è¡ç®ã¨3è¡ç®ï¼ç¡åé¢æ°ä½ææ
- function() ï½ end
ãï¼ã
ããã§ã¯ããã®è¨äºã®ã¿ã¤ãã«ã§ãããé¢æ°ãå®ç¾©ããé¢æ°ãå®ç¾©ããã¦ã¿ã¾ãããã
function createLoggedFunc(func, succeeded_log, failed_log) return function() local status, error_msg = func() if status then write(succeeded_log) else write(failed_log) end return status, error_msg end end forward = createLoggedFunc(turtle.forward, "turtle.forward()", "-- turtle.forward()") back = createLoggedFunc(turtle.back, "turtle.back()", "-- turtle.back()")
ãã¤ã³ãã¯2è¡ç®ã®return
ã§ãã
é¢æ°createLoggedFunc()ã¯ã以ä¸ã®ãããªæ§é ããã¦ãããå®è¡ããã¨ç¡åé¢æ°ãè¿ãã¦ãããã®ã§ãã
function createLoggedFunc() return (function() ï½ end) end
次åã®ã話
以ä¸ãããã®æ¹æ³ã§ä½ã£ãããã°ã©ã å®æçã§ãã
ãã®ããã°ã©ã ã®è©³ç´°ãªè§£èª¬ã¯ã¾ã次åãã楽ãã¿ã«ã