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

os.queueEvent not working

Started by houseofkraft, 16 January 2017 - 08:17 PM
houseofkraft #1
Posted 16 January 2017 - 09:17 PM
Hello!

I was making an ACPI program for ComputerCraft and I tried to test it out by running it and pressing CTRL+S (not holding it) and it's supposed to queue "ACPI-SH" but it didn't! The PK variable did reset like normal though.

Code
function watch()
   _G.pk = {}
   local pk = _G.pk
   while true do
	 local event, key = os.pullEvent("key")
	 if key == 29 or key == keys.r or key == keys.s then
	   table.insert(pk, key)
	   _G.pk = pk -- Update the global variable
	 end
	 -- Turn the table into a string
	 local output = ""
	 for k,v in pairs(pk) do
	   output = output .. v .. "/"
	 end
	 local r = 29 .. "/" .. 19 .. "/"
	 local s = 29 .. "/" .. 31 .. "/"
	 if output == r then
	   -- CTRL+R has been pressed!
	   os.queueEvent("ACPI-RB")
	   -- Reset PK
	   local pk = {}
	   _G.pk = pk
	 elseif output == s then
	   -- CTRL+S has been pressed!
	   os.queueEvent("ACPI-SH")
	   -- Reset PK
	   local pk = {}
	   _G.pk = pk
	 end
   end
end
function cont()
   term.clear()
   term.setCursorPos(1,1)
   shell.run("shell")
end
parallel.waitForAll(watch, cont)

Pastebin

Thanks!
valithor #2
Posted 16 January 2017 - 10:33 PM
How exactly are you checking if your event is being queued? And how are you checking if the pk table is being reset?

Do keep in mind when you do things like:


_G.pk = {}
local pk = _G.pk

You do not have 2 tables, you instead have 2 variables that point to the same table. Any changes made using one variable to the table happens to the other variable's table simply because they are the same table.

This makes things like the following redundant (You are setting _G.pk to itself since pk and _G.pk both point to the same table):


table.insert(pk,key)
_G.pk = pk

These things really wouldn't keep the programming from working, it is just useful things to know.

As best I can tell by reading through the code, the pk table is never actually reset. Whenever you try to reset the table you create a new local pk table (this is different from the local pk table at the top of your code), and set _G.pk to it. However, whenever you receive a new event you set _G.pk equal back to the local pk defined at the top of the code causing it to have all of the previous entries.
Edited on 16 January 2017 - 09:38 PM
houseofkraft #3
Posted 16 January 2017 - 10:39 PM
I'm checking how it's queued by making it so when the user presses a key it adds //KEYNUMBER// to the table. When it detects that it is //CTRL//R OR S// then it will queue the event and reset the table.
valithor #4
Posted 16 January 2017 - 10:41 PM
I'm checking how it's queued by making it so when the user presses a key it adds //KEYNUMBER// to the table. When it detects that it is //CTRL//R OR S// then it will queue the event and reset the table.

Seems we have a failure to communicate :)/>

What i meant is how are you making sure that os.queueEvent("ACPI-SH") is not actually queueing the event? Where do you check if the event is or is not queued, and how.