ComputerCraftã®ç¡ç·éä¿¡ã使ãããªãã(9)ãâå½ä»¤ã®æå¹æ§ãæåã»å¤±æã調ã¹ã
ã¯ããã«
ååã®æå¾ã«ã以ä¸ã®ãããªèª²é¡ãæãã¾ããã
å½ä»¤ããã¨ãã«ãå½ä»¤ã®éè¡ã«æåããã®ã失æããã®ããããã¦å¤±æããã®ãªãã©ã®ãããªåå ã§å¤±æããã®ãããããã®ã¨ã©ã¼ãè¿ã£ã¦ããã¨ãããã«ãããããã使ãããããªãã¾ãããã
ãã®èª²é¡ãä»åã¨æ¬¡åã®2åã«åãã¦ã段éãè¸ãã§å®è£ ãã¦ã¿ã¾ãããã
ã¾ãä»åã¯ãå½ä»¤ã¡ãã»ã¼ã¸ãããããæå¹ãªã®ãã©ãããããã¦å®è¡ãããæåããã®ã失æããã®ãã«ã¤ãã¦ãã©ã®ããã«å¤æãããããã調ã¹ã¦ã¿ã¾ãããã
ãªãããã§ã¯ã以ä¸ã®ï¼ã¤ã®ã¬ãã«ã®ã¨ã©ã¼ãåºå¥ãã¦èª¿ã¹ã¦ãã¾ããã注æãã ããã
- æå®ããé¢æ°ãããããåå¨ããªãã¨ãã«çºçããé大ãªã¨ã©ã¼ãããã°ã©ã ãå¼·å¶åæ¢ããã
- å®è¡ãããä½ããã®åå ã§å¤±æããã¨ãã«çºçãã軽微ãªã¨ã©ã¼ãçºçãã¦ãç¹ã«å¦çãæ¸ããªããã°ãã®ã¾ã¾ããã°ã©ã ã¯ç¶ãã
- 第1è¿å¤ã¨ãã¦falseã第2è¿å¤ã¨ãã¦ãã®å¤±æåå ãè¿ããããªæ¯è¼ç軽微ãªã¨ã©ã¼ã
- ãã¨ãã°é¢æ°
turtle.down()
ã¯ãçæããªã移åã§ããªãã¨ãã«ç¬¬1è¿å¤falseã第2è¿å¤"Out of fuel"ãè¿ãã
ä»åã¯ãLuaã®ç´°ããªä»æ§ã«ã¤ãã¦èª¿æ»ã»æ¤è¨ããå 容ã¨ãªã£ã¦ãã¾ãã ãã»ã©èå³ã®ãªãæ¹ã¯ããããã®ãã¾ã¨ããã ãèªãã§ããã ãã¦ããã¾ãã¾ããã
loadstring()ã®è¿å¤ã«ã¤ãã¦èª¿æ»
ããã¾ã§ã®ããã°ã©ã ã§ã¯ãåãåã£ãã¡ãã»ã¼ã¸æååãloadstring()
ã使ã£ã¦é¢æ°ã«ã³ã³ãã¤ã«ãã¦ãã¾ãããããã§ã¯ãã®å¼æ°ã«ããããªå¤ãä¸ãããã©ããªãã®ã§ããããã
func = loadstring("turtle.select(1)") print(type(func))
ä¸ã®ã³ã¼ãã«ä»¥ä¸ã®ãããªæååãå½ã¦ã¯ãã¦èª¿ã¹ãçµæãã以ä¸ã®è¡¨ã«ãªãã¾ãã
å¼æ°æåå | funcã®å¤ | åè |
---|---|---|
turtle.select(1) | function | å®è¡å¯è½ãªæ£ç¢ºãªé¢æ°æååãä¸ããã¨ãé¢æ°ã |
aaa() | function | åå¨ããªãé©å½ãªé¢æ°ã£ã½ãæååãä¸ãã¦ããé¢æ°ã |
aaa | nil | ããããé¢æ°ã§ããªãé©å½ãªæååãä¸ããã¨ãnilã |
ã¾ã¨ããã¨ã
ã¨ã«ããé¢æ°ã£ã½ãæååï¼æ«å°¾ã«ã()ãï¼ãä¸ããã¨é¢æ°ã«ãªããé¢æ°ä»¥å¤ã®æååãä¸ããã¨nilãã¨ãªãã¾ãã
åé¡ã¨ãªãã®ã¯ãaaa()ã®ãããªå®è¡ä¸å¯è½ãªé¢æ°ã£ã½ãæååãä¸ãã¦ãé¢æ°ãã§ãã¦ãã¾ãç¹ã§ãããã¤ã¾ãloadstring()ã ãã§ã¯ãã¨ã©ã¼ã詳細ã«å¤å¥ããã®ãé£ããããã§ãã
pcall()ã詳細ã«æ¤è¨ãã
ããã§ã¯ãåºæ¥ä¸ãã£ãé¢æ°ãå®è¡ããpcall()ãªãã°ç´°ããå¤å¥ã§ããã®ã§ããããããã®pcall()ã®æ©è½ã¯ä»¥åã®è¨äºã§ç´¹ä»æ¸ã¿ã§ããã念ã®ããåæ²ãã¾ãã
- pcall(f, arg1, arg2, ã»ã»ã»)
- pcall()ã¯ä¸è¨ã§ããã¨ããé¢æ°ãå®è¡ãã¦ã¨ã©ã¼ãçºçããããããæã¾ãã¦ãfalseã¨ã¨ã©ã¼ã¡ãã»ã¼ã¸ãè¿ããé¢æ°ã§ãã
- å¼æ°1ï¼ç¡åé¢æ°ï¼ï¼ãå®è¡ãããé¢æ°ãä¸ãã¾ã
- å¼æ°2以éããã®ç¡åé¢æ°ã«ä¸ããå¼æ°ãããã¤ã§ã
- è¿å¤1ï¼boolean)ï¼ãã¹ãã¼ã¿ã¹ã³ã¼ããé¢æ°å®è¡æåãªãtrueãã¨ã©ã¼ãçãããfalseã
- è¿å¤2以éï¼ãé¢æ°å®è¡æåãªãé¢æ°å®è¡ã«ããè¿å¤ããã¨ã©ã¼çºçæã«ã¯ã¨ã©ã¼ã¡ãã»ã¼ã¸ãè¿ãã
- å®è¡ä¾ï¼ pcall(func, arg1, arg2)ãã¯ã func(arg1, arg2)ãå®è¡ãã¦ã¨ã©ã¼çºçãæããã¦å¦çãè¡ãã¾ãã
ãã¤ã³ãã¯ãè¿å¤ã§ããã
ç°¡åã«è¨ãã¨ãä¸çªç®ã®è¿å¤ã®true/falseã«ãã£ã¦ãããã°ã©ã ãå¼·å¶çµäºãããããªé大ãªã¨ã©ã¼ãçºçãããã©ãããå¤æã§ãã¾ããå½ç¶ãªãããfalseãªãã¨ã©ã¼çºçã§ãã
ããããéã«ãã®è¿å¤1ãtrueã ã¨ãã¦ããå¿ ãããé¢æ°ã®å®è¡ã«æåããã¨ã¯éãã¾ããã
ãã¨ãã°ãã¿ã¼ãã«ãä¸ã«ç§»åããturtle.down()
é¢æ°ãå®è¡ããã¨ãã«ãé害ç©ããã£ããçæã足ããªãã£ããªã©ã®çç±ã§å®è¡ã«å¤±æããã¨ãã«ã¯è¿å¤1ã¨ãã¦trueãè¿ãã¨åæã«ãè¿å¤2ã¨ãã¦falseãè¿å¤3ã¨ãã¦å¤±æããçç±ãè¿ãã¾ãã
- コンピュータクラフト非公式Wiki - API/Turtleãããã¿ã¼ãã«ç§»åç³»ã®é¢æ°ãå®è¡ããã¨ãã«è¿ã失æçç±ã®è¡¨ãå¼ç¨ãCC1.56以ä¸ã®ãã¼ã¸ã§ã³ããå®è£ ã
失æããçç±ï¼è¿å¤3ï¼ | æå³ |
---|---|
Movement obstructed | ãããã¯ãmobããã¬ã¤ã¤ã¼ãªã©ã«å¦¨å®³ããã |
Out of fuel | çæåã |
Too high to move | ãããã¯ãè¨ç½®ã§ããéçé«åº¦ã«ãã |
Too low to move | ãããã¯ãè¨ç½®ã§ããéç深度ã«ãã |
pcall()ã«ã¤ãã¦ããã£ããã¨
pcall()ã«ã¤ãã¦ã¾ã¨ãã¾ãã
local results = {pcall(func)} print(results[1]) print(results[2]) print(results[3])
ä¸è¨ã®ãããªã³ã¼ãã«ãã£ã¦ãpcall()ã®è¿å¤ãã¹ã¦ããã¼ãã«ã³ã³ã¹ãã©ã¯ã¿{}
ã«ãã£ã¦ãã¼ãã«results
ã«ä»£å
¥ã§ãã¾ãã
以ä¸ã¯ãfuncã®å¤ã«ãã£ã¦ã©ã®ãããªè¿å¤ã¨ãªãã®ãã調ã¹ã表ã§ãã
funcã®å¤ | results[1] | results[2] | results[3] | åè |
---|---|---|---|---|
é¢æ°turtle.up | true | true | - | åé¡ãªã移åæåã®ã¨ã |
é¢æ°turtle.up | true | false | "Out of fuel" | çæä¸è¶³ã«ãã移å失æ |
åå¨ããªãé¢æ°aaa() | false | "attempt to call nil" | - | ãããããããªé¢æ°ã¯ãªãï¼ |
pcall()ã®3ã¤ã®è¿å¤ã使ãã°ãã¾ã失æã®ç¨®é¡ãå¤å¥ã§ãããã§ããã
ããã¦å½ç¶ã®ããã«åå¨ããç½
ããã§å¤å¥ã¯ç°¡åã ï¼ãã¨æ²¹æãã¦ã¯ããã¾ããã次ã®ã³ã¼ããå®è¡ãã¦ã¿ã¾ãããã
ç¶æ³ã¨ãã¦ã¯ãåè¿°ã®çæä¸è¶³ã«ãã移å失æã®ã¨ã©ã¼ã¨ãªããã¨ãæ³å®ãã¦ãã¾ãã
local func = loadstring("turtle.up()") local results = {pcall(func)} print(results[1]) print(results[2]) print(results[3])
ããããã¨ãè¿å¤ï¼ï¼results[1]ï¼ãtrueã¨ãªãã®ã§ããã2çªç®ä»¥éã®è¿å¤ã帰ã£ã¦ãã¾ããã
ããã ãã¼ï¼
å®ã¯ãé¢æ°loadstring()ã«ãã£ã¦ã³ã³ãã¤ã«ããé¢æ°ã¯ããæ示çã«returnæãåãè¾¼ã¾ãªãã¦ã¯å¤ãè¿ããªããã¨ããæ§è³ªãããã¾ãã
local func = loadstring("return ".."turtle.up()") local results = {pcall(func)} print(results[1]) print(results[2]) print(results[3])
ãã®ãããªã³ã¼ãã§ããã°ãå¼æ°2ã¨å¼æ°3ããã£ããã¨å¾ããã¾ãï¼ã¡ãªã¿ã«..
ã«ãã£ã¦äºã¤ã®æååãé£çµãã¾ãï¼ã
ãã®æ¹å¼ã®éçã«ã¤ãã¦
ãªãã次ã®ãããªé¢æ°ãè¤æ°ä¸¦ã¹ãæååã®ã¨ãã«ã¯"return "ãé ã«ã¤ããæ¹å¼ã¯éç¨ãã¾ããã
-- ã»ãã³ãã³ã;ãã¯è¡åºåãè¨å·ã local str = "turtle.turnRight(); turtle.forward(); turtle.turnLeft()" local func = loadstring("return "..str) local results = {pcall(func)} print(results[1]) print(results[2]) print(results[3])
ã ã£ã¦ã"return turtle.turnRight(); turtle.forward(); turtle.turnLeft()" ã¨ããã®ã¯ãLuaã®ææ³çã«æå³ä¸æã§ãããã
ããããã3ã¤ã®é¢æ°ã®ã©ãã«æ³¨ç®ãã¦å¤ãè¿ãã®ãããä¸æã§ããã
ã ãããæ¬ããã°ã©ã ã§ã¯ãè¡åºåãè¨å·ã§ããã»ãã³ãã³ã;ããç®å°ã«ããã®ãããªæååã«ã¯"return "ãã¤ããªããã¨ã«ãã¾ãã
ã³ã©ããã£ï¼ãææãã£ã¦è¨ããªãï¼ï¼
ã¾ã¨ã
loadstirng() â pcall()ãã¨ããæµãã§æååãé¢æ°ã¨ãã¦å®è¡ããã¨ãã
- pcall()ã®3ã¤ã®è¿å¤ã®ãã¿ã¼ã³ã調ã¹ããã¨ã§ãé大ãªã¨ã©ã¼ã®çºçï¼ãããªé¢æ°ã¯ç¡ãï¼ï¼é¢æ°å®è¡ãã¦æåï¼é¢æ°å®è¡ãã¦å¤±æï¼è»½å¾®ãªã¨ã©ã¼çºçï¼ã®å¤å¥ãã§ããã
- ãã ããloadstring(æåå)ã§é¢æ°ãçæããã¨ãã«ã"return turtle.up()"ã®ããã«é ã«"return "ãã¤ããå¿
è¦ãããã
- ãã®æ¹å¼ãéç¨ããªãå ´åãããã®ã§ãã®ã¨ãã¯ãããããï¼P
次åã¯ãä»åå¤æãããã®ï¼ã¤ã®äºé ãå ã«ã
å½ä»¤ããã¨ãã«ãå½ä»¤ã®éè¡ã«æåããã®ã失æããã®ããããã¦å¤±æããã®ãªãã©ã®ãããªåå ã§å¤±æããã®ãããããã®ã¨ã©ã¼ãè¿ã£ã¦ããã¨ãããã«ãããããã使ãããããªãã¾ãããã
ã¨ãã課é¡ãå®ç¾ã§ããããã°ã©ã ãä½ãã¾ãããã