Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
Exception_17

How does a function in event.listen() get the signal's data?

Question

3 answers to this question

Recommended Posts

  • 0

The modem's wiki is here: https://ocdoc.cil.li/component:modem

event.listen() returns all the variables given by the event being called. This is the modem's return:

local ev, to, from, port, dist, ... = event.pull("modem_message")

ev is the event that is returned, which for the modem should equal "modem_message"

to is the address of the modem that received it (the wireless network card that is installed on the machine)

from is the address of the modem that sent the message

port is the port that the message was received on

dist is the distance away the computer which sent the message is

... is any other variables that the computer passes. For instance, if you were to run 

component.modem.broadcast(123,"Hello","World")

and your event.pull looked like

local ev, to, from, port, dist, message1, message2, message3 = event.pull("modem_message")

port would be set to 123, message 1 would equal "Hello", and message2 would equal "World". message3 would just be nil

 

Hope this helped

Link to post
Share on other sites
  • 0
3 hours ago, cadergator10 said:

The modem's wiki is here: https://ocdoc.cil.li/component:modem

event.listen() returns all the variables given by the event being called. This is the modem's return:


local ev, to, from, port, dist, ... = event.pull("modem_message")

ev is the event that is returned, which for the modem should equal "modem_message"

to is the address of the modem that received it (the wireless network card that is installed on the machine)

from is the address of the modem that sent the message

port is the port that the message was received on

dist is the distance away the computer which sent the message is

... is any other variables that the computer passes. For instance, if you were to run 


component.modem.broadcast(123,"Hello","World")

and your event.pull looked like


local ev, to, from, port, dist, message1, message2, message3 = event.pull("modem_message")

port would be set to 123, message 1 would equal "Hello", and message2 would equal "World". message3 would just be nil

 

Hope this helped

So i just make like this:?

function f()
  local _,_,_,_,_,msg = event.pull("modem")
  print(msg)
end

event.listen("modem", f())

 

Link to post
Share on other sites
  • 0
On 2/8/2023 at 12:25 PM, Exception_17 said:

So i just make like this:?


function f()
  local _,_,_,_,_,msg = event.pull("modem")
  print(msg)
end

event.listen("modem", f())

 

That should work. However I think in order to get modem messages the filter has to be "modem_message" instead of "modem". And what that code is going to do is it waits for a modem message. When it receives the message it runs f(), which then waits for another message before printing it out.

These should work:

while true do
  local _,_,_,_,_,msg = event.pull("modem_message")
  print(msg)
end

and if you really want to use event.listen:

function f(_,_,_,_,_,msg)
  print(msg)
end

event.listen("modem_message",f)
  

although I wouldn't recommend event.listen unless you have some error handling or checks for interrupting your program in order to remove the listener (listeners persist otherwise until you restart the computer, which can cause some issues)

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.