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

Inputs with keys

Started by Gumball, 16 September 2014 - 02:51 AM
Gumball #1
Posted 16 September 2014 - 04:51 AM
(At the title I meant buttons)


Im wondering how do I make a program where if you click a button, it saves what you pressed somehow in order, so pretend the is button a and b, i press a once, b twice, then a again, then it prints out (in order) what buttons I pressed in what order.

Can someone please help me? Im having a feeling it uses vairables to track what you pressed and when using for loops, but the codes not coming together i my head, could someone please help me? Thanks!

-bluebird173
Edited on 16 September 2014 - 02:55 AM
Bomb Bloke #2
Posted 16 September 2014 - 05:10 AM
Say you want four button presses, as in your example. You'd use something like:

local history = {}

for i=1,4 do
  history[i] = getButtonPressResult()
end

for i=1,table.maxn(history) do
  print(history[i])
end
Gumball #3
Posted 17 September 2014 - 09:37 PM
Say you want four button presses, as in your example. You'd use something like:

local history = {}

for i=1,4 do
  history[i] = getButtonPressResult()
end

for i=1,table.maxn(history) do
  print(history[i])
end

Thanks but, what would the "" mean? Ive been wondering what those would mean for awhile and say that they were really useful
blipman17 #4
Posted 17 September 2014 - 10:41 PM
when people have loops like

local i = 0
while i < 5 do
i = i + 1
print("potatoes")
end

they use the i from info as a variable that is just used for one moment to stop the loops.

everyone coud do also

local counter = 0
while counter < 5 do
counter = counter + 1
print("potatoes")
end

since the name is not important.
The name i is not important.
It is only important that there is a variable that stops loops like "while do", "for do" and "repeat until" loops.
the name often used for such a variable is i.
Edited on 17 September 2014 - 08:41 PM
TheOddByte #5
Posted 17 September 2014 - 10:51 PM
Thanks but, what would the "" mean? Ive been wondering what those would mean for awhile and say that they were really useful
I'll give you a short explanation about it, it's basically accessing an index from a table, 'i' is in this case a variable that's commonly used in loops ( i = index, k = key, v = value )
But as mentioned above it could be any name you'd want, as long as you put the same when accessing the index from the table as you used in the loop

local t = {
    "Hello";
    "World";
    "!";
}
--[[
    t[1] = "Hello"
    t[2] = "World"
    t[3] = "!"

    When it loops through the table the variable 
    'i' is going to be equal to the number 1, 2, 3
    and is accessing the content of the table in
    those indexes
--]]
for i = 1, #t do
    print( i .. ": " .. t[i] )
end
--[[
    Output:
        1: Hello
        2: World
        3: !
--]]
Gumball #6
Posted 18 September 2014 - 01:40 AM
Thanks but, what would the "" mean? Ive been wondering what those would mean for awhile and say that they were really useful
I'll give you a short explanation about it, it's basically accessing an index from a table, 'i' is in this case a variable that's commonly used in loops ( i = index, k = key, v = value )
But as mentioned above it could be any name you'd want, as long as you put the same when accessing the index from the table as you used in the loop

local t = {
	"Hello";
	"World";
	"!";
}
--[[
	t[1] = "Hello"
	t[2] = "World"
	t[3] = "!"

	When it loops through the table the variable
	'i' is going to be equal to the number 1, 2, 3
	and is accessing the content of the table in
	those indexes
--]]
for i = 1, #t do
	print( i .. ": " .. t[i] )
end
--[[
	Output:
		1: Hello
		2: World
		3: !
--]]

Ahhhh I see so in a variable/table its accessing certain parts of it. thx!
Gumball #7
Posted 25 November 2014 - 06:13 AM
Nvm, closed, I understand it now, and i'm waaaaay better at coding now.


history = {}
hash = 0

while true do  
  local event, key = os.pullEventRaw()
	if(event == "key") then
	  hash = hash + 1
	  history[hash] = keys.getName(key)
	end
end
Edited on 25 November 2014 - 05:18 AM