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

(sorted) new problem - Keys event

Started by Dustmuz, 29 May 2015 - 07:53 AM
Dustmuz #1
Posted 29 May 2015 - 09:53 AM
^^^^^^^^^^ This part have been solved ^^^^^^^^^^^^
Hello there Great People of Computercraft :D/>

im trying to make a program that finds peripherals.
i know i've seen a lot of them around, but i cant find any now :(/>

the part i need to "steal" is the part of the code that finds the peripherals..

what im trying to do with the code, is make a program that finds the peripherals, makes a list (onscreen) and gives the user the option to "enable" the controls/monitoring for that particular peripheral

and is there a page somewhere that lists all the keys on the keyboard that os.pullEvent("key") can use??

best regards
this blind Dusty
^^^^^^^^^^ This part have been solved ^^^^^^^^^^^^

now i have a new question about the Keys event

i made this

line 80 - local event, key = os.pullEvent("key")
line 81 -if key == key.space then
line 82 -  local mon = peripheral.wrap(tostring(monside))
line 83 -  local bank = peripheral.wrap(tostring(bankside))
line 84 -  local rea = peripheral.wrap(tostring(reactorside))
line 85 -end


but the line if key == key.space then gives me an error

startup:81: attempt to index ? (a number value)

ehm.. what have i done wrong :(/>
Edited on 29 May 2015 - 02:13 PM
valithor #2
Posted 29 May 2015 - 10:03 AM
It would be worth looking into a few peripheral api functions such as:

peripheral.getNames()
this will return the wrappable names of all the peripherals available to the computer. If the peripheral is touching the computer it will return top bottom left right front back if it is on a network it will return something like monitor_1 for that peripheral. This function returns a table, which is easy to iterate through.

peripheral.find()
This will return a wrapped table of all the peripherals with that given name, for example if you search for "monitor" it will return a table with each entry being one of the monitors it found wrapped.

peripheral.getType()
In the case of it returning the side instead of the name for the getNames function you could use this to get what kind of peripheral is on a given side.

keys: http://www.computerc.../wiki/Keys_(API)
note: the way linking works on this site doesn't like a few of the wiki links. You will have to manually add the ) to the url for that link to work, or you could copy and paste it directly instead of clicking it.

edit:

Another common thing that is often done is only checking the 6 sides of the computer for a peripheral.

for _,side in pairs(rs.getSides()) do
  if peripheral.getType(side) == someperipheral then
	wrap(someperipheral) -- this is more or less pseudo code
  end
end
Edited on 29 May 2015 - 08:09 AM
Dustmuz #3
Posted 29 May 2015 - 10:23 AM
heyy thaaanks a lot :D/>
just what i needed :)/>

and thanks for the "code" aswell..
Bomb Bloke #4
Posted 29 May 2015 - 10:45 AM
keys: http://www.computerc.../wiki/Keys_(API)
note: the way linking works on this site doesn't like a few of the wiki links. You will have to manually add the ) to the url for that link to work, or you could copy and paste it directly instead of clicking it.

Or you could just format the link tags properly yourself. 'tis not hard.

http://www.computercraft.info/wiki/Keys_(API)
Dustmuz #5
Posted 29 May 2015 - 11:05 AM
update on the first post. problem described there
Bomb Bloke #6
Posted 29 May 2015 - 11:16 AM
You're attempting to index into a table, "key", which doesn't exist - it's thus nil, hence you get an "attempt to index nil" error.

(Edit: Actually, scratch that, it seems you've defined it as a number somewhere, so instead the error says… well, I'm sure you get the idea.)

Try it with "keys.space".
Edited on 29 May 2015 - 09:17 AM
Dustmuz #7
Posted 29 May 2015 - 11:18 AM
thanks Bomb :)/>

that worked :)/>
flaghacker #8
Posted 29 May 2015 - 03:52 PM
You can replace the { } around code and /code with [ ] f or some actual code tags:

--code here

Just a little tip, you were actually quite close yourself ;)/>