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
The code for the receiver is
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()