This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
fatboychummy's profile picture

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

Started by fatboychummy, 14 October 2016 - 11:18 PM
fatboychummy #1
Posted 15 October 2016 - 01: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 on 14 October 2016 - 11:20 PM
Dog #2
Posted 15 October 2016 - 01: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 on 14 October 2016 - 11:52 PM
fatboychummy #3
Posted 15 October 2016 - 02:50 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

Alright, now I assume the variable that would be assigned as the which key was detected would be id?
KingofGamesYami #4
Posted 15 October 2016 - 03:02 AM
Alright, now I assume the variable that would be assigned as the which key was detected would be id?

That would be correct.
fatboychummy #5
Posted 15 October 2016 - 03:41 AM
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 on 15 October 2016 - 01:47 AM
KingofGamesYami #6
Posted 15 October 2016 - 03: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.
fatboychummy #7
Posted 15 October 2016 - 03:55 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.

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…).
KingofGamesYami #8
Posted 15 October 2016 - 03:59 AM
I would recommend googling "Lua tables tutorial". It's probably the best source for them.
Bomb Bloke #9
Posted 15 October 2016 - 04:04 AM
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.

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 on 15 October 2016 - 02:06 AM
fatboychummy #10
Posted 15 October 2016 - 04:27 AM
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?

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).
Bomb Bloke #11
Posted 15 October 2016 - 05:11 AM
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.
fatboychummy #12
Posted 16 October 2016 - 01:29 AM
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.
fatboychummy #13
Posted 17 October 2016 - 12:05 AM
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 on 16 October 2016 - 10:05 PM