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

Strange Problem

Started by civilwargeeky, 14 October 2012 - 02:20 AM
civilwargeeky #1
Posted 14 October 2012 - 04:20 AM
So I really don't know why this is happening. All of my code works fine, except for one part that should work anyway. (My problem is toward the end)
edit: Made my description of problem clearer
 --Just a simple function
function clearall()
term.clear()
term.setCursorPos(1,1)
end

test = 0
diskside = ""

--Just checks to see if there is a disk
check = fs.exists("disk")
if check ~= true then
test = 1
end

--The part that actually  moves the programs
while test ~= 1 do
clearall()
print("What operation?")
print("download/upload")
local choice = io.read()
if choice == "download" then
test = 1
end
if choice == "upload" then
test = 1
end
clearall()

if choice == "download" then
local ondisk = fs.list("disk")
for i=1, #ondisk do
local current = fs.combine("disk",ondisk[i])
fs.delete(ondisk[i])
fs.copy(current,ondisk[i])
end
else

if choice == "upload" then
local oncomp = fs.list("")
for i=1, #oncomp do
local current = fs.combine("disk",oncomp[i])
if (oncomp[i] ~= "rom") and
   (oncomp[i] ~= "disk") then
fs.delete(current)
fs.copy(oncomp[i],current)
end
end
end

end

end

if check == true then
textutils.slowPrint("Working...")
sleep(1)
print("")
print("Done")

--The part I'm having trouble with: with this code, it is not ejecting the disk
--Note: the table "side" is defined by a program that runs at startup and gives all the different sides as strings.
-- Also, when this runs, choice is supposed to either equal "upload" or "download" or else it wouldn't have broken the while
--Now, the strange thing is, if I take out the if choice == "upload" then, and the end that follows
--then it does what its supposed to do, but like this, it dosen't do anything
if choice == "upload" then
for i=#side, 1, -1 do
diskcheck = disk.isPresent(side[i])
if diskcheck == true then
diskside = side[i]
end
end
disk.eject(diskside)
end

else
print("No disk")
end
remiX #2
Posted 14 October 2012 - 04:30 AM
Where do you define 'choice'. What does the error say?
civilwargeeky #3
Posted 14 October 2012 - 04:33 AM
Its not an error, it just seems like it skips the code.
choice is defined towards the top.

local choice = io.read()
civilwargeeky #4
Posted 14 October 2012 - 04:52 AM
Well actually nevermind.

I just tried taking out the local from "local choice" and now it works.
What exactly does local mean anyway?
remiX #5
Posted 14 October 2012 - 04:58 AM
Its not an error, it just seems like it skips the code.
choice is defined towards the top.

local choice = io.read()

Oh right, didn't see that lol. I can't read codes that aren't indented :)/>/>
Fatal_Exception #6
Posted 14 October 2012 - 07:46 AM
http://lua-users.org/wiki/ScopeTutorial
brett122798 #7
Posted 14 October 2012 - 08:23 AM
Change this:


local choice = io.read()

To:


local choice = read()

I'm pretty sure that Lua in CC won't take io.read().



What exactly does local mean anyway?

I think it means that the variable will only be there within the length the program is open. A global variable stays around unless you reset the computer. That's at least what I heard.
PixelToast #8
Posted 14 October 2012 - 08:26 AM
you sould also combine if and else statements
elseif <expression> then
remiX #9
Posted 14 October 2012 - 11:32 AM
Change this:


local choice = io.read()

To:


local choice = read()

I'm pretty sure that Lua in CC won't take io.read().

It does
PixelToast #10
Posted 14 October 2012 - 06:25 PM
Change this:


local choice = io.read()

To:


local choice = read()

I'm pretty sure that Lua in CC won't take io.read().

It does
really? last time i checked io.read wasnt compatible with term
unless the devs changed it, idk
Lyqyd #11
Posted 14 October 2012 - 07:48 PM
Change this:


local choice = io.read()

To:


local choice = read()

I'm pretty sure that Lua in CC won't take io.read().

It does
really? last time i checked io.read wasnt compatible with term
unless the devs changed it, idk

io.read() uses read(), and has for probably most of the life of the mod. It works just fine.