I decided that to make the transition from Touch Lua on my iPod to Codea, I’d start with a very basic program that stores notes locked up with passwords. Something that can be done in Touch Lua, but cannot be achieved persistently throughout sessions. I found this much harder on Codea. I got this really weird error.
-- Storage
-- Use this function to perform your initial setup
function setup()
print("Hello World!")
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(255, 255, 255, 255)
-- This sets the line thickness
strokeWidth(5)
-- Do your drawing here
if isKeyboardShowing() == false then
showKeyboard()
end
end
local passon = true
local method = "store"
local store = ""
local pass = ""
local key = ""
function keyboard(k)
if k == RETURN then
if method == "store" then
if passon then
passon = false
else
saveLocalData(pass, store)
end
elseif method == "read" then
print(readLocalData(pass))
end
store = ""
elseif k == BACKSPACE then
if method == "store" then
if passon then
pass = store:sub(1,#store-1)
else
store = store:sub(1,#store-1)
end
elseif method == "read" then
key = key:sub(1,#key-1)
end
else
if method == "store" then
if passon then
pass = pass .. k
else
store = store .. k
end
elseif method == "read" then
key = key .. k
end
end
end
function touched()
if method == "store" then
pass = ""
store = ""
method = "read"
elseif method == "read" then
key = ""
method = "store"
end
end
showKeyboard()
Sorry if that’s long but I figured I might want to provide it all.
The error is small;
error: [string “–storage…”]:
34: key cannot be empty string
@warspyking, we live to help, but if you could have one thread for all your questions, it’d make everyone very happy
@warspyking The error you’re getting is correct. On line 34, the key “pass” is an empty string. Make sure you have a value in “pass” before you try to use it as a key.
It’s because the variable are local, and you define them lower in the code. Don’t make them local, and try to put all initial variable assignments in setup.
local variables are faster to access no?
@warspyking local variables are faster. @SkyTheCoder The local variables are defined outside of a function, so they’re local to the code, not a specific function.
EDIT: As @SkyTheCoder said, the local variable should be defined before setup() if they’re to be used by all the code.
@dave1707 So why shouldn’t I use local variables?
Also, how is the string empty when I input it in?
@warspyking Actually your code works. When you start your code, enter a key and press return. Then enter the string you want to store at that key and press return.
I put my variables in setup and the output is blank…
What I’m doing is typing a pass, enter, typing something to store, enter, tapping the screen, typing the pass again.
I think I found something. I edited the touched function
function touched()
if method == "store" then
print("ReadMode")
pass = ""
store = ""
method = "read"
elseif method == "read" then
print("StoreMode")
key = ""
passon = true
method = "store"
end
end
Nothing prints.
@warspyking I found a couple of thing wrong in your code. The first thing is in the function touched. You need to use function touched(t) and add the code if t.state==BEGAN then so that your touch code if only executed once per touch, not twice. The second problem is when you’re trying to read the key. In the keyboard function your setting “key” to the value you want to read, then when you press RETURN, you do a readLocalData( pass). Change pass to key.
@dave1707 Wow thanks, I totally missed that!
@dave1707 My current code:
-- Storage
-- Use this function to perform your initial setup
function setup()
local passon = true
local method = "store"
local store = ""
local pass = ""
local key = ""
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(255, 255, 255, 255)
-- This sets the line thickness
strokeWidth(5)
-- Do your drawing here
if isKeyboardShowing() == false then
showKeyboard()
end
end
function keyboard(k)
if k == RETURN then
if method == "store" then
if passon then
passon = false
else
saveLocalData(pass, store)
passon = true
end
elseif method == "read" then
print(readLocalData(key))
end
store = ""
elseif k == BACKSPACE then
if method == "store" then
if passon then
pass = store:sub(1,#store-1)
else
store = store:sub(1,#store-1)
end
elseif method == "read" then
key = key:sub(1,#key-1)
end
else
if method == "store" then
if passon then
pass = pass .. k
else
store = store .. k
end
elseif method == "read" then
key = key .. k
end
end
end
function touched(t)
if t.state == "BEGAN" then
if method == "store" then
print("ReadMode")
pass = ""
store = ""
method = "read"
elseif method == "read" then
print("StoreMode")
key = ""
passon = true
method = "store"
end
end
end
showKeyboard()
But nothing prints when I tap? I get no output whatsoever.
@warspyking Change
if t.state == "BEGAN" then
to
if t.state == BEGAN then
Remove the " from around BEGAN.
@dave1707 Okay, done. But still nothing. Does screen take touch input if keyboard is showing?
@warspyking I forgot to tell you to remove the “local” from all of your variables in setup(). By putting “local” on those variables, they can only be used in setup().
@warspyking Touch input works even if the keyboard is showing as long as you touch the screen area and not the keyboard area. The purpose of “local” is to make a variable local to a function or a project. By putting local on the variables in setup(), they can only be used in setup(). If you want local variables to be use by all the functions in the project, then declare the variables local before the setup() function. Then those variables will be local and useable by the whole project but not another project that uses this as a dependency.