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

[LUA][Error] main.lua:92: attempt to call string

Started by iceDave, 10 December 2012 - 08:50 PM
iceDave #1
Posted 10 December 2012 - 09:50 PM
Hi there, i wrote a program that runs in a loop.
If i run it the first time, everything is working fine. It communicate with the second computer, turns the redpower on / off for the side what i choose.. like i sad, runs fine.
If i run it the second time i get this error on line 92… attemt to call string

main.lua (computer id 7)
Spoiler


-- This is the main script for my FTB modpack Main computer
-- It will handle my redpower what is connected to all my engines, lights, computers e.g

 os.loadAPI("myAPI")
 --myAPI.foo("this is a test")

function powerswitch(engine, power)
term.clear()
term.setCursorPos(1,1)

if power == "1" then power = "true" powerstate = "ON" end
if power == "2" then power = "false" powerstate = "OFF" end
engine_no = engine

-- first send engine number
rednet.send(8, engine_no)
-- receive ok from engine
id, engine_ok = rednet.receive()
-- second send power state
rednet.send(8, power)
-- receive ok from engine
id, power_ok = rednet.receive()
-- send go ahead to engine
rednet.send(8, "switch")

print(engine_ok)
print(power_ok)

-- receive job done from engine
id, job_state = rednet.receive()
-- print engine and powerstate is switched
write("The power from engine number ")write(engine_no)write(" is turned ")write(powerstate)

sleep(2)
main()
end

function engine_no(engine)
term.clear()
term.setCursorPos(1,1)
print("***************************************************") -- print 51 * for the top frame of the main screen
write("*              ")term.setTextColor(colors.white)write("Power Engine ") write(engine)term.setTextColor(colors.blue)write("                     *") -- print main screen...
print("***************************************************") -- print main screen...
write("* 1 - ")term.setTextColor(colors.white)write("Turn On")term.setTextColor(colors.blue)write("                                     *")
write("* 2 - ")term.setTextColor(colors.white)write("Turn Off")term.setTextColor(colors.blue)write("                                    *")
print("* 3 -                                             *")
print("* 4 -                                             *")
print("* 5 -                                             *")
print("* 6 -                                             *")
print("* 7 -                                             *")
print("* 8 -                                             *")
print("* 9 -                                             *")
print("*                                                 *")
write("* 0 - ")term.setTextColor(colors.white)write("Main Screen")term.setTextColor(colors.blue)write("                                 *")
print("*                                                 *")
print("***************************************************")
write("*              ")term.setTextColor(colors.white)write("Please select a number")term.setTextColor(colors.blue)write("             *")
print("***************************************************")
power = io.read()
if power == "1" or power == "2" then
powerswitch(engine, power)
end
if power == "0" then
main()
end
end
function generator()
term.clear()
term.setCursorPos(1,1)
print("***************************************************")
write("*              ")term.setTextColor(colors.white)write("Power generator room")term.setTextColor(colors.blue)write("               *")
print("***************************************************")
write("* 1 - ")term.setTextColor(colors.white)write("Engine number 1")term.setTextColor(colors.blue)write("                             *")
write("* 2 - ")term.setTextColor(colors.white)write("Engine number 2")term.setTextColor(colors.blue)write("                             *")
write("* 3 - ")term.setTextColor(colors.white)write("Engine number 3")term.setTextColor(colors.blue)write("                             *")
write("* 4 - ")term.setTextColor(colors.white)write("Engine number 4")term.setTextColor(colors.blue)write("                             *")
write("* 5 - ")term.setTextColor(colors.white)write("Engine number 5")term.setTextColor(colors.blue)write("                             *")
print("* 6 -                                             *")
print("* 7 -                                             *")
print("* 8 -                                             *")
print("* 9 -                                             *")
print("*                                                 *")
write("* 0 - ")term.setTextColor(colors.white)write("Main Screen")term.setTextColor(colors.blue)write("                                 *")
print("*                                                 *")
print("***************************************************")
write("*              ")term.setTextColor(colors.white)write("Please select a number")term.setTextColor(colors.blue)write("             *")
print("***************************************************")
engine = io.read()
if engine == "1" or engine == "2" then
engine_no(tostring(engine))
end
end

function main()
term.clear()
term.setCursorPos(1,1)
term.setTextColor(colors.lightBlue)
print("***************************************************")
write("*                      ")term.setTextColor(colors.white)write("Main")term.setTextColor(colors.lightBlue)write("                       *")
print("***************************************************")
write("* 1 - ")term.setTextColor(colors.blue)write("Power generator room")term.setTextColor(colors.lightBlue)write("                        *")
write("* 2 - ")term.setTextColor(colors.gray)write("Refinery pumps")term.setTextColor(colors.lightBlue)write("                              *")
write("* 3 - ")term.setTextColor(colors.gray)write("Fuel pumps")term.setTextColor(colors.lightBlue)write("                                  *")
write("* 4 - ")term.setTextColor(colors.gray)write("Lights")term.setTextColor(colors.lightBlue)write("                                      *")
write("* 5 - ")term.setTextColor(colors.gray)write("Farming")term.setTextColor(colors.lightBlue)write("                                     *")
print("* 6 -                                             *")
print("* 7 -                                             *")
print("* 8 -                                             *")
print("* 9 -                                             *")
print("*                                                 *")
print("*                                                 *")
print("*                                                 *")
print("***************************************************")
write("*              ")term.setTextColor(colors.white)write("Please select a number")term.setTextColor(colors.lightBlue)write("             *")
print("***************************************************")
selection = io.read()
if selection == "1" then
generator()
end
end


rednet.open("right") -- opens the connection to the modem attached to the right side of the computer
main()


switch.lua (computer id 8)
Spoiler


rednet.open("top")
while true do
term.clear()
term.setCursorPos(1,1)
-- first get engine number
id, engine_no = rednet.receive()
-- send ok to main
rednet.send(7, "ok")
-- second get power state
id, power = rednet.receive()
-- send ok to main
rednet.send(7, "ok")
-- receive switch from main
id, switch_ok = rednet.receive()

if engine_no == "1" then engine_output = "left" end
if engine_no == "2" then engine_output = "back" end
if engine_no == "3" then engine_output = "right" end

print(engine_no)
print(engine_output)
print(power)

-- set engine number to power stet
-- redstone.setOutput(engine_output, power)
if power == "true" then
redstone.setOutput(engine_output, true)
end
if power == "false" then
redstone.setOutput(engine_output, false)
end
-- send job done to main
rednet.send(7, "Job done")
end

would be really great if someone could help me, i really can't figure out why this happen and how i could fix it.
faubiguy #2
Posted 11 December 2012 - 03:58 AM
When you do
engine_no = engine
it overwrites the function engine_no with the string engine, so next time you try to call engine_no it calls the string instead, and crashes.
iceDave #3
Posted 11 December 2012 - 10:21 AM
Thanks mate, it fixed my program.. its to easy to mess up a program :S