sleepå®è£ ããã¤ãã³ããå¦ã¶ï¼ï¼ï¼ï¼Too long without yieldingã¨ã©ã¼ã®è§£æ±ºæ¹æ³ï¼
ã¨ã©ã¼ãé¿ããã¾ãå¥ã®æ¹æ³
前回ã®è¨äºã§ããToo long without yieldingããé¿ããããã«å®æçã«ãsleepããå ¥ãããã¨ããã話ããã¾ããã
ãããå®ã¯ããã以å¤ã«ãã¨ã©ã¼ãé¿ããæ¹æ³ãããã¾ãã
ï¼ããã«æ°ã¥ããã®ã¯ã¤ãæè¿ãªãã§ãããããããè¸ã¾ãã¦ãããã¾ã§ã®ããã°ã©ã ãä¿®æ£ãããã©ããæ©ã¿ä¸ï¼
Turtle APIã使ãã¨ã¨ã©ã¼ãèµ·ããªã
ã¾ãã次ã®ããã°ã©ã ãå®è¡ãã¦ã¿ã¾ãããã
-- ã¿ã¼ãã«ã®é¸æã¹ãããã1ã¨2交äºã«åãæ¿ããã延ã ã¨ããã¤ã¾ã§ãã while true do turtle.select(1) turtle.select(2) end
ãããå®è¡ããã¨ããã¤ã¾ã§ãã£ã¦ããToo long without yieldingãã¨ã©ã¼ãçºçãã¾ããã
ã¾ããturtle.select()ãã ãã§ãªããTurtle APIã®ä»ã®é¢æ°ã使ã£ã¦ãã¨ã©ã¼ãçãã¾ããã
ãªãã ã¨æãã¾ããï¼
ã¨ã©ã¼ãèµ·ããªãçç±
ååã®ã話ãèªãã§ãããªãããªãã¨ãªãæ³åã¤ãã¾ãããï¼
ãTurtle APIèªä½ããã³ã«ã¼ãã³å«ãã§ããããããï¼ã
çµè«ããè¨ãã¨æ£è§£ã§ãã
証æ ãè¦ãã¾ãããã
ãComputerCraft1.53/lua/rom/apis/turtle/turtleãã¨ãããã¡ã¤ã«ãè¦ã¦ã¿ã¾ãããã
ï¼CCã³ã³ãã¥ã¼ã¿å´ããè¦ããªãã/rom/apis/turtle/turtleãã§ããï¼
以ä¸ã«éè¦ãªé¨åãæç²ãã¾ããã
local function waitForResponse( _id ) local event, responseID, success while event ~= "turtle_response" or responseID ~= _id do event, responseID, success = os.pullEvent( "turtle_response" ) end return success end local function wrap( _sCommand ) return function( ... ) local id = native[_sCommand]( ... ) if id == -1 then return false end return waitForResponse( id ) end end
注ç®ãã¹ãã¯ãä¸ãã4è¡ç®ã«ãããos.pullEvent( "turtle_response" )ãã§ãã*1ã
waitForResponse()é¢æ°ã¯ãos.pullEvent()ãå«ãã§ãã¾ãã
ããã¦ãã®waitForResponse()é¢æ°ã¯ãwrapé¢æ°ã§ä½¿ããã¦ãã¾ãã
wrapã¯ããã®åã®ã¨ãã*2ãTurtle APIã®ãã»ã¨ãã©ãã®é¢æ°ã§èªåçã«å¼ã°ãã¾ãã
ã ããTurtle APIã®é¢æ°ãå®æçã«ä½¿ãéãããToo long without yieldingãã¨ã©ã¼ãçããªãã®ã§ãã
ã§ããªãTurtleAPIãos.pullEventãå¿ è¦ã¨ããã®ï¼
os.pullEvent( "turtle_response" )ã¯ã"turtle_response"ã¤ãã³ããæ¾ãä¸ãã¦ãã¾ãã
éå
¬å¼CC wikiã®ã¤ãã³ãä¸è¦§ããããã®ã¤ãã³ãã«é¢ãã説æãå¼ç¨ãã¾ãããã
第1è¿å¤ | 第2è¿å¤ | 第3è¿å¤ | 説æ |
---|---|---|---|
"turtle_response" | ã³ãã³ãIDï¼æ°å¤ï¼ | ã³ãã³ãã®æå¦ï¼ãã¼ãªã¢ã³ï¼ | ã«ã¹ã¿ã ã³ãã³ããå®è¡ããã |
ã¤ã¾ããturtle.select()ã®ãããªã³ãã³ããæåãããã©ããã®æå¦ãè¿ãã¤ãã³ãã®ããã§ããã
ãã£ã¦ãåè¿°ã®wrapé¢æ°ã大ã¾ãã«è§£èª¬ãããªãã°ã
- Turtle APIã®åé¢æ°ãå®è¡ããã¨ã
- ã³ãã³ããå®è¡ï¼wrapé¢æ°ä¸ãnativeããã¬ãã®é¨åï¼
- ã³ãã³ããæ£è¦ã®ãã®ã§ããã°ãã³ãã³ãIDãè¿ã
- ãããªã³ãã³ãããããå®è¡ã§ããªããã¨ããå ´åã«-1ã¨ããIDãè¿ãï¼ï¼ãããããturtle.select(100)ãªã©ã®ãããªæããã«å®æ½ä¸è½ãªå ´åã«è¿ãã¨æãããï¼
- ã³ãã³ãIDã-1ãªã失æï¼falseï¼ãè¿ãã
- ããã§ãªããªããwaitoForResponse(id)ããå®æ½ãã¦ããã®çµæãè¿ãã
waitoForResponse(id)ã®è§£èª¬
- å®æ½ããã³ãã³ããå®éã«æåãããã©ããã"turtle_response"ã¨ãã¦åãåã
- ãã£ã¨æ£ç¢ºã«è¨ãã¨ãããã§Luaã®å¶å¾¡ãä¸æ¦ä¸æ¢ãã¦å¶å¾¡ãJavaå´ã«æ»ããJavaå´ã§"turtle_response"ã¤ãã³ããçãããLuaã®å¦çãåéããã
- ãªãwhileã«ã¼ãã®çç±ã¯ãããã§å¦çãããã³ãã³ãã§ãããæ£ç¢ºã«å¤æããããã§ããéãã³ãã³ãã®ã¤ãã³ãã ã£ãããã¾ãos.pulllEvent()ãã¦ãå¶å¾¡ãJavaã«æ»ãã¾ãã
ã¤ã¾ãã©ããããã¨ã ã£ã¦ã°ãï¼
Turtle APIã使ã£ã¦ã¿ã¼ãã«ã«ä½ãããããã¨ããæåãããtrueã失æãããfalseããè¿ãã¨ããååãããã¾ãã
ãã¨ãã°ãturtle.suckUp()ãtrueãè¿ããã©ããã§ãã¢ã¤ãã ã²ããã«æåãããã©ããããããã¾ãããã
ãã®æåã»å¤±æãæ£ç¢ºã«å¤æãããããã«ãwrapããå§ã¾ãä¸é£ã®é¢æ°ã使ã£ã¦ããããã§ãã
- ãå½ä»¤ããããããããããªãããã¨ããå¤æãwrapé¢æ°é¨åã§è¡ãã
- ããã¦ãå®éã«ãã®å½ä»¤ãå®è¡ãã¦æåãããã©ãããã¨ããé¨åã"turtle_respense"ã¤ãã³ãã«ããå¤æãã
ç§ã®ç°å¢ã¯CC1.53ãªã®ã§ããã®ç¨åº¦ã®ã·ã³ãã«ãªå¦çã§ãããCC1.56以éã¯ãåç´ã«true/falseã ããããªãã失æããçç±ã¾ã§ã¨ã©ã¼ã¡ãã»ã¼ã¸ã§è¿ãã¦ãããããã§ããã
ãããããCC1.56以éã§ã¯ããã®wrapé¢æ°ãããã§ãã¨ã©ã¼ã¡ãã»ã¼ã¸ã«é¢ããå¦çãè¡ã£ã¦ããã®ã§ã¯ãªããã¨äºæ³ãã¦ãã¾ãã
ãã®ãã¡æéãããã¨ãã«ã調ã¹ã¦ã¿ããããªã
ã¾ã¨ã
ååã®ã¾ã¨ãã«è¿½è¨ãã¾ããã
- CCã§ããã°ã©ã çµãã¨ãã«ã¯ãå®æçã«ã³ã«ã¼ãã³ãå¼ã³ãªï¼
- ãããªãã¨ããToo long without yieldingãã¨æãããã
- ã³ã«ã¼ãã³ãä½ãªã®ãããããªãã¦ã大ä¸å¤«ãos.pullEvent()ã®ãã¨ã ããã
- å¥ã«ä¿ºã®ããã°ã©ã ã§ã¤ãã³ãå¦çããæ°ã¯ãªããã ãã©ãâã ã£ããsleep使ããªï¼
- ãããããsleep使ã£ããããã°ã©ã ã®åä½ãé ããªãããããâãsleep(0)使ãï¼ã0.05ç§ãããææ ¢ããªããã
- Turtle APIãå®æçã«ä½¿ãã¨ãã®ã¨ã©ã¼ã¯èµ·ããªãã®ãããããã¿ã¼ãã«åï¼ï¼ï¼ãã ãä¾å¤ããï¼ã
- å®æçã«ã¨è¨ãããã©ãå®éã«ã©ã®ãããã®æéãã¨ãªã®ã・・・・・・ãä½æã§ã¯5ç§ãããï¼
- 5秒でしたã
ããã«è£è¶³
å®ã¯ãã®TurtleAPI以å¤ã«ããwrapãã¦os.pullEvent()ã使ãAPIã¯ããã¾ãã以ä¸ã«ãã¿å ã§ãããCC公式WikiのcoroutineページããããpullEvent()ãwrapãã¦ããAPIä¸è¦§ããæç²ãã¾ãã
- rednet.receive blocks until either a rednet_message event or (if a timeout was requested) a matching timer event occurs.
- gps.locate blocks until either a number of modem_message events or (if a timeout was requested) a matching timer event occurs.
- http.get blocks until an HTTP success event or HTTP failure event occurs.
- io.read and the built-in read functions block waiting for, presumably, either char events or key events.
- os.sleep blocks until a matching timer event occurs.
- textutils.slowPrint calls os.sleep.
- textutils.pagedPrint blocks until a key event occurs for each page.
- Most of the functions in the turtle API (with the exception of turtle.getItemCount, turtle.getItemSpace, and turtle.getFuelLevel) block waiting for a turtle response event.
çµæ§ããã¾ãããã§ãæ°è»½ã«ä½¿ããããªã®ã¯ãos.sleep()ã¨Turtle APIãããã§ããããæ®å¿µã
ããã¦ãTurtleAPIã®ä¸ã§ããturtle.getItemCount()ã turtle.getItemSpace()ãturtle.getFuelLevel()ã¯ä¾å¤ã®ããã§ãããããã3ã¤ã®é¢æ°ã¯ä½¿ã£ã¦ããã¨ã©ã¼ãé²ãã¾ãããæ°ãã¤ãã¾ãããã