Jump to content




using rednet.recieve() and os.pullEvent("key") in tandem, is it possible?


  • You cannot reply to this topic
12 replies to this topic

#1 fatboychummy

  • Members
  • 25 posts

Posted 15 October 2016 - 12:18 AM

I want to wait for a keypress OR a rednet message, but am unsure how to.


I put
id, mes, prt = rednet.recieve()
_, key = os.pullEvent("key")
but then it will wait for a rednet message and only after it recieves one will it move onto waiting for the keypress.

Or could I possibly do this with os.pullEventRaw?

Edited by fatboychummy, 15 October 2016 - 12:20 AM.


#2 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 15 October 2016 - 12:51 AM

Use os.pullEvent() without a filter then use if statements, like so...
local event, id, message, protcol = os.pullEvent()
if event == "rednet_message" then
  --# do rednet stuff here
elseif event == "key" then
  --# do key stuff here
end

Edited by Dog, 15 October 2016 - 12:52 AM.


#3 fatboychummy

  • Members
  • 25 posts

Posted 15 October 2016 - 01:50 AM

View PostDog, on 15 October 2016 - 12:51 AM, said:

Use os.pullEvent() without a filter then use if statements, like so...
local event, id, message, protcol = os.pullEvent()
if event == "rednet_message" then
  --# do rednet stuff here
elseif event == "key" then
  --# do key stuff here
end

Alright, now I assume the variable that would be assigned as the which key was detected would be id?

#4 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 15 October 2016 - 02:02 AM

View Postfatboychummy, on 15 October 2016 - 01:50 AM, said:

Alright, now I assume the variable that would be assigned as the which key was detected would be id?

That would be correct.

#5 fatboychummy

  • Members
  • 25 posts

Posted 15 October 2016 - 02:41 AM

View PostKingofGamesYami, on 15 October 2016 - 02:02 AM, said:

View Postfatboychummy, on 15 October 2016 - 01:50 AM, said:

Alright, now I assume the variable that would be assigned as the which key was detected would be id?

That would be correct.

Just did some testing; I am running LurCraft's Relived modpack which is 1.7.10, I guess rednet_message was the wrong one. It is apparently modem_message.

Also, Thanks for the help!
edit: Also, apparently ID does not run as the computer ID anymore. I may need to use a few more variables. I will just test by placing random variables around in print() functions

Edit2: Keyvalues are wierd, but okay. A is 30, b is 48, c is 25...?

Edited by fatboychummy, 15 October 2016 - 02:47 AM.


#6 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 15 October 2016 - 02:46 AM

rednet_message and modem_message are different events. modem_message does not give you the computer ID, as that is actually part of a rednet packet.

#7 fatboychummy

  • Members
  • 25 posts

Posted 15 October 2016 - 02:55 AM

View PostKingofGamesYami, on 15 October 2016 - 02:46 AM, said:

rednet_message and modem_message are different events. modem_message does not give you the computer ID, as that is actually part of a rednet packet.

Odd, it does in fact give the computer ID, but the part we have assigned to protocol is the one that gives it.


But..... Now my problem is, is that all the rednet stuff is stuffed into a table. I am unsure how to open a table and read the contents though. (This is my first time working with tables, believe it or not...).

#8 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 15 October 2016 - 02:59 AM

I would recommend googling "Lua tables tutorial". It's probably the best source for them.

#9 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 15 October 2016 - 03:04 AM

View Postfatboychummy, on 15 October 2016 - 02:41 AM, said:

Just did some testing; I am running LurCraft's Relived modpack which is 1.7.10, I guess rednet_message was the wrong one. It is apparently modem_message.

The deal is that all networking is done via modem_message events, but CraftOS runs a coroutine alongside your code which checks for these, and, if it determines they were sent using rednet.send(), generates corresponding rednet_message events (unpacking the table for you for eg). You DO want to work with these rednet_message events, not the original modem_message events.

View Postfatboychummy, on 15 October 2016 - 02:41 AM, said:

Keyvalues are wierd, but okay. A is 30, b is 48, c is 25...?

You can see the map here, but usually you're better off using the keys API. Eg:

if event == "key" and id == keys.a then

That's if you simply want to know which buttons were pressed on the keyboard, though. If you want to know which characters were generated, use char events instead:

if event == "char" and id == "a" then

Edited by Bomb Bloke, 15 October 2016 - 03:06 AM.


#10 fatboychummy

  • Members
  • 25 posts

Posted 15 October 2016 - 03:27 AM

View PostBomb Bloke, on 15 October 2016 - 03:04 AM, said:

View Postfatboychummy, on 15 October 2016 - 02:41 AM, said:

Just did some testing; I am running LurCraft's Relived modpack which is 1.7.10, I guess rednet_message was the wrong one. It is apparently modem_message.

The deal is that all networking is done via modem_message events, but CraftOS runs a coroutine alongside your code which checks for these, and, if it determines they were sent using rednet.send(), generates corresponding rednet_message events (unpacking the table for you for eg). You DO want to work with these rednet_message events, not the original modem_message events.
Running a test, I have found that the version of CraftOS the server is using (1.7) does not run this coroutine(Or was disabled or something) so no matter what, whether it is sent with rednet.broadcast or rednet.send it recieves it, and labels it as modem_message events, without automatically fixing it to be a proper rednet_message. I will have to manually unpack the table.

Or... extra thought, these rednet_message events are probably generated afterwards aren't they? Wouldn't I then need two os.pullevents?

View PostBomb Bloke, on 15 October 2016 - 03:04 AM, said:

View Postfatboychummy, on 15 October 2016 - 02:41 AM, said:

Keyvalues are wierd, but okay. A is 30, b is 48, c is 25...?

You can see the map here, but usually you're better off using the keys API. Eg:

if event == "key" and id == keys.a then

That's if you simply want to know which buttons were pressed on the keyboard, though. If you want to know which characters were generated, use char events instead:

if event == "char" and id == "a" then
oh, alright with the keys then. those are working properly now that I got the keymap (And the numbers assigned make sense now lol).

#11 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 15 October 2016 - 04:11 AM

View Postfatboychummy, on 15 October 2016 - 03:27 AM, said:

Or... extra thought, these rednet_message events are probably generated afterwards aren't they? Wouldn't I then need two os.pullevents?

Well yeah, your main coroutine gets both the modem_message and the rednet_message event, one after the other and in that order.

If you want to pull one event of a single type, ignoring all others, then you set a filter:

local event, par1, par2, par3 = os.pullEvent("rednet_message")

If you want events of multiple types, then you just rig up a loop and some if/then checks:

while true do
  local event, par1, par2, par3 = os.pullEvent()

  if event == "rednet_message" then
    -- do stuff

  elseif event == "key" then
    -- do other stuff

  end
end

The latter example will pull modem_message events, but the if/then block won't act on them, and the loop will have it simply pull the next event from the queue as soon as it drops in there.

#12 fatboychummy

  • Members
  • 25 posts

Posted 16 October 2016 - 12:29 AM

View PostBomb Bloke, on 15 October 2016 - 04:11 AM, said:

while true do
  local event, par1, par2, par3 = os.pullEvent()

  if event == "rednet_message" then
	-- do stuff

  elseif event == "key" then
	-- do other stuff

  end
end

The latter example will pull modem_message events, but the if/then block won't act on them, and the loop will have it simply pull the next event from the queue as soon as it drops in there.
will try this one and let you know.

#13 fatboychummy

  • Members
  • 25 posts

Posted 16 October 2016 - 11:05 PM

Aha, couldn't get your solution to work at first but I figured out what I was doing wrong (I wrote rednet.message instead of rednet_message, sometimes I think I'm dumber than a rock.)! Thanks!

Edited by fatboychummy, 16 October 2016 - 11:05 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users