sleepå®è£ ããã¤ãã³ããå¦ã¶ï¼ï¼ï¼ï¼Too long without yieldingã¨ã©ã¼ã®è§£æ±ºæ¹æ³
ã¯ããã«
CCããã°ã©ã ãæ¸ãã¦ããã¨ãå¿
ãä¸åº¦ã¯åºä¼ã
ãToo long without yieldingãã¨ã©ã¼
ããã解決ããããã«ä¸è¬çã«è¨ãããã®ãããsleep()ãå ¥ããã
ãªãï¼ãsleepå
¥ãããããã°ã©ã ã®å®è¡ãé
ããªããããªãã
ãã ã§ããããã°ã©ã å®è¡ã¨ããéãModãªã®ã«ã
ããã°ã©ã ãããã«é
ããããªãã¦ã¤ãã¤ãã
ãªãã¦æã£ã¦ããã¨ããããã¾ããã
ä»åã®è¨äºã¯ããã®ã¨ã©ã¼ã«ã¤ãã¦è§£èª¬ãããã¨æãã¾ãã
ãªããæ¬æã¯é常ã«é·ãã§ããã¨ã©ã¼ã®çç±ã«èå³ããªããã¨ã©ã¼ãé¿ããããã®TIPSã ããç¥ãããã¨ããæ¹ã¯ããã®è¨äºã®æ«å°¾ã®ãã¾ã¨ããã ãèªãã§ããã ããã°çµæ§ã§ãã
ãToo long without yieldingãã¨ã©ã¼ãããã¨èµ·ãã
以ä¸ã®ãããªããã°ã©ã ãå®è¡ããã¨ãã ããã5ç§åå¾ã§ããToo long without yieldingãã¨ã©ã¼ãçããããã°ã©ã ãã¨ã¾ãã¾ãã
-- ãã ã²ãããã«ç»é¢ã«aã表示ãç¶ããããã°ã©ã while true do print("a") end
-- å·¦ããæ¥ç¶ããã¬ããã¹ãã¼ã³ä¿¡å·ãONã«ãªãã¾ã§ä½ããããããã°ã©ã repeat -- ããã«ããããå¦çãæ¸ãã¦ãã until rs.getInput("left")
sleepå ¥ããã¨è§£æ±ºãã
ãã®ãããªã¨ã©ã¼ãçããå ´åã¯ã以ä¸ã®ããã«sleepãå ¥ãã¦ããããã¨ã§ããã¦ã解決ãã¾ãã
-- ãã ã²ãããã«ç»é¢ã«aã表示ãç¶ããããã°ã©ã while true do print("a") sleep(0) -- sleep 1tick (0.05sec) end
ã§ãããã¾ã§ã®é£è¼ã§ãsleepã¯ãã¿ã¤ãã¼ã¤ãã³ã使ã£ã¦å¾
ã¤ã ããã®é¢æ°ã§ãããã¨ãããã£ã¦ãã¾ãã
ãªãããã§è§£æ±ºããã®ã§ãããã
ãToo long without yieldingãã®çç±
ï¼ä»¥ä¸ã®çç±ã«ã¤ãã¦ã®æç« ã¯åã®æ³åã¨ä»®èª¬ã大é¨åã§ããç¹ã«CCä½è ã®è¨è¨æå³ãæ¨æ¸¬ããé¨åã«ã¤ãã¦ã¯å¤§ããééãã¦ããå¯è½æ§ãããã¾ãããããæå³ã¯ã¨ãããã¨ãã¦å®éã«ãªãããè¨è¨ã«ã¤ãã¦ã¯ããããåã£ã¦ããã¯ãã§ãããã®ãããèè ã®æ¹ã®ãææããå¾ ã¡ãã¦ããã¾ãï¼
Minecraftæ¬ä½ã¨ãã®Modã§ããComputerCraftï¼CCï¼ãJavaã§æ¸ããã¦ãããã¨ã¯ã¿ãªãããåç¥ã®ã¨ããã§ãã
ããã¦CCã¯ã¦ã¼ã¶ã¼ãä½ã£ãLuaããã°ã©ã ãå®è¡ã§ãã¾ããã
ããã¯CCèªä½ã«ãLuaJã¨ããJavaã§æ¸ãããLuaããã°ã©ã å®è¡ç°å¢ï¼Javaã§æ¸ãããLuaã¨ã³ã¸ã³ï¼ãçµã¿è¾¼ã¾ãã¦ããããã§ãã
CCã¯ãã²ã¨ã¤ã®Luaããã°ã©ã ãå®è¡ããããã«ãJavaã¹ã¬ããçæâLuaJã使ã£ã¦Luaããã°ã©ã å®è¡ã¨ããæé ãç¹°ãè¿ãã¦ããã¨æããã¾ãã
ããããã®ã¿ã¼ãã«ãåæã«åãããã¨ãã§ããã®ã¯ããã®ãããã§ããããã¶ãã
ãããããã§åé¡ãªã®ããã¦ã¼ã¶ã¼ãä½ã£ãLuaããã°ã©ã ãç¡éã«ã¼ãï¼ãããã¯ããã«ã»ã¼çãããããå¦çã®éãï¼ããã°ã©ã ã§ããå ´åã§ãã
Javaã¹ã¬ããã使ã£ã¦Luaããã°ã©ã ãå®è¡ããã¯ããããã©ããã¤ã¾ã§ãããã°ã©ã ãçµããããç¹å®ã®ã¹ã¬ãããæ®ãç¶ãã¦ããã
Javaå´ããè¦ãã¨ã¹ã¬ããããã¤ã¾ã§ãæ®ã£ã¦ããã®ã¯å°ãã¾ããããè¨ç®æ©è³æºã®ç¡é§ã§ãã
ãã¾ã Luaããã°ã©ã çµãããªãã®ï¼ãããããã¦ããç¡éã«ã¼ãã®ããã°ã©ã ãªã®ï¼ãããã¨ãæ®éã«ããã°ã©ã å®è¡ä¸ãªã®ï¼ãã¾ãããã°ã§ããã°ã©ã ãæ´èµ°ãã¦ãããããªãããï¼ããããã®ã¹ã¬ããçãã¦ããã®ï¼æ»ãã§ããã®ï¼çãã¦ãï¼ã
ãã®äºæ
ã®è§£æ±ºæ¹æ³ã¨ãã¦ã
Luaããã°ã©ã ãå¼ã³åºãã¦å®è¡ããããã®ã¾ã¾å¦çãçµããã¾ã§å»¶ã
ã¨å¾
ã¡ç¶ããã¨ããæ¾ä»»ä¸»ç¾©æ¦ç¥ã§ã¯ãªããä¸å®æéã ãLuaããã°ã©ã ãå®è¡ãããéä¸ã§ä¸æãåé¡ãªããã¨ã確èªãããã¾ããã®ç¶ããå®è¡ããã¨ããåä½ãç¹°ãè¿ãæ¦ç¥ãã¤ã¾ãã¯å°åºãæ¦ç¥ãæ¡ããã¨ã«ãã¾ããã
é »ç¹ã«å¶å¾¡ãLuaããã°ã©ã å¼ã³åºãå´ï¼ã¤ã¾ãã¯Javaå´ï¼ã«æ»ãã®ã§ããããä¸æ¸¬ã®äºæ
ã«ã対å¿ã§ãã¾ãããã
ãããããã«ãåé¡ãããã¾ããéä¸ã§ä¸æããã¨ããããã©ãã©ã®ã¿ã¤ãã³ã°ã§ä¸æããã°ããã®ã§ããããJavaå´ã®åæãªã¿ã¤ãã³ã°ã§ä¸æãããã¨å°ãã¾ãããã
ãã¨ãã°ãç·ç»ãã¢ãã¡ã¼ã·ã§ã³ãããLuaããã°ã©ã ã§åæãªã¿ã¤ãã³ã°ã§å¦çãä¸æãããã¨ããã¨ãããå¦çãåéããã«ãããã¿ã¤ãã³ã°ãçãããã§ãããåããã«ã¯ã«ã¯ã«ãªã£ã¦ãã¾ãã¾ãã
ããã§ä¸æã®ã¿ã¤ãã³ã°ã¯ãJavaå´ãåæã«å¤æããã®ã§ã¯ãªããCCããã°ã©ãï¼CCã¦ã¼ã¶ã¼ï¼å´ã«ä»»ãããã¨ã«ãã¾ããã
ããã°ã©ã ä¸ã§ä¸æãã¦ãè¯ãã¿ã¤ãã³ã°ããCCããã°ã©ãå´ãæå®ããããã§ãã
ã§ã¯ããã®æå®ã¯ã©ããã£ã¦è¡ãã®ã§ãããã
ããã¦ããããããLuaå´ãæå®ããã¿ã¤ãã³ã°ã§å¦çãä¸æããä¸æããå ´æããã¾ããã®ç¶ããåéãããã¨ããæ©æ§ã¯ã©ããã£ã¦å®ç¾ãã¦ããã®ã§ããããã
Luaã®ç¬èªæ©æ§ãã³ã«ã¼ãã³
ãã®ããã®æ©æ§ã¨ãã¦ãLuaã«ã¯coroutineï¼ã³ã«ã¼ãã³ï¼ã¨ãã便å©ãªæ©è½ï¼é¢æ°ï¼ãå®è£
ããã¦ãã¾ãããããLuaãããçµã¿è¾¼ã¿ç¨ã¨ãã¦è¨è¨ããã¦ããã®ã¯ä¼éã§ã¯ããã¾ããã
詳ãã説æããã¨é·ããªãã®ã§èª¤è§£ãæããç°¡æ½ã«ã¾ã¨ãã¾ãããããã¯ããããã«ããå¤é¨ããã°ã©ã ãLuaããã°ã©ã ãå¼ã³åºãã¦å®è¡ããã¨ããããããã®Luaããã°ã©ã å
ã«ã³ã«ã¼ãã³é¢æ°ãå«ã¾ãã¦ãããããã®å ´æã§ä¸æçã«Luaããã°ã©ã ã®å¦çãä¸æããå¤é¨ããã°ã©ã ã«å¦çãæ»ããã¨ããæ©è½ã§ãã
å¦çãä¸æã¨ãå¦çãæããã¨ããã¨breakãreturnãæãã¤ãã¾ãããã³ã«ã¼ãã³ã«ã¯ãä¸ææã®ç¶æ
ãå®å
¨ã«ä¿æãããããä¸æãããã®ç¶æ
ãã®å ´æããä½äºããªãã£ããã®ããã«åã¹ã¿ã¼ãã§ãããã¨ãããé常ã«å¼·åã§ä¾¿å©ãªç¹å¾´ãããã¾ãã
CCã®ä½è
ã§ããdan200æ°ã¯ããLuaããã°ã©ã ã¯ãå®æçã«ã³ã«ã¼ãã³é¢æ°ã使ã£ã¦Javaå´ã«å¶å¾¡ãæ»ããªãã¦ã¯ãªããªããã¨ããã«ã¼ã«ãä½ãã¾ããã
ããããªããã°ããToo long without yieldingãã¨ã©ã¼ã§å¦çãã¨ãããã¨ã
ã¦ã¼ã¶ã¼ã§ããç§ãã¡ã¯ãã®æåã«å¾ããªãã¦ã¯ãªãã¾ããã
ããããããå®æçã«ä¸æãããã¨ãæèããªããããã°ã©ãã³ã°ããã®ï¼ãããã©ãããããããæã£ãããªãããå®å¿ãã ããã
dan200æ°ã¯ããã ã®ä¸æã®ç®å°ã¨ãã¦ã³ã«ã¼ãã³æ©æ§ãæ¡ç¨ããã®ã§ã¯ãªããéè¦ãªå½¹å²ãããããä¸ã§æ¡ç¨ãã¾ããã
ãã®éè¦ãªå½¹å²ãæããããã使ãããé¢æ°ã®ä¸ã«ã³ã«ã¼ãã³é¢æ°ãåãè¾¼ã¾ãã¦ããã®ã§ãããã°ã©ã ä¸ã«ï¼æ¯è¼çï¼èªç¶ã«ã³ã«ã¼ãã³ãå
¥ãè¾¼ãããã«ãªã£ã¦ãã¾ã*1ã
å
·ä½çã«ã©ã®é¢æ°ã«ã³ã«ã¼ãã³é¢æ°ãåãè¾¼ã¾ãã¦ããã®ãã示ãã¾ãããã
以åãsleep()ãå®ç¾©ããã¦ãããã¡ã¤ã«ã¨ãã¦ããComputerCraft1.53/lua/bios.luaããç´¹ä»ãã¾ããã
ãã®ä¸ã«ã次ã®ãããªè¨è¿°ãããã¾ãã
function os.pullEventRaw( _sFilter ) return coroutine.yield( _sFilter ) end function os.pullEvent( _sFilter ) local eventData = { os.pullEventRaw( _sFilter ) } if eventData[1] == "terminate" then error( "Terminated", 0 ) end return unpack( eventData ) end
- os.pullEventRaw()ã¯ãã³ã«ã¼ãã³ã®å¥åé¢æ°
- ããã¾ã§ä½åº¦ã使ã£ã¦ããos.pullEvent()ã¯ãos.pullEventRaw()ã«"terminate"ã¤ãã³ãï¼CTRL+Tï¼ã«é¢ããå¦çãä»ãå ãããã®*2
ãã¤ã¾ããos.pullEvent()ã¯ãã³ã«ã¼ãã³ãã®ãã®ã ã£ããã ãï¼ã
ããããããªãã ã£ã¦ã¼ã¼ï¼AAç¥ï¼ã
os.pullEvent()ã¨è¨ãã°ã¤ãã³ãã§ãããã
ã¤ã¾ãCCã®ã¤ãã³ãé¢é£ã®æ©è½ã¯ãã³ã«ã¼ãã³ã«ãã£ã¦å®è£
ããã¦ããã®ã§ãã
os.pullEvent()ã®ã¯ããã
CCæ¬ä½ã®ã½ã¼ã¹ãè¦ããã¨ãã§ããªãã®ã§ããã¾ã§äºæ³ã§ããã
å¤é¨ããã°ã©ã ãããããJavaå´ãå種ã¤ãã³ãã®çºçã管çãã¦ããã¯ãã§ãã
ã³ã«ã¼ãã³ã§Luaã®å¦çãä¸æãJavaå´ã«æ»ã£ã¦ãããã¤ãã³ããçºçããã¾ã§ãã®ã¾ã¾å¾
æ©ã
ããã¦ä½ããã®ã¤ãã³ãã®çºçã¨ã¨ãã«ãå度ãã®ä¸æããä½ç½®ããLuaããã°ã©ã å¦çãåéãã¦ããã®ã ã¨æããã¾ãã
ããã¾ã§ããos.pullEvent()ã¯ã¤ãã³ããæ¾ãé¢æ°ãã¨èª¬æãã¦ãã¾ãããã
ãã£ã¨æ£ç¢ºã«è¨ããªãã°ããos.pullEvent()ã¯Luaã®å¦çãä¸æãã¦Javaå´ã«å¶å¾¡ãæ»ããããã¦ã¤ãã³ãçºçã¨ã¨ãã«å¦çãåéãããé¢æ°ã¨èª¬æããã®ãæ£ããã®ã§ãããããã¶ããããããã
ãã®ãããã¤ãã³ãããªãã³ãªï¼ãªãããã®ã¤ãã³ããããªã¬ã¼ã¨ãã¦åãã ãï¼ããã°ã©ã ã§ããã°ãos.pullEvent()ããã¤ãã¦ã¯ã³ã«ã¼ãã³ãããã°ã©ã ä¸ã«èªç¶ã¨å«ã¾ããããã«ãªãã¾ãã
ãããã¤ãã³ããå¿
è¦ã¨ããªãããã°ã©ã ãä½ãã¨ãã¯ãæèãã¦os.pullEvent()ã使ããããããããã¾ãããã
sleep()ã¯ã¿ã¤ãã¼ã¤ãã³ãã使ã£ã¦ããã®ã§ãsleepã使ãã°èªåçã«os.pullEvent()ã使ããã¨ã«ãªãã¾ããæ軽ãªã®ã§ãå§ãã§ãã
ä»åã®ã¾ã¨ã
ã¾ããé·ã
ã¨æ¸ãã¾ãããåå¿è
ã®æ¹ã«ã¯é£ããé¨åãããã¨æãã¾ãã
ãã®å ´åã¯ã以ä¸ã ãã§ãè¦ãã¦ãã ããã
- CCã§ããã°ã©ã çµãã¨ãã«ã¯å®æçã«ã³ã«ã¼ãã³(coroutine.yeild)ã使ãï¼
- ãããªãã¨ããToo long without yieldingãã¨æãããã
- ã³ã«ã¼ãã³ãä½ãªã®ãããããªãã¦ã大ä¸å¤«ãos.pullEvent()ã®ãã¨ã ããã
- å¥ã«ä¿ºã®ããã°ã©ã ã§ã¤ãã³ãå¦çããæ°ã¯ãªããã ãã©ãâã ã£ããsleep使ããªï¼
- ãããããsleep使ã£ããããã°ã©ã ã®åä½ãé ããªãããããâãsleep(0)使ãï¼ã0.05ç§ãããææ ¢ããªããã
- å®æçã«ã¨è¨ãããã©ãå®éã«ã©ã®ãããã®æéãã¨ãªã®ã・・・・・・ãä½æã§ã¯5ç§ãããï¼
- 5秒でしたã
- å®ã¯ãä»ã«ãããã®ã¨ã©ã¼ãé¿ããæ¹æ³ãããã¾ããããã«ã¤ãã¦ã¯ã¾ã次回ã
*1:Luaçµã¿è¾¼ã¿ã使ã£ã¦ããã²ã¼ã ï¼å¦çç³»ï¼ã¯ä»ã«ãæ°å¤ãããããã§ããããããã®ä¾ãç¥ããªãã®ã§ãã®è¨è¨ãä¸è¬çãªã®ãã©ããã¯ãããã¾ããããããããã®è¨è¨ææ³ãåªãã¦ãããã¨ã¯ç¢ºãã§ãã
*2:errorã¨ããé¢æ°ã使ããã¦ãã¾ãã便å©ããã§ãããä»åº¦ä½¿ã£ã¦ã¿ã¾ãããã