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

Help with intercommunications system

Started by DiegoG, 18 October 2016 - 09:35 PM
DiegoG #1
Posted 18 October 2016 - 11:35 PM
I've been working on a pretty complex project lately, but I'm not sure how to make it work, really. I'm sort of new to Lua and programming in general, so I'd likely be making some serious mistakes in my code. Any help is appreciated.

PhoneStartup.lua (Pocket Computer)

ServerStartup.lua –Most comments are here

Mainly the errors are that firstly, I don't think I know how to handle external files; second, I don't think the rednet connection will work this way; and lastly, I've tried removing the piece that handles external files and it only gives me a rednet error saying it expected a number at line 86.
Bomb Bloke #2
Posted 19 October 2016 - 02:15 AM
In PhoneStartup.lua, line 14 reads:

if isRegistered == false or nil then

Comparisons don't work like that - a single value on its own simply counts as false (if it is nil or false), or true (if it's anything else), so your "or nil" is redundant (it's the logical equivalent of "or, if this this untrue statement is true"…). If you specifically want to check whether "isRegistered" is equal to false, or "isRegistered" is equal to nil, then you actually have to phrase it that way:

if isRegistered == false or isRegistered == nil then

Though in this case, we can simplify by turning the statement on its head - since "isRegistered" counts as true if it's not false / nil, and we can invert using "not", we can do:

if not isRegistered then

You make this sort of mistake in numerous places. Line 125 is even worse:

if id or message or protocol not nil then

I'm surprised that butchered syntax doesn't immediately error out on you. You meant:

if id and message and protocol then  -- if (id is non-false/nil), and (message is non-false/nil), and (protocol is non-false/nil), then...

Though that'll still cause problems, because you never defined a protocol when you sent your messages from the other script, so "protocol" will be nil whether a message was received or not. "id" is the only variable that really needs to be checked here - if that's nil then you know all the others are too.

Line 30 reads:

isRegistered.close()

Though you never opened a file handle against isRegistered, and that line is placed at a point where it'll never be executed (due to the return statements above it). You do open a handle against userData, though, on line 16, which you don't attempt to close - nor do you ever try to write anything through it.
Edited on 19 October 2016 - 12:16 AM
DiegoG #3
Posted 19 October 2016 - 11:10 PM
In PhoneStartup.lua, line 14 reads:

if isRegistered == false or nil then

Comparisons don't work like that - a single value on its own simply counts as false (if it is nil or false), or true (if it's anything else), so your "or nil" is redundant (it's the logical equivalent of "or, if this this untrue statement is true"…). If you specifically want to check whether "isRegistered" is equal to false, or "isRegistered" is equal to nil, then you actually have to phrase it that way:

if isRegistered == false or isRegistered == nil then

Though in this case, we can simplify by turning the statement on its head - since "isRegistered" counts as true if it's not false / nil, and we can invert using "not", we can do:

if not isRegistered then

You make this sort of mistake in numerous places. Line 125 is even worse:

if id or message or protocol not nil then

I'm surprised that butchered syntax doesn't immediately error out on you. You meant:

if id and message and protocol then  -- if (id is non-false/nil), and (message is non-false/nil), and (protocol is non-false/nil), then...

Though that'll still cause problems, because you never defined a protocol when you sent your messages from the other script, so "protocol" will be nil whether a message was received or not. "id" is the only variable that really needs to be checked here - if that's nil then you know all the others are too.

Line 30 reads:

isRegistered.close()

Though you never opened a file handle against isRegistered, and that line is placed at a point where it'll never be executed (due to the return statements above it). You do open a handle against userData, though, on line 16, which you don't attempt to close - nor do you ever try to write anything through it.

Thank you for the help, I've fixed all the mistakes you pointed out in both files; also, you said here
nor do you ever try to write anything through it.
I thought that I could do that by using the
print()
function while the handler was active; I guess I'm wrong?
Bomb Bloke #4
Posted 20 October 2016 - 03:10 AM
print() writes text to the active terminal display. If you want to output text through a specific file handle, then you do it through that file handle.

local userData = fs.open("/user/localuser", "w")
userData.writeLine("Hello World!")
userData.close()