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

os.pullEventRaw call more than 1 event?

Started by Mr_Programmer, 03 January 2016 - 11:00 PM
Mr_Programmer #1
Posted 04 January 2016 - 12:00 AM
hi all,

im wondering if its possiable to check for 2 events at once?

i want to have a moden receiveing things but if i click on the screen i want the modem to stop and for me to be able to click on things

is this possiable or not using the os.pullEventRaw?

 local event, button, X, Y = os.pullEventRaw("mouse_click") 

could i just add it on to where i have the "mouse_click" or not?

i dont know what to do?
valithor #2
Posted 04 January 2016 - 12:10 AM
If I remember correctly, os.pullEventRaw only accepts a single argument, so you can not listen for multiple events. The simpliest way to overcome this is to not filter out events, and instead check the event name of all events.

Example:

while true do
  local event, button, X, Y = os.pullEventRaw()
  if event == "mouse_click" then
	--# mouse was clicked, so run the code you would have run if you were filtering for mouse events
  elseif event == "modem_message" then
	--# received a modem message
  end
end

If that doesnt work for what you are wanting to do, you could always make a function like this:

local function multiPullEvent(...)
  while true do --# going to loop until we find the event we are wanting
	local event = {os.pullEventRaw()} --# catching the event in a table
	for k,v in pairs({...}) do --# going to loop through the arguments passed to this function
	  if v==event[1] then --# checking to see if the event name matches one of the arguments
		return unpack(event) --# unpack splits the event table up into its arguments
	  end
	end
  end
end
Edited on 03 January 2016 - 11:15 PM
Mr_Programmer #3
Posted 04 January 2016 - 12:20 AM
If I remember correctly, os.pullEventRaw only accepts a single argument, so you can not listen for multiple events. The simpliest way to overcome this is to not filter out events, and instead check the event name of all events.

Example:

while true do
  local event, button, X, Y = os.pullEventRaw()
  if event == "mouse_click" then
	--# mouse was clicked, so run the code you would have run if you were filtering for mouse events
  elseif event == "modem_message" then
	--# received a modem message
  end
end

If that doesnt work for what you are wanting to do, you could always make a function like this:

local function multiPullEvent(...)
  while true do --# going to loop until we find the event we are wanting
	local event = {os.pullEventRaw()} --# catching the event in a table
	for k,v in pairs({...}) do --# going to loop through the arguments passed to this function
	  if v==event[1] then --# checking to see if the event name matches one of the arguments
		return unpack(event) --# unpack splits the event table up into its arguments
	  end
	end
  end
end

I think the first one will work but what is the difference between rednet_message and modem_message? how would i then convert the rednet message into like a print?

 if event == "modem_message" then
print(event)
end 

Would that work or not?
Edited on 03 January 2016 - 11:20 PM
valithor #4
Posted 04 January 2016 - 12:27 AM
I think the first one will work but what is the difference between rednet_message and modem_message? how would i then convert the rednet message into like a print?

 if event == "modem_message" then
print(event)
end 

Would that work or not?

A modem message is a message sent directly from the modem api.

A rednet message is a message sent from the rednet api (rednet uses the modem api in the background, but it formats them in a way that the receiving computer knows it is a rednet message).

If you are sending the messages with the rednet api then listen for rednet messages instead of modem messages.

For rednet api (is what I am assuming you are using):
os.pullEvent/os.pullEventRaw will return the arguments of the event in the same order no matter what the variable names before it are, so the first variable is the event name as always, the second is the senderID, the third is the message itself, and the fourth is the protocol, so you would do something like:

if event =="rednet_message" then
  print(X) --# X was the third variable that was before the os.pullEventRaw, so it is what the message was set to
end

edit:

I forgot to mention this, but it might be smarter to use a table to catch the event rather than variables, as then you are just referencing the table instead of using variable names
example:

while true do
  local event = {os.pullEventRaw()}
  if event[1] == "mouse_click" then
	--# mouse was clicked
	--# event[2] is what the button was
	--# event[3] is x
	--# event[4] is y
  elseif event[1] == "rednet_message" then
	--# rednet message received
	--# event[2] is the sender id
	--# event[3] is the message
	--# event[4] is the protocol
  end
end
Edited on 03 January 2016 - 11:33 PM
Mr_Programmer #5
Posted 04 January 2016 - 12:29 AM
Aghh Okay, Ill give it ago, so if i want it to check what the senders ID is then i would have to:

 print(X) 
Edited on 03 January 2016 - 11:32 PM
valithor #6
Posted 04 January 2016 - 12:33 AM
Aghh Okay, Ill give it ago, so if i want it to check what the senders ID is then i would have to:

 print(X) 

Sorry apparently I cannot count… X would be the message and button would be the senderID I will go fix the example i posted now.

edit:

but yes if what i had posted the first time was right, then that would be the senderID
Edited on 03 January 2016 - 11:36 PM
Mr_Programmer #7
Posted 04 January 2016 - 12:37 AM
Aghh Okay, Ill give it ago, so if i want it to check what the senders ID is then i would have to:

 print(X) 

Sorry apparently I cannot count… X would be the message and button would be the senderID I will go fix the example i posted now.

Ok, this is my "mouse_click" code

cf = 0
configM = true

while configM == true do
local event, button, X, Y = os.pullEventRaw("mouse_click")
if cf == 0 then
if event == "mouse_click" then
if X >=19 and X <=34 and Y == 7 and button == 1 then
configM = false
renameSpawners()
elseif X >=43 and X <=52 and Y == 1 and button ==1 then
cf = 1
drawMainMenu()
elseif X >=3 and X <=14 and Y == 17 and button == 1 then
configM = false
CompMenu()
else
configMainMenu()
end
end
elseif cf == 1 then
if X >=35 and X <=51 and Y == 7 and button == 1 then
userDSP = false
userLLDSP = true
StartupLog = false
Monlog()
login()
elseif X >=36 and X <=49 and Y == 9 and button == 1 then
rednet.send(ServerID, "systemReboot")
os.reboot()
elseif X >=35 and X <=50 and Y == 11 and button == 1 then
os.shutdown()
else
configMainMenu()
end
end
end

I was having a error yesterday where the "mouse_up" was undoing what the "mouse_click" did so i was told to put:
local event, button, X, Y = os.pullEventRaw("mouse_click") --# mouse click inside the brackets 

so if i take that off to let the "rednet_message" trigger somthing then i will still have the "mouse_up" messing with the menus and stuff, how would i fix that?
Edited on 03 January 2016 - 11:42 PM
KingofGamesYami #8
Posted 04 January 2016 - 12:44 AM
Like suggested in the other thread, you should use a variable to keep track of when the menu is visible / not visible.
Edited on 03 January 2016 - 11:45 PM
valithor #9
Posted 04 January 2016 - 12:45 AM
Aghh Okay, Ill give it ago, so if i want it to check what the senders ID is then i would have to:

 print(X) 

Sorry apparently I cannot count… X would be the message and button would be the senderID I will go fix the example i posted now.

Ok, this is my "mouse_click" code

cf = 0
configM = true

while configM == true do
local event, button, X, Y = os.pullEventRaw("mouse_click")
if cf == 0 then
if event == "mouse_click" then
if X >=19 and X <=34 and Y == 7 and button == 1 then
configM = false
renameSpawners()
elseif X >=43 and X <=52 and Y == 1 and button ==1 then
cf = 1
drawMainMenu()
elseif X >=3 and X <=14 and Y == 17 and button == 1 then
configM = false
CompMenu()
else
configMainMenu()
end
end
elseif cf == 1 then
if X >=35 and X <=51 and Y == 7 and button == 1 then
userDSP = false
userLLDSP = true
StartupLog = false
Monlog()
login()
elseif X >=36 and X <=49 and Y == 9 and button == 1 then
rednet.send(ServerID, "systemReboot")
os.reboot()
elseif X >=35 and X <=50 and Y == 11 and button == 1 then
os.shutdown()
else
configMainMenu()
end
end
end

I was having a error yesterday where the "mouse_up" was undoing what the "mouse_click" did so i was told to put:
local event, button, X, Y = os.pullEventRaw("mouse_click") --mouse click inside the brackets 

so if i take that off to let the "rednet_message" trigger somthing then i will still have the "mouse_up" messing with the menus and stuff, how would i fix that?

The reason that "mouse_up" was messing you up originally is because you only check for the "mouse_click" event in the if statement that checks if cf == 0. This means that when you click the loop runs once with the mouse_click event, and cf is set to 1. Then the loop runs again with the "mouse_up" event, and because cf is 1 it runs the second if statement that does not check to make sure the event was a "mouse_click" event, and it undoes what you just did. Easiest way to fix is to just move the check for the "mouse_click" event to where everything that uses it is inside of that if statement.

edit:

cf = 0
configM = true

while configM == true do
local event, button, X, Y = os.pullEventRaw("mouse_click")
if event == "mouse_click" then --# notice I moved this if statement out of the cf if statement
if cf == 0 then
if X >=19 and X <=34 and Y == 7 and button == 1 then
configM = false
renameSpawners()
elseif X >=43 and X <=52 and Y == 1 and button ==1 then
cf = 1
drawMainMenu()
elseif X >=3 and X <=14 and Y == 17 and button == 1 then
configM = false
CompMenu()
else
configMainMenu()
end
-- end --# this end also needed to be removed and added at the end since we moved the if statement
elseif cf == 1 then
if X >=35 and X <=51 and Y == 7 and button == 1 then
userDSP = false
userLLDSP = true
StartupLog = false
Monlog()
login()
elseif X >=36 and X <=49 and Y == 9 and button == 1 then
rednet.send(ServerID, "systemReboot")
os.reboot()
elseif X >=35 and X <=50 and Y == 11 and button == 1 then
os.shutdown()
else
configMainMenu()
end
end
end --# added another end to close the if event == "mouse_click" if statement
end

Like suggested in the other thread, you should use a variable to keep track of when the menu is visible / not visible.

I do believe that is what the cf variable is doing. I may be wrong though.

edit: and the configM variable (although it is not being used)
Edited on 03 January 2016 - 11:47 PM
Mr_Programmer #10
Posted 04 January 2016 - 02:19 PM
Like suggested in the other thread, you should use a variable to keep track of when the menu is visible / not visible.

I do believe that is what the cf variable is doing. I may be wrong though.

edit: and the configM variable (although it is not being used)

Yes that is what they are doing cf == 0 is the main menu then cf == 1 is the dropdown menu for reboot, shutting down and user account settings…

configM is breaking the while loop for the "mouse_click" event

while configM == true do
  if event = "mouse_click" then
    newFuncction()
    configM = false --#breaking the while loop
  end
end 
Its not proper code but its a example.
Edited on 04 January 2016 - 01:24 PM