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

tables and pullevents

Started by Enderslime, 20 November 2015 - 07:50 PM
Enderslime #1
Posted 20 November 2015 - 08:50 PM
local keys = {}
Keys["keys.1"] = "1"
Keys["keys.2"] = "2"
Keys["keys.3"] = "3"
Keys["keys.4"] = "4"
Keys["keys.5"] = "5"
Keys["keys.6"] = "6"
Keys["keys.7"] = "7"
Keys["keys.8"] = "8"
Keys["keys.9"] = "9"

function draw()

paintutils.drawFilledBox(0,0,100,100,colors.blue)
term.setCursorPos(3,3)
print("Turtle Controller V1")
term.setCursorPos(3,5)
print("Add ID : "..ID)

end


ID = ""

draw()
term.setCursorPos(10,5)

while true do
local event, key = os.pullEvent("key")


end


this is my current code and i want to test when you press a number key and add it to the var id, i thought of tables but couldn't remember how to implement that, could someone help me? is there a better way?
LeDark Lua #2
Posted 20 November 2015 - 08:56 PM

local keys={
["1"]=true;
["2"]=true;
["3"]=true;
["4"]=true;
["5"]=true;
["6"]=true;
["7"]=true;
["8"]=true;
["9"]=true;
["0"]=true;
}

while true do
   event={os.pullEvent()}
   if event[1]=="key" then
      if keys[event[2]] then
         --do something if the number keys are pressed
      end
   end
end
I think its this what are you looking for!
Lupus590 #3
Posted 20 November 2015 - 09:17 PM
you are initialising you table in a very bad way. here is an example of making tables.


example = {1,2,3,4,5}
example[1] --# == 1

example = {}
example[1] --# == nil
example[1] = 1
example[2] = 2
example[1] --# == 1

I would go with the first method as it is easier for the computer to 'read'
Enderslime #4
Posted 20 November 2015 - 09:27 PM
you are initialising you table in a very bad way. here is an example of making tables.


example = {1,2,3,4,5}
example[1] --# == 1

example = {}
example[1] --# == nil
example[1] = 1
example[2] = 2
example[1] --# == 1

I would go with the first method as it is easier for the computer to 'read'

Why is it bad?
Lupus590 #5
Posted 20 November 2015 - 09:54 PM
it's more won't work than bad. And if it does work they it's a very unintuitive way to do it.
KingofGamesYami #6
Posted 20 November 2015 - 10:16 PM
It would work, if the capitalization wasn't changed from keys to Keys.