16 posts
Posted 05 January 2013 - 08:31 PM
Hi there,
As lot's of people are, I'm creating an emailing (except this is more like a chat) system, and I've run into a problem that I don't know how to fix. I know quite a bit about lua and am still learning. Here's my problem
–
layout1()
local a = 4
term.setCursorPos(20,1)
print("Sending a message to"..mailsendto)
term.setCursorPos(20,2)
print("Subject: "..mailsubjectto)
for i = 1, 10 do
term.setCursorPos(20, a)
local composingmessage = read()
local (?? What can i put here to change the variable name?) = composingmessage
a = (a+1)
end
print(40)
–
because the computer dosn't automatically create a new line, every time the user gets to the end of the line, he/she hits enter. The problem is, it's not all one variable so when he/she hits enter it needs to be saved as different variables. But lua dosn't like math in variables, (I don't think) so how can i get around this problem?
Thank you so much! :D/>
8543 posts
Posted 05 January 2013 - 09:00 PM
Moved to Ask a Pro.
2088 posts
Location
South Africa
Posted 05 January 2013 - 11:39 PM
You will need to make an advanced read function that goes into the next line when you hit enter or something, learn the edit program and see how it does it?
537 posts
Location
Copenhagen, Denmark
Posted 05 January 2013 - 11:54 PM
Look in the source of the edit program. Another solution, if you don't mind NOT being able to edit lines above, would be something like this:
function readLines(maxLines)
io.write("\n")
local lines = {}
for i = 1, maxLines do
lines[i] = io.read()
if i > 1 and lines[i] == "" and lines[i - 1] == "" then -- If you made two blank lines, stop the loop
break;
end
end
return lines
end
2005 posts
Posted 06 January 2013 - 03:45 AM
The key point here is the table. Lua doesn't mind at all if you use maths to index tables.
Technically, all variables are contained in environment tables, so you could access your environment as a table and create ordinary looking variables that way. But that's a hassle.
16 posts
Posted 06 January 2013 - 11:41 AM
So mad, how would I then read those lines? Say if I were to print it, would it be:
print(lines[1])
print(lines[2]) etc etc?
2005 posts
Posted 06 January 2013 - 11:46 AM
Exactly.
16 posts
Posted 06 January 2013 - 11:51 AM
Awesome, I shall try that out now then. :D/>
180 posts
Posted 06 January 2013 - 12:01 PM
So mad, how would I then read those lines? Say if I were to print it, would it be:
print(lines[1])
print(lines[2]) etc etc?
Or since you won't know ahead of time how many lines there will be:
for i = 1,#lines do
print(lines[i])
end
16 posts
Posted 06 January 2013 - 12:24 PM
ok, I'll try that then :)/>
16 posts
Posted 06 January 2013 - 12:28 PM
Hmm, this is what I have..
–MessageComposeLoop
function readLines(maxLines)
io.write("\n")
local lines = {}
local a = 4
for i = 1, maxLines do
term.setCursorPos(20, a)
lines = io.read()
a = (a+1)
if i > 1 and lines == "" and lines == "" then – If you made two blank lines, stop the loop
break;
end
end
return lines
end
readLines(10)
–EndMessageComposeLoop
term.clear()
layout1()
term.setCursorPos(1,7)
print("Your message")
–Display the message loop:
for i = 1,#lines do – Line 744
print(lines)
end
–End Message display loop
It's coming up with 744 (highlighted) attempt to get length of nil…… is it maybe because the term.clear() is clearing the variables? Also, what does the return function do? :)/>
16 posts
Posted 06 January 2013 - 12:57 PM
Ok, fixed that problem. It didn't like printing it when it was out of the original function.
Thank you SOOO much for your help. Really appreciate it!! :D/>
2088 posts
Location
South Africa
Posted 06 January 2013 - 10:44 PM
Your readLines() function returns a table, so call it like this:
t_messageTable = readLines(10)
Now to print the message:
for i = 1, #t_messageTable do
print(t_messageTable[i])
end