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

"rednet.send" problems; sending nil

Started by DiegoG, 23 October 2016 - 06:10 PM
DiegoG #1
Posted 23 October 2016 - 08:10 PM
So, I've been working on a program for quite a while now; and even though it mostly works, there are a few errors that I can't seem to fix, or don't even know why they exist in the first place. Mainly, as the title says, it's because of the RedNet API function: "rednet.send"; I'm not sure what am I doing wrong but it very often sends commands and strings wrong, like: During startup, the server receives a table value. The first time it tries to fetch the commands from the server, it always fails. The first time it tries to register itself in the server, it always fails. Most times I need the server to return something (Example: the string "Pong!") it returns nil; and now for some reason when my pocket computer tries to fetch the commands the server receives it as nil.

Here's the Server's Source Code
EDIT: A.1.2.11 --10/24/2016

And Here's the Pocket Computer's (or RCD (Remote Controller Device))'s Source Code
EDIT: A.1.2.2 --10/24/2016

I also took a few screenshots, all which are available in this Imgur album
Any help is very appreciated; if my code lacks any comments or you don't understand something, please let me know
Also feel free to correct any mistakes I've made or a way to improve my code, which is also very appreciated.
Edited on 24 October 2016 - 11:21 PM
Lupus590 #2
Posted 23 October 2016 - 10:35 PM
ServerStartup.lua line 2: checkForModem_modem is not defined yet, move function above the one which calls it. Additionally, that is not how to call a function if you are trying to send a function over rednet then you can't due to limitations of our current Lua version.

RCDStartup.lua line 2: same issue as above

This issue seems to be throughout your code, you need to tell the computer what something is before you tell it to use it. I'm interested in how you managed to get this to even run.
Edited on 23 October 2016 - 08:36 PM
DiegoG #3
Posted 23 October 2016 - 11:05 PM
ServerStartup.lua line 2: checkForModem_modem is not defined yet, move function above the one which calls it. Additionally, that is not how to call a function if you are trying to send a function over rednet then you can't due to limitations of our current Lua version.

RCDStartup.lua line 2: same issue as above

This issue seems to be throughout your code, you need to tell the computer what something is before you tell it to use it. I'm interested in how you managed to get this to even run.

Simple.
If you had read the code you'd know; I define every function, and then I make a startup function; in which everything is run and defined in the order it needs to be; and, once more, the startup function is called after itself is defined.

EDIT:
I forgot to mention, I'm not trying to send a function over rednet; I send over a string which is read at the other side, for example:

alpha = 1
beta = 2
rednet.open(side)
rednet.send(beta, "do_something")
rednet.open(side)
id, message = rednet.receive(10)
if message == "do_something" then
something()
end
Edited on 24 October 2016 - 12:16 AM
Bomb Bloke #4
Posted 24 October 2016 - 02:24 AM
This issue seems to be throughout your code, you need to tell the computer what something is before you tell it to use it. I'm interested in how you managed to get this to even run.

That bit works because checkForModem_modem is defined within the global scope by checkForModem(), which is called before setHost().

Granted, everything would ideally be localised, but that particular bit won't error. It'd also be a heck of a lot easier to read if not for the redundant functions everywhere… it's less "simple" and more "reading a choose-your-own-adventure novel with only one paragraph on each page". :|

(When writing a function, ask yourself - am I going to call this from more than one place in the script? If the answer's no, then reconsider what you're doing - don't use function definitions as if they were comments!)

Speaking of which, keep in mind that whenever you perform a function call, Lua puts a new running instance of that function onto the function stack. That then stays there until your function returns, at which point the next function down continues from where it left off.

This means that if you create a loop of recursive function calls, such as the one you've constructed between the logIn() and logIn_retry() functions within RCDStartup.lua, there's a potential crash bug when Lua eventually runs out of the memory reserved for such shenanigans and you get a stack overflow. If you want to perform a loop, use while/repeat/for.

Remember that if you make a variable local to a given block it'll effectively be discarded from memory as soon as that block ends. For example, no matter how this lot resolves:

Spoiler
        if not senderID_A or id == senderID_A then
            local senderID_A = id
            local senderUSER_A = username
        elseif not senderID_B or id == senderID_B then
            local senderID_B = id
            local senderUSER_B = username
        elseif not senderID_C or id == senderID_C then
            local senderID_C = id
            local senderUSER_C = username
        elseif not senderID_D or id == senderID_D then
            local senderID_D = id
            local senderUSER_D = username
        elseif not senderID_E or id == senderID_E then
            local senderID_E = id
            local senderUSER_E = username
        else
            printMessage(1, "Cannot store anymore IDs")
            return false
        end

… senderID_A / senderUSER_A / senderID_B / etc will all be nil at the end of it, because you localised them to those tiny little "if" blocks. You'd be better off defining them above as upvalues, or better again, sticking them all into the one table (which you could then iterate through instead of repeating your code).

http://lua-users.org/wiki/ScopeTutorial

Anyway, line 203 in RCDStartup.lua - did you not mean to print "reply"?
DiegoG #5
Posted 25 October 2016 - 01:27 AM
Spoiler
This issue seems to be throughout your code, you need to tell the computer what something is before you tell it to use it. I'm interested in how you managed to get this to even run.

That bit works because checkForModem_modem is defined within the global scope by checkForModem(), which is called before setHost().

Granted, everything would ideally be localised, but that particular bit won't error. It'd also be a heck of a lot easier to read if not for the redundant functions everywhere… it's less "simple" and more "reading a choose-your-own-adventure novel with only one paragraph on each page". :|

(When writing a function, ask yourself - am I going to call this from more than one place in the script? If the answer's no, then reconsider what you're doing - don't use function definitions as if they were comments!)

Speaking of which, keep in mind that whenever you perform a function call, Lua puts a new running instance of that function onto the function stack. That then stays there until your function returns, at which point the next function down continues from where it left off.

This means that if you create a loop of recursive function calls, such as the one you've constructed between the logIn() and logIn_retry() functions within RCDStartup.lua, there's a potential crash bug when Lua eventually runs out of the memory reserved for such shenanigans and you get a stack overflow. If you want to perform a loop, use while/repeat/for.

Remember that if you make a variable local to a given block it'll effectively be discarded from memory as soon as that block ends. For example, no matter how this lot resolves:

Spoiler
		if not senderID_A or id == senderID_A then
			local senderID_A = id
			local senderUSER_A = username
		elseif not senderID_B or id == senderID_B then
			local senderID_B = id
			local senderUSER_B = username
		elseif not senderID_C or id == senderID_C then
			local senderID_C = id
			local senderUSER_C = username
		elseif not senderID_D or id == senderID_D then
			local senderID_D = id
			local senderUSER_D = username
		elseif not senderID_E or id == senderID_E then
			local senderID_E = id
			local senderUSER_E = username
		else
			printMessage(1, "Cannot store anymore IDs")
			return false
		end

… senderID_A / senderUSER_A / senderID_B / etc will all be nil at the end of it, because you localised them to those tiny little "if" blocks. You'd be better off defining them above as upvalues, or better again, sticking them all into the one table (which you could then iterate through instead of repeating your code).

http://lua-users.org...i/ScopeTutorial

Anyway, line 203 in RCDStartup.lua - did you not mean to print "reply"?

Welp, I think I've fixed most of it; all the functions I only called once were merged into startup(); I eliminated a few useless functions and merged a couple of redundant ones. I also fixed some of it on the Server script but I didn't find much, except for the same useless functions and one-time called functions; I also followed your advice and decided to use a table instead; found out it's MUCH easier, wonder why I didn't think of it before. Anyways, I can't try out the code now, but I'd like you to review it anyway if you can. I edited the main post and added the new versions below the originals (Regarding this topic)
Also… thanks for your help, sorry for my ineptitude…
Bomb Bloke #6
Posted 25 October 2016 - 03:04 AM
No need to apologise, no one knows everything.