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

Ignoring events

Question

Basically I have a program which, among other things, registers an event listener with:

event.listen("modem_message",move)

I would like it to only respond once to receiving a message, but unfortunately each time I run the program it registers a 'new' listener, so it runs 'move' multiple times. So I tried to set it to unregister the listener at the start of the program, using:

event.ignore("modem_message",move)

But this doesn't work, seemingly because the current definition of 'move' is treated as different to the definition which was registered as a listener. So how would I go about removing the registered listener?

 

p.s. I'm currently rebooting the computer each time I need to rerun the program, which is mostly when I change the definition of move, but this is rather annoying.

Link to post
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Maybe you can use something like that:

local event = require("event")
local computer = require("computer")

local TEST = tostring(...)

local function move(...)
  print(TEST, ...) --your code
end

--This function removes the listener when the next program pushes the signal "update_listeners"
local function removeListener()
  event.ignore("modem_message", move)
  --removes this function
  return false
end
--This function adds the listener.
local function addListener()
  event.listen("modem_message", move)
  --The internal list of listeners is updated after all listeners have been executed.
  --That means that the following function will not be executed in this step:
  event.listen("update_listeners", removeListener)
  --removes this function
  return false
end

event.listen("update_listeners", addListener)
computer.pushSignal("update_listeners")
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.