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

Need help with code

Started by mougli123, 04 July 2012 - 04:56 AM
mougli123 #1
Posted 04 July 2012 - 06:56 AM
I have two codes for two computers that send messages between each other. The sender sends a number that the receiver interprets as an amount and checks that amount against the stored amount and replies good or bad depending on whether the given amount is less than the stored and the one who sent the original message receives this and sends redstone pulses to extract the amount of items from a chest. The problem is that when the computer checks the value it often gives me a wrong response and sends the wrong message back. Other random times the computer would print attempt to compare number with string expected, got number.

The code for the sender is
Spoiler

function main()
term.clear()
term.setCursorPos(1 ,1)
id = os.getComputerID()
time = os.time()
rednet.open("bottom")
print("Gravity Corporation UU-Matter Retriever")
print("------------------------------------------")
print(textutils.formatTime( time, false ))
print("ID: " ..id)
print(" ")
print("How much UU-Matter would you like?")
write("> ")
matter = io.read()
message()
end
function message()
textutils.slowPrint("Querying inventory server...")
rednet.send(14, textutils.serialize(matter))
id, msg = rednet.receive(3)
if msg == "good" then
  print("Retrieving ", matter, " UU-Matter")
  for i = 1, matter do
   rs.setBundledOutput("back", colors.white)
   sleep(.2)
   rs.setBundledOutput("back", 0)
   sleep(.3)
  end
  main()
elseif msg == "bad" then
  print("Not enough UU-Matter")
  sleep(2)
  main()
else
  print("NO MESSAGE RECEIVED")
  sleep(2)
  main()
end
end
main()

The code for the receiver is
Spoiler

term.clear()
term.setCursorPos(1 ,1)
id = os.getComputerID()
rednet.open("left")
print("Gravity Corporation UU-Matter Inventory")
print("DO NOT TOUCH")
print("---------------------------------------")
print("ID: " ..id)
function count()
while true do
  event, id, msg = os.pullEvent()
  if colors.test(rs.getBundledInput("right"), colors.white) == true then
   matter = matter + 1
   print("UU-Matter +1")
   save()
  end
  if event == "rednet_message" then
   if id == nil then
	print("failed")
   elseif id == 13 then
	print("good")
	query()
   end
  end
end
end
function query()
thing = textutils.unserialize(msg)
if thing < matter then
print("pass")
  rednet.send(13, "good")
  matter = matter - thing
  saveState()
else
  rednet.send(13, "bad")
  count()
end
end
function saveState()
file = fs.open("amounts", "w")
file.writeLine((matter))
print("Saved")
file.close()
loadState()
end
function loadState()
file = fs.open("amounts", "r")
matter = file.readLine()
print("Loaded")
file.close()
count()
end
function save()
file = fs.open("amounts", "w")
file.writeLine((matter))
file.close()
count()
end
loadState()
Luanub #2
Posted 04 July 2012 - 09:44 AM
The code itself looks good. You're probably getting incorrect counts back for a couple of reason.

The first being you're not using local vars which means the data in memory is not getting dumped when the script exits or the function completes. So even though you think you are starting the counting at 0 it's very possible that its actually starting at a higher number.

The second being you don't really declare the vars and set an initial value to them, this will further help to ensure you are starting your counting at the right number.

Add this to the top of the code and see if it helps, you may want to place it inside one of the functions without the local in front of it as well. That way it resets to 0 each time the function runs if the program is something that you leave running.

local matter = 0

Another thing I would use rs.testBundledOut() vs colors.test() it seems to be less error prone.

Change this

  if colors.test(rs.getBundledInput("right"), colors.white) == true then

to

if rs.testBundledInput("right", colors.white) then
mougli123 #3
Posted 04 July 2012 - 05:16 PM
The counter is supposed to start counting at any number because it stores the current value and loads it up again from a file. It looks like typing a 0 before the number to request fixes every problem so I added this to add the 0 to the number

tostring(num)
num = "0".. num
print(num)
tonumber(num)
Pinkishu #4
Posted 04 July 2012 - 09:58 PM
Uhm you load matter from file as a string but never tonumber it? So yeah tonunbering would probably fix that maybe even withou tadding that 0 xD

You should compare numbers arithmetically there, not strings
fuzzl3 #5
Posted 08 September 2012 - 03:01 PM
I'm having a similar issue with my code while using the input values (numbers) in other variables so they don't interfere with each other.

Inputs:

print("Input length of area to be quarried:  ")
local input1 = read()
print("Input width of area to be quarried:  ")
local input2 = read()
print("Input current height:  ")
local input3 = read()
print("Input desired stopping level:  ")
local input4 = read()
local length = input1
local width = input2
local height = input3-1
local bottom = input4

and there are more variables that use those values, but the current problem set of code seems to be


width2 = width
  while width2>0 do

If this isn't enough i'll have the whole program at the bottom, but hopefully if this issue can be resolved I can apply the solution throughout the code. Also, would defining a local variable in a function mean that if the same variable is named in other functions they won't conflict or is it still better to just define a new variable?

Spoiler

term.clear()
term.setCursorPos(1,1)
print("Input length of area to be quarried:  ")
local input1 = read()
print("Input width of area to be quarried:  ")
local input2 = read()
print("Input current height:  ")
local input3 = read()
print("Input desired stopping level:  ")
local input4 = read()
local length = input1
local width = input2
local height = input3-1
local bottom = input4
tonumber("width")
sleep(1)
term.clear()
term.setCursorPos(1,1)
print("Length will be "..length..", width will be "..width..", height is "..height..", and digging will stop at level "..bottom..".")
print("Is this correct?")
input5 = read()

local h = input4
local x = 0
local z = 0
local a = 0
local b = 0
local i = 0
local g = height
local number = input2
local level = height-bottom
local y = height
local odd = "false"
local even = "false"
local odd2 = "false"
local even2 = "false"
local aboveBottom = bottom+1
local difference = g-h
function checkHeight(level)
  if level%2 == 0 then
	even2 = "true"
  else
	odd2 = "true"
  end
end
function check(number)
  number = input2
  if number%2 == 0 then
	even = "true"
  else
	odd = "true"
  end
end
function clearLayerOddAlpha()
   width2 = width
  while width2>0 do
	if width2 == 1 then
	  clearRowRedAlpha()
	  nextLayerOddAlpha()
	else
	  clearRowRedAlpha()
	  nextRowLeft()
	  clearRowBlueAlpha()
	  nextRowRight()
	end
  end
end
function clearLayerOddBeta()

  while width2>0 do
	if width2 == 1 then
	  clearRowRedBeta()
	  nextLayerOddBeta()
	else
	  clearRowRedBeta()
	  nextRowRight()
	  clearRowBlueBeta()
	  nextRowLeft()
	end
  end
end
function clearRowRedAlpha()
a = length
  while a>0 do
	turtle.dig()
	turtle.forward()
	x = x+1
	a = a-1
  end
width2 = width2-1
z = z+1
end
function clearRowRedBeta()
a = width
  while a>0 do
	turtle.dig()
	turtle.forward()
	z = z-1
	a = a-1
  end
width2 = width2-1
x = x-1
end
function clearRowBlueAlpha()
a = length
  while a>0 do
	turtle.dig()
	turtle.forward()
	a = a-1
	x = x-1
  end
width2 = width2-1
z = z+1
end
function clearRowBlueBeta()
a = width
  while a>0 do
	turtle.dig()
	turtle.forward()
	a = a-1
	z = z+1
  end
width2 = width2-1
x = x-1
end
function nextRowLeft()
  turtle.turnLeft()
  turtle.dig()
  turtle.forward()
  turtle.turnLeft()
end
function nextRowRight()
  turtle.turnRight()
  turtle.dig()
  turtle.forward()
  turtle.turnRight()
end
function nextLayerOddAlpha()
  turtle.digDown()
  turtle.down()
  turtle.turnRight()
  difference = difference-1
end
function nextLayerOddBeta()
  turtle.select(16)
  if turtle.getItemCount() then
	y = height
	while y<height do
	  turtle.up()
	  y = y+1
	end
	turtle.turnRight()
	dumpInventory()
	while y>g do
	  turtle.down()
	  y = y-1
	end
	turtle.digDown()
	turtle.down()
	g = g-1
  else
	turtle.digDown()
	turtle.down()
	turtle.turnLeft()
	g = g-1
  end
end
function dumpInventory()
  for i=1,16,1 do
	turtle.select(i)
	turtle.drop(64)
  end
  shell.run("turn","left",2)
end

function commenceDigging()
  check(number)
  checkHeight(level)
  if odd == "true" then
	if odd2 == "true" then
	  while difference>=0 do
		if difference==0 then
		  b = width
		  while b>0 do
			if b == 1 then
			  clearRowRedAlpha()
			else
			  clearRowRedAlpha()
			  nextRowLeft()
			  clearRowBlueAlpha()
			  nextRowRight()
			end
		  end
		else
		  clearLayerOddAlpha()
		  clearLayerOddBeta()
		end
	  end
	  turtle.turnRight()
	  while z>0 do
		turtle.forward()
		z = z-1
	  end
	  turtle.turnRight()
	  while x>0 do
		turtle.forward()
		x = x-1
	  end
	  while g<height do
		turtle.up()
		g = g+1
	  end
	  dumpInventory()
	end
  end
end
if input5 == "yes" then
  term.clear()
  term.setCursorPos(1,1)
  print("Engaging quarry process")
  sleep(2)
  commenceDigging()
else
  print("Session terminated")
  sleep(1)
  term.shutdown()
end