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

Saving a Password with Buttons

Started by mist3r_m0nk3y, 20 July 2014 - 11:36 AM
mist3r_m0nk3y #1
Posted 20 July 2014 - 01:36 PM
Hi everyone

I'm relatively new to Computercraft, but I already understand the basics of this mod and of lua. I'm playing on version 1.57 of Computercraft on the Direwolf20 Pack.

I'm currently working on a password program with a numpad on a monitor. I'm using Direwolf20s button API (pastebin: HRbMF1Eg).

I created a numpad from 0-9 with buttons and want to get a 4-Digit password out of it. I also have a "Reset" and a "Enter" button. Right now I'm doing it very complicated with a lot of code:


function button1()
if step == 1 then
  click1 = 1
elseif step == 2 then
  click2 = 1
elseif step == 3 then
  click 3 = 1
elseif step == 4 then
  click4 = 1
end
step = step + 1
end

I need to do this for every button, so 10 times. Is there an easier way to do this?

This is the pastebin for my not yet finished program: t8jc4KTx
If you want to look at it, you need to use it on a 2x3 advanced monitor on TOP of the computer. Also download the button API and name it button

Thanks a lot :)/>

mist3r_m0nk3y
hilburn #2
Posted 20 July 2014 - 04:17 PM
You could make a table that contains the password and then a function that appends the latest clicked button to the table. for example:


local pwd = {} --#declares an empty table at the start of the program

local function numberClick(button_label) --# 1,2,3 etc, set the on-click function to be numberClick(1) for 1 etc
	 pwd[#pwd + 1] = button_label --#pwd is the current length of the pwn table, so +1 to append to the end
end

local function reset()
	pwd={} --#clears the password table
end

local function submit()
	if pwd==storedpwd then
		--#valid password
	else
		--#invalid password
	end
end

Alternatively, you could store it as a string and do:


pwd=""

local function numberClick(button_label)
	 pwd=pwd..button_label --# the .. concatenates 2 strings
end

Or if you really want to keep it as a number for some reason (Lua will treat a string composed only of numbers as a number anyway)


pwd=0

local function numberClick(button_label)
	 pwd=pwd*10 + button_label --# eg adding 4 to 13 -> 13*10 + 4 -> 130+4 = 134
end

Hope this helps!
Edited on 20 July 2014 - 02:21 PM