ComputerCraftã®ç¡ç·éä¿¡ã使ãããªãã(2)ãâRednetAPIã®è§£èª¬
ã¯ããã«
ååã¯ç¡ç·éä¿¡ã·ã¹ãã ã«ã¤ãã¦æ¦è¦ã説æãã¾ããã
ä»åã¯Rednet APIãç´¹ä»ãã¤ã¤ãå®éã«åããã¦ã¿ã¾ãããã
Rednet APIã使ã£ãã¡ãã»ã¼ã¸ã®éåä¿¡
WirelessModemã®ä½¿ç¨æºå
ã¾ãæåã«ãWireless Modemã®è£
çæ¹åã確èªãã¾ãã
ããã¦ãã®æ¹åãå¼æ°ã«rednet.open()
é¢æ°ãå®è¡ãããã¨ã§ãå¨è¾ºæ©å¨ã使ç¨å¯è½ãªç¶æ
ã«ãã¾ãã
ã¤ã¡ã¼ã¸ã¨ãã¦ã¯ããã£ã³ãã«åç·ãéãæºåããã¦ããã¨æãã°ããã§ãããã
ã¡ãã»ã¼ã¸ãéä¿¡ããå´ãåä¿¡ããå´ããå¿ ãæåã«ä½¿ç¨æºåããã¦ãã ããã
ãªãã¢ãã ã¯ãã¿ã¼ãã«ãªãã°å³å´ã«è£ çããã¦ãã¾ãããã³ã³ãã¥ã¼ã¿ãªãã°ä»»æã®æ¹åã«è£ çãããã¨ãã§ãã¾ãã
-- å³å´ã«è£ çããWirelessModemã®ä½¿ç¨æºåããã rednet.open("right")
- æ¹åã¨ãã¦æå®ã§ããæååï¼ "top", "bottom",ã"right", "left", "front", "back"
è£è¶³ï¼è£ çæ¹åã«ã¤ãã¦ã®æ³¨æ
- è£
çãããå¨è¾ºæ©å¨ã®æ¹åãèããã¨ããã³ã³ãã¥ã¼ã¿ï¼ç»åä¸ã§é»è²ï¼ãªãã°ããã¬ã¤ã¤ã¼ã¨ã³ã³ãã¥ã¼ã¿ç»é¢ãåããåã£ãç¶æ
ã§ãã¬ã¤ã¤ã¼ããè¦ãæ¹åãèãã¾ããã¤ã¾ãç»åä¸ã§ã¢ãã ãè£
çããã¦ããã®ã¯ãã¬ã¤ã¤ã¼ããè¦ã¦å³å´ãªã®ã§ã
right
ã§ãã - ããã«å¯¾ãã¦ã¿ã¼ãã«ï¼ç»é¢ä¸ã§èµ¤ï¼ã§ã¯ãã¿ã¼ãã«ããè¦ãæ¹åãèãã¾ããç»åã§ã¯ã¿ã¼ãã«ã®å³æå´ã«ã¢ãã ãè£
çããã¦ããã®ã§ããã¡ãã
right
ã¨æå®ãã¾ãã - ãããã¯ãã³ã³ãã¥ã¼ã¿ï¼ã¿ã¼ãã«ï¼ãè¨ç½®ããã¨ãã«ããã®ã¾ã¾ç´ ç´ã«ãã¬ã¤ã¤ã¼ããè¦ã¦ã©ã¡ãå´ãã§å¤æãã¦ãè¯ãããããã¾ããã
- è£
çãããå¨è¾ºæ©å¨ã®æ¹åãèããã¨ããã³ã³ãã¥ã¼ã¿ï¼ç»åä¸ã§é»è²ï¼ãªãã°ããã¬ã¤ã¤ã¼ã¨ã³ã³ãã¥ã¼ã¿ç»é¢ãåããåã£ãç¶æ
ã§ãã¬ã¤ã¤ã¼ããè¦ãæ¹åãèãã¾ããã¤ã¾ãç»åä¸ã§ã¢ãã ãè£
çããã¦ããã®ã¯ãã¬ã¤ã¤ã¼ããè¦ã¦å³å´ãªã®ã§ã
ã¡ãã»ã¼ã¸ãéä¿¡ãã
rednet.send()
é¢æ°ã«ãã£ã¦ãæå®ãããã£ã³ãã«ã«ãæååããéä¿¡ãã¾ãã
éä¿¡å ã³ã³ãã¥ã¼ã¿IDãããããã調ã¹ã¦ããããã®IDã¨åããã£ã³ãã«IDã使ã£ã¦ãã ããã ãã¨ãã°ãID2ã®ã³ã³ãã¥ã¼ã¿ã«ã¡ãã»ã¼ã¸ãéä¿¡ãããã®ãªãã°ããã£ã³ãã«2ã使ãã¾ãã
rednet.send(ãã£ã³ãã«ID, ã¡ãã»ã¼ã¸æåå)
- ã¡ãã»ã¼ã¸éä¿¡ã«æåããããã®é¢æ°ã¯trueãè¿ãã
- ãã ã注æãããã®ã¯ããéä¿¡ãã«æåã§ãã£ã¦ãç¸æããåä¿¡ãããã©ãããã¯ä¿è¨¼ãããªãã
- ãéä¿¡æåã¨è¨ã£ããããããã®ã¡ãã»ã¼ã¸ãç¸æãåãåã£ãã¨ã¯è¨ã£ã¦ãªãï¼ããªãã
ã¡ãã»ã¼ã¸åä¿¡ãã
rednet.receive()
é¢æ°ã¯ã
èªåã®IDã®ãã£ã³ãã«ãç£è¦ãã¤ã¤å¾ æ©ãã¡ãã»ã¼ã¸æååãæµãã¦ãããåä¿¡ãããã¨ããæ©è½ãæã¡ã¾ãã ã¾ããBROADCASTï¼åæéä¿¡ï¼ç¨ã®ãã£ã³ãã«ã§ãã65535ã«æµãã¦ããã¡ãã»ã¼ã¸ãåãåããã¨ãã§ãã¾ãã
ãã®é¢æ°ã¯åä¿¡ãã£ã³ãã«ãé¸ã¶ãã¨ã¯ã§ãã¾ãããèªåIDã®ãã£ã³ãã«ã¨BROADCASTãã£ã³ãã«ã®ã¿ã§ãã
rednet.receive(å¾ ã¡æé)
- å¼æ°ã§æå®ããå¾ ã¡æéï¼ç§ï¼ã ãèªåIDï¼ã¨BROADCASTç¨ï¼ã®ãã£ã³ãã«ãç£è¦ãã¤ã¤å¾ æ©ãå¾ ã¡æéãçç¥ãããã¡ãã»ã¼ã¸ãæµãã¦ããã¾ã§æ°¸é ã«å¾ ã¡ç¶ããã
- è¿ãå¤ã¯ä»¥ä¸ã®ã¨ãã
- 第1è¿å¤ï¼ãéä¿¡è ã®IDï¼æ°å¤ï¼
- 第2è¿å¤ï¼ãã¡ãã»ã¼ã¸ï¼æååï¼
- 第3è¿å¤ï¼ãèªåã¨éä¿¡è éã®è·é¢ï¼æ°å¤ï¼
ãµã³ãã«ããã°ã©ã
ã¨ã¦ãã·ã³ãã«ãªããã°ã©ã ãªã®ã§ãå®éã«è©¦ãã¦ã¿ãã¨ç解ããããã§ãã
ãããããã®ã³ã¼ãéãªãã¿ã¼ããã«ç»é¢ã§edit
ã³ãã³ãã使ã£ã¦å
¥åãã¦ãããã»ã©æéã§ã¯ãªãã§ãããã
以ä¸ã®ããã°ã©ã ã¯ãï¼ï¼ï¼åä¿¡å´ããã°ã©ã ï¼ï¼åä¿¡å¾ æ©ï¼ãï¼ï¼ï¼éä¿¡å´ããã°ã©ã ï¼ï¼ã¡ãã»ã¼ã¸éä¿¡ï¼ã®é ã«å®è¡ãã¦ãã ãããé çªãééããªãããã«ã
ã¾ãããããããåä¿¡è å´ã®IDã調ã¹ã¦ããã®ãå¿ããªãã§ãã ãããWirelessModemã®è£ çæ¹åã¨åããã¦ãèªåã®ç°å¢ã«ãã£ã¦ããã°ã©ã ãæ¸ãæãã¦ãã ããã
- ã³ã³ãã¥ã¼ã¿ã»ã¿ã¼ãã«ã®IDã調ã¹ãã«ã¯ãã¿ã¼ããã«ç»é¢ã§
id
ã³ãã³ãã使ãã¾ããããï¼åä¿¡å´ã§ããã¿ã¼ãã«ã®IDã¯21ã§ãããï¼
ã¡ãã»ã¼ã¸åä¿¡å´ã§åããããã°ã©ã ãï¼âãã¡ãããå ã«å®è¡ï¼
ï¼ï¼ï¼å³å´ã«è£ çãããã¢ãã ã®ä½¿ç¨æºåãï¼ï¼ï¼èªåã®IDã®ãã£ã³ãã«ã§ã¡ãã»ã¼ã¸å¾ ã¡ãããï¼ï¼ï¼ã¡ãã»ã¼ã¸ãåä¿¡ãããç»é¢ã«è¡¨ç¤ºã
rednet.open("right") local senderID, message, distance = rednet.receive() print(message,' from ID',senderID)
- ãã¤ã³ãã¯ã
rednet.receive()
ããã®è¿ã£ã¦ãã3ã¤ã®è¿å¤ãããããå¤æ°ã«ä»£å ¥ãã¦ããã¨ããã§ãï¼ã¡ãªã¿ã«ããã®ããã«ä¸åº¦ã«è¤æ°ã®å¤æ°ã¸ä»£å ¥ãããã¨ãå¤éä»£å ¥ã¨å¼ã³ã¾ãï¼ã - ã¾ããã¡ãã»ã¼ã¸ãç»é¢è¡¨ç¤ºããã¨ãã«ã誰ããéããã¦ããã¡ãã»ã¼ã¸ãªã®ãããã®IDãåæã«è¡¨ç¤ºãã¾ãã
ã¡ãã»ã¼ã¸éä¿¡å´ã§åããããã°ã©ã ãï¼â次ã«ãã¡ãï¼
ï¼ï¼ï¼å³å´ã«è£ çãããã¢ãã ã®ä½¿ç¨æºåãï¼ï¼ï¼ãã£ã³ãã«ID21ã使ã£ã¦ãhelloãã¨ããã¡ãã»ã¼ã¸ãéä¿¡ã
rednet.open("right") rednet.send(21, "hello")
å®è¡çµæ
åä¿¡å´ãâãéä¿¡å´ãã®é ã«ããã°ã©ã ãåããã¦ãæçµçã«åä¿¡å´ã®ã³ã³ãã¥ã¼ã¿ã®ã¿ã¼ããã«ç»é¢ã«ã"hello"ã表示ããã¦ãããæåã§ãã
ãã®ç»åã§ã¯ãID20ãããhelloãã¨ããã¡ãã»ã¼ã¸ãéããã¦ããã¨ãããã¨ããããã¾ããã
次åã®ã話
次åã¯ããã®ã¡ãã»ã¼ã¸éä¿¡ããã°ã©ã ãããããã使ãããããã¾ãã ãã¨ãã°ããã®ããã°ã©ã ã§ã¯ã1åã¡ãã»ã¼ã¸ãéä¿¡ã»åä¿¡ãããããã§çµäºã§ããã
- ä½åº¦ã§ãã¡ãã»ã¼ã¸ãåãåããã¨ãã§ããããã«ãåä¿¡å´ã¯ã¡ãã»ã¼ã¸ãåä¿¡ãããã¾ããã£ã³ãã«ç£è¦ã¢ã¼ãã«å ¥ãããã«ã¼ãããã
- éä¿¡å´ã¯ããã°ã©ã å®è¡æã®å¼æ°ã§ä»»æã®æååãéããããã«ããã
- åä¿¡å´ã2å°ã«ãã¦ãéä¿¡å´ãBROADCASTï¼ä¸æéä¿¡ï¼ãã£ã³ãã«ã«ã¡ãã»ã¼ã¸ãéä¿¡ãããã©ããªããã
ãªã©ãäºå®ãã¦ãã¾ãã