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

I dont understand why it will not run...

Started by scarecrow569, 29 November 2012 - 08:27 PM
scarecrow569 #1
Posted 29 November 2012 - 09:27 PM
When I run it upto the part where I type yes, as soon as I hit enter I get this error
solarfactory:70: attempt to index ? (a number value)

it should be turning on the black and white bundled cables that are sitting in the back
of the computer but It must be clashing with some code above, but I'm not sure what could. code:

Also, how would I make the copper round upto a multiple of 3?

btw, it makes solar pannels.


mon = peripheral.wrap("left")
x=0
while true do
if true then
mon.clear()
  mon.setCursorPos(1,1)
  mon.write("    *** Solar Pannel Factory ***	 ")
  mon.setCursorPos(1,2)
  mon.write("-------------------------------------")
  mon.setCursorPos(1,4)
  mon.write(" Use the Computer to input how many  ")
  mon.setCursorPos(1,5)
  mon.write(" solar pannels you would like to make")
end
print("how many panels would you like to make?")
x = tonumber(io.read())
y = x
if x ~= 0 then
  break
end
os.sleep(1)
end

local glass = 3 * y
local coal = 3 * y
local riron = 4 * y
local iron = 8 * y
local redstone = 8 * y
local tin = 4 * y
local copper = 6.5 * y
local rubber = 13 * y

while true do
mon.clear()
mon.setCursorPos(1,1)
mon.write("    *** Solar Pannel Factory ***	 ")
mon.setCursorPos(1,2)
mon.write("-------------------------------------")
mon.setCursorPos(1,3)
mon.write(" You will need...				    ")
mon.setCursorPos(1,4)
mon.write(glass.." Glass				  ")
mon.setCursorPos(1,5)
mon.write(coal.." Coal				    ")
mon.setCursorPos(1,6)
mon.write(riron.." Refined Iron		   ")
mon.setCursorPos(1,7)
mon.write(iron.." Iron				    ")
mon.setCursorPos(1,8)
mon.write(redstone.." Redstone		    ")
mon.setCursorPos(1,9)
mon.write(tin.." Tin					  ")
mon.setCursorPos(1,10)
mon.write(copper.." Copper			    ")
mon.setCursorPos(1,11)
mon.write(rubber.." Rubber				  ")

print("Have you loaded the chests with the correct material? type 'yes' to continue...")
if io.read() == "yes" then
break
end
os.sleep(2)
end
 
if true then
    redstone.setBundledOutput("back", colors.black+colors.white)
end


KaoS #2
Posted 29 November 2012 - 09:31 PM
on line 28 you make the variable redstone, this overwrites the redstone API so the second to last line won't work

oh and here is how to round copper up to the nearest 3


copper-copper%3+(copper%3==0 and 0 or 3)
scarecrow569 #3
Posted 29 November 2012 - 11:48 PM
Wow, Great timely response!
I wouldn't have thought to see redstone as a variable that would mess with the API
also, I hate putting in code I don't fully understand. Could you explain this or point me to
a link that explains it? also how would this be implemented into my current code?
Doyle3694 #4
Posted 30 November 2012 - 12:51 AM
your last 3 lines are unnessesary, you should change them to just

redstone.setBundledOutput("back", colors.black+colors.white)
KaoS #5
Posted 30 November 2012 - 01:02 AM
I will be happy to explain, it is all possible thanks to the modulus operator (%)

basically a%b is the remainder of a/b if a/b is rounded down to the nearest integer. example

5%2
is 1 becuse 3%2 is 2 remainder 1

so copper-copper%3 rounds it down to the nearest multiple of 3, then (copper%3==0 and 0 or 3) checks if copper is perfectly divisible by 3 (if the remainder is 0), if it is perfectly 0 then add nothing, if it has a remainder then add 3 to round up

I fear I may not have explained that very well, feel free to ask if you do not understand anything
KaoS #6
Posted 30 November 2012 - 01:05 AM
your last 3 lines are unnessesary, you should change them to just

redstone.setBundledOutput("back", colors.black+colors.white)

that seems to be done fairly often in the code :mellow:/>
KaoS #7
Posted 30 November 2012 - 01:33 AM
Hey Scarecrow I retyped your code using a few techniques to simplify things, take a look and see if you can use these in future


term.redirect(peripheral.wrap("left"))
local y
while not y do
  term.clear()
  term.setCursorPos(1,1)
  print("	*** Solar Pannel Factory ***")
  print("-------------------------------------")
  print(" Use the Computer to input how many  ")
  print(" solar pannels you would like to make")
  print("how many panels would you like to make?")
  y = tonumber(read())
end
local glass = 3 * y
local coal = 3 * y
local riron = 4 * y
local iron = 8 * y
local redstone = 8 * y
local tin = 4 * y
local copper = 6.5 * y
local rubber = 13 * y
while true do
  term.clear()
  term.setCursorPos(1,1)
  print("	*** Solar Pannel Factory ***")
  print("-------------------------------------")
  print(" You will need...")
  print(glass.." Glass")
  print(coal.." Coal")
  print(riron.." Refined Iron")
  print(iron.." Iron")
  print(redstone.." Redstone")
  print(tin.." Tin")
  print(copper.." Copper")
  print(rubber.." Rubber")
  print("\nHave you loaded the chests with the correct material? type 'yes' to continue...")
  if read() == "yes" then
    break
  end
end
redstone.setBundledOutput("back", colors.black+colors.white)

EDIT: *sigh* it removed my indentation :(/> -> re-added it manually
Edited on 30 November 2012 - 12:47 AM
ChunLing #8
Posted 30 November 2012 - 09:09 AM
The dialog box for adding code doesn't accept tabs, but if you exit that and paste into the normal post window between the code tags then it gives you double space indents for them.
KaoS #9
Posted 30 November 2012 - 09:16 AM
if you use n++ you just add an extra tab in front and it works… I think it always does

so just ctrl+a -> tab -> ctrl+c and you're good 2 go
scarecrow569 #10
Posted 30 November 2012 - 07:28 PM
so copper-copper%3 rounds it down to the nearest multiple of 3, then (copper%3==0 and 0 or 3) checks if copper is perfectly divisible by 3 (if the remainder is 0), if it is perfectly 0 then add nothing, if it has a remainder then add 3 to round up
I found this to be brilliant. Is this common in coding or chefs special?
your last 3 lines are unnessesary, you should change them to just
Yeh sorry I was trying to find why it wasn't working.
KaoS #11
Posted 30 November 2012 - 07:35 PM
haha thanks :rolleyes:/> it's normal coding (but incredibly useful), I was ignorant to its uses for a long time though. it would pay to remember that one
ChunLing #12
Posted 02 December 2012 - 02:22 AM
I'd say that using the and or operators outside of conditional checks is "common" rather than "normal". They function the way they do because of how the lua conditional checks function (very robustly, easily digesting any value) and thus it is simpler to just pass values directly along rather than convert them. They weren't especially intended for this use, but because it fills a function that is in some other languages (the ternary operator for example).