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

Need to stop concatenate from happening.

Started by guesswhat123212, 24 June 2014 - 12:38 AM
guesswhat123212 #1
Posted 24 June 2014 - 02:38 AM
as I was writing a new turtle program I also decided to write a helper for sending rednet messages. I use the command sendMsg(id,reply,data1,data2,data3,data4,data5,data6) (line 121) it turns all that and then a bit more into a table and then sends the table over standard rednet.

however I am hitting a snag, I am trying to put string "co" in data1 area but then its trying to concatenate and failing.(at least that's what I am thinking its doing)

I came to this conclusion after just getting error "textutils:177: attempt to concatenate string and nil" so I added print numbers to watch where it was failing. its failing after print("11") which would be line 197.

any ideas on how to stop it from concat or is it something I am not seeing?

pastebin link to full code, http://pastebin.com/NJJuvzHZ
code snippets though they do not like my formatting.
Spoiler–co check
if dataTable["wireless"] then
print("11")
sendMsg(0,true,co,0,0,0,0,0) <—- where I think the issue is
print("12")
if recTable[3] == "yes" then
dataTable["co"] = true
dataTable["coID"] = recTable("sid")
dataTable["yTravel"] = recTable[4]
updateConfig()
else
end
end

– rednet functions

function sendMsg(rid,reply,data1,data2,data3,data4,data5,data6)
sendTable = {}
sendTable[1] = rid
sendTable[2] = reply
sendTable[3] = data1
sendTable[4] = data2
sendTable[5] = data3
sendTable[6] = data4
sendTable[7] = data5
sendTable[8] = data6
sendTable["sid"] = os.getComputerID()
sendTable["name"] = os.getComputerLabel()
sendTable["status"] = status
transmitData(sendTable)
end

function transmitData(data)
if dataTable["wireless"] then
if data[1] == 0 then
rednet.broadcast(textutils.serialize(sendTable))
else
rednet.send(data[1],textutils.serialize(sendTable))
end
end
if data[2] then
getMsg()
end
end

function getMsg()
if dataTable["wireless"] then
recTable = {}
sid, msg, d = rednet.receive(3)
recTable = textutils.unserialize(msg)
end
end
AssossaGPB #2
Posted 24 June 2014 - 03:19 AM
Your problem is that you forgot the quotes around "co". Also, you are calling getMsg() before it is declared, in lua any functions you call must be declared above where it is called, I know it weird.
guesswhat123212 #3
Posted 24 June 2014 - 03:38 AM
I tried it with quotes as well which is why I removed them. However I re-added the quotes and moved getMsg() higher up and it still gives me same issue.

note: also moved transmitData(data) up as well just to make sure I was not hitting that issue. going over the rest of it now just to rule it out.
Edited on 24 June 2014 - 01:40 AM
Bomb Bloke #4
Posted 24 June 2014 - 04:22 AM
Looking inside the textutils API, the line which is erroring out is part of the textutils.unserialize() function.

You call that in a few places, but the relevant spot seems to be here in getMsg() (called by transmitData(), which is called by sendMsg(), which is called by line 197 - next time use more print statements!):

function getMsg()
        if dataTable["wireless"] then
                recTable = {}
                sid, msg, d = rednet.receive(3)
                recTable = textutils.unserialize(msg)
        end
end

You've got a three second time out on that rednet.receive() call. If it doesn't get a reply in that time, it'll give up, and sid / msg / d each get set to nil. Attempting to unserialise msg while it's set to nil will crash out your script.

Also, you are calling getMsg() before it is declared, in lua any functions you call must be declared above where it is called, I know it weird.

This is not the case if the functions aren't localised. That's not to say that globalising them is a good idea, mind you.
Edited on 24 June 2014 - 02:23 AM
guesswhat123212 #5
Posted 24 June 2014 - 04:41 AM
you sir, are awesome, i did not even think about it not getting a reply and then crashing the whole thing because of serialize (even tho its trying to find out if there is another computer to talk to). I will add a check in there for that. as for the print statements I just started adding them as I typed 98% of this while at work in notepad++. I need most (if not all) of my functions to be global because from within this code there are 8 other codes that it can run and it uses some of the functions (tracking its coords in the big one, along with the rednet)
Bomb Bloke #6
Posted 24 June 2014 - 05:44 AM
Unless you want the functions to be called from an entirely different script, there's never any need to globalise anything.

Even if it's not possible to order your functions such that you can localise them in the usual way (which in itself may be a sign of unwanted recursion - watch out for infinite loops!), you could always use forward declarations.

In this case, that'd be done by sticking a line up the top of the script along these lines:

local getMsg, transmitData, sendMsg, <etc>