Jump to content




Inputs with keys


  • You cannot reply to this topic
6 replies to this topic

#1 Gumball

  • Members
  • 254 posts
  • LocationFairbanks, Alaska

Posted 16 September 2014 - 03: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 by bluebird173, 16 September 2014 - 03:55 AM.


#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 16 September 2014 - 04: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


#3 Gumball

  • Members
  • 254 posts
  • LocationFairbanks, Alaska

Posted 17 September 2014 - 08:37 PM

View PostBomb Bloke, on 16 September 2014 - 04:10 AM, said:

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 "[i]" mean? Ive been wondering what those would mean for awhile and say that they were really useful

#4 blipman17

  • Members
  • 92 posts

Posted 17 September 2014 - 09: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 by blipman17, 17 September 2014 - 09:41 PM.


#5 TheOddByte

    Lazy Coder

  • Members
  • 1,607 posts
  • LocationSweden

Posted 17 September 2014 - 09:51 PM

View Postbluebird173, on 17 September 2014 - 08:37 PM, said:

Thanks but, what would the "[i]" 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: !
--]]


#6 Gumball

  • Members
  • 254 posts
  • LocationFairbanks, Alaska

Posted 18 September 2014 - 12:40 AM

View PostTheOddByte, on 17 September 2014 - 09:51 PM, said:

View Postbluebird173, on 17 September 2014 - 08:37 PM, said:

Thanks but, what would the "[i]" 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!

#7 Gumball

  • Members
  • 254 posts
  • LocationFairbanks, Alaska

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 by bluebird173, 25 November 2014 - 06:18 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users