Posted 28 January 2013 - 05:23 AM
Hello everybody,
i know this sounds like the good and old tonumber(read()) mistake, but it isnt.
I am programming an menu for a logistic storage. In this menu you can select an item by pressing left and right key.
I marked it in the code.
The problem is that if i press the left button i get
132: attempt to compare string with number expected, got string
If i press the left button i get
129: attempt to perform arithmetic __add on string and number
It should check the variable "id" and increase and decrease the variable by one. The program thinks id is a string, but i want a number. I never said that it should be a string in my oppinion. I think it messed it up after it used the getItemName() function.
For the arrays i use this. I know that there are better ways of doing it but i am just testing things out at the moment.
I read them out of a file.
The first time i open the menu it worked. But if i use the right or left key it gives me the error.
Thank you for your help :D/>
UNOBTANIUM
i know this sounds like the good and old tonumber(read()) mistake, but it isnt.
I am programming an menu for a logistic storage. In this menu you can select an item by pressing left and right key.
I marked it in the code.
The problem is that if i press the left button i get
132: attempt to compare string with number expected, got string
If i press the left button i get
129: attempt to perform arithmetic __add on string and number
It should check the variable "id" and increase and decrease the variable by one. The program thinks id is a string, but i want a number. I never said that it should be a string in my oppinion. I think it messed it up after it used the getItemName() function.
Spoiler
version = "Storage Turtle 0.1"
running = true
select = 1
xcoord = {}
ycoord = {}
side = {}
itemname = {}
itemid = {}
id = 1
item1 = ""
a = 6
local w,h = term.getSize()
z = h-3
local function load(file)
local f = fs.open(file,"r")
local data = f.readAll()
f.close()
return textutils.unserialize(data)
end
local function getItemName()
if itemname[id] == "" then
return ("No Item" .. id)
else
return itemname[id]
end
end
local function printCentered(str, ypos)
term.setCursorPos(w/2 - #str/2, ypos)
term.write(str)
end
local function printRight(str, ypos)
term.setCursorPos(w-#str, ypos)
term.write(str)
end
function drawHeader(title, line)
printCentered(title, line)
printCentered(string.rep("-", w), line+1)
end
function drawCopyright()
printRight("by UNOBTANIUM", h)
end
function drawMenuMain()
drawCopyright()
drawHeader(version,1)
if select == 1 then
printCentered("> Get Item <", a)
else
printCentered("Get Item", a)
end
if select == 2 then
printCentered("> Sort Items <", a+1)
else
printCentered("Sort Items", a+1)
end
if select == 3 then
printCentered("> Quit <", z)
else
printCentered("Quit", z)
end
end
function drawMenuGetItem()
drawCopyright()
drawHeader(version,1)
drawHeader("GET ITEM",3)
item1 = getItemName()
if select == 1 then
printCentered("> ".. item1 .." <", a)
else
printCentered(item1, a)
end
if select == 2 then
printCentered("> Back <", z)
else
printCentered("Back", z)
end
end
local menustate = "main"
local mopt = {
["main"] = {
options = {"sget", "ssort", "quit"},
draw = drawMenuMain
},
["sget"] = {
options = {"item", "main"},
draw = drawMenuGetItem
}
}
function runMenu()
while true do
term.clear()
mopt[menustate].draw()
local id, key = os.pullEvent("key")
if key == 200 then
select = select-1
end
if key == 208 then
select = select+1
end
if select == 0 then
select = #mopt[menustate].options
end
if select > #mopt[menustate].options then
select = 1
end
if menustate == "sget" and key == 205 then -- HERE is the right key
id = id + 1
end
if menustate == "sget" and key == 203 then -- AND HERE is the left key
if id > 1 then
id = id - 1
end
end
term.clear()
term.setCursorPos(1,1)
term.clear()
if key == 28 then
if mopt[menustate].options[select] == "quit" then
rednet.broadcast("sshutdown")
running = false
break
end
if mopt[menustate].options[select] == "item" then
rednet.broadcast(id)
break
end
if mopt[menustate].options[select] == "ssort" then
rednet.broadcast("ssort")
break
end
menustate = mopt[menustate].options[select]
select = 1
end
end
end
xcoord = load("xcoord.txt")
ycoord = load("ycoord.txt")
side = load("side.txt")
itemname = load("itemname.txt")
itemid = load("itemid.txt")
rednet.open("back")
rednet.open("top")
while running do
runMenu()
end
For the arrays i use this. I know that there are better ways of doing it but i am just testing things out at the moment.
I read them out of a file.
xcoord = {0,0,1,1,2,2,3,3,4,4}
ycoord = {0,0,1,1,2,2,3,3,4,4}
side = {true,false,true,false,true,false,true,false,true,false}
itemname = {"a","b","c","d","e","f","g","h","i","j"}
itemid = {1,2,3,4,5,6,7,8,9,10}
The first time i open the menu it worked. But if i use the right or left key it gives me the error.
Thank you for your help :D/>
UNOBTANIUM