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

Key Pad Help?

Started by whiteknight1488, 08 June 2012 - 06:40 AM
whiteknight1488 #1
Posted 08 June 2012 - 08:40 AM
I have this code running

function keyPad(void)
local mt = {
{1,2,3},
{4,5,6},
{7,8,9}
};
local mt2 = {0,0,0,0};
local xOffset = 23;
local nX = 1;
local nY = 1;
local n = 1;
while (n<=4) do
term.clear();
for i = 1,3 do
for j = 1,3 do
term.setCursorPos((xOffset + i),(j));
if (nY == j) and (nX == i) then
term.write("*");
else
term.write(mt[j]);
end

end
end
for i = 1,n-1 do
term.setCursorPos((xOffset + i),(4));
term.write("*");
end
r,s = os.pullEvent();

if(r == "key") then
if (s == 205) then
if (nX < 3) then
nX = nX +1;
end
end
if (s == 208) then
if (nY < 3) then
nY = nY +1;
end
end
if (s == 200) then
if (nY > 1) then
nY = nY - 1;
end
end
if (s == 203) then
if (nX > 1) then
nX = nX -1;
end
end
if (s == 28) then
mt2[n] = mt[nY][nX];
n = n+1;
end
end
end
term.clear();
term.setCursorPos((1),(1));
return mt2;
end
name
function: keyPad(hello);


….but when i try to run from the computer i get a string "startup":66: '<name>' expected

Any help? Thanks
Luanub #2
Posted 08 June 2012 - 12:13 PM
It looks like your fairly new to Lua, I see several errors I will high light some below but I highly recommend you read some of the tutorials.

First off you dont need to put semi colons at the end of a line in lua remove them. Your last function also has a couple of issues

it should be

function keyPad(hello)

And it needs some content so…

function keyPad(hello)
--inset keyPad coding here
end -- every function needs an end to it

Plus you already defined it at the top of the code, you can only use a function name once in a script.

Also it looks like you never call the function. You need to add keyPad() somewhere to call it, you will also need to insert the data that you are sending for the argument hello in your function so keyPad("hello") or something along those lines.

Here's an example of how it should work.. Try running it and see how it works.


function test( arg1 ) -- declare the function and var name for the first and only argument for the function
if arg1 == "Hello" then
   print ("hello")
elseif arg1 == "Goodbye" then
   print ("goodbye")
end
end

write ("Enter Hello or Goodbye: ")
local input = read() -- capture user input into the var input
test(input) -- run the function test using the var input as the data for arg1