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

Rednet reactor control help

Started by faolan89, 06 January 2016 - 11:34 PM
faolan89 #1
Posted 07 January 2016 - 12:34 AM
I made two programs, one for the reactor to receive messages from a pocket computer and another for the pocket computer to send messages to the computer. The computer on the reactor works fine but when I click a button on the pocket computer the program closes without doing anything. I have gotten it to work once but I didn't change anything and it stopped working again.


Computer Program: http://pastebin.com/XymCyihw

Pocket Computer Program: http://pastebin.com/fN9BgTbU

EDIT: From what i figured out the program isn't receiving all the info from the reactor that it should be. I made another program to see if it sends all of the info and it was able to receive and print everything that was sent.
Edited on 07 January 2016 - 02:55 AM
TheOddByte #2
Posted 07 January 2016 - 01:26 AM
I just want to point this out directly

click = { os.pullEvent("mouse_click") }
button = click[1]
xPos = click[2]
yPos = click[3]
This piece of code is incorrect, the os.pullEvent function first returns the event and then the other parameters, so this would be like this instead

local click = { os.pullEvent( "mouse_click" ) }
local button = click[2]
local xPos, yPos = click[3], click[4]
which could also be put into a single line like this

local event, button, xPos, yPos = os.pullEvent( "mouse_click" )

But one thing I don't understand is why you have multiple functions that handle events, it'd be much easier and cleaner code-wise to have it in one single function like this

local function handleEvents()
    local e = { os.pullEvent() }
    if e[1] == "rednet_message" then
        --# handle rednet event
    elseif e[1] == "mouse_click" then
        --# handle mouse clicks
    elseif e[1] == "key" then
        --# handle key events
    end
end
and then you could have a main function that would look something like this

local function main()
    while true do
        draw()
        handleEvents()
    end
end

--# Call the main function using pcall
local ok, err = pcall( main )
if not ok and err ~= "Terminated" then
    print( "An unexpected error occured: " .. err )
end


And this part I'm guessing you're expecting a table


isActive = message[3][1]
fuel = message[3][2]
fuelMax = message[3][3]
energyStored = message[3][4]
RFS = message[3][5]
reactivity = message[3][6]
waste = message[3][7]
caseTemp = message[3][8]
fuelTemp = message[3][9]
you could cram that into one line aswell using unpack

local isActive, fuel, fuelMax, energyStored, RFS, reactivity, waste, caseTemp, fuelTemp = unpack( message[3] )

Something you may have noticed is that I've declared most of your variables as locals, and I have to say that it's a much better idea to declare variables as such in most cases, it makes the program run faster etc, I'm sure someone can explain it better than me.

Oh and finally I'd like to ask, are you getting an error at all? Or is it just giving you a black-screen when it exits the program?
It's hard to know what the problem is when I don't know the error, the stuff above is just advices that can help you make your code more readable for yourself, and also to give you better coding habits.
faolan89 #3
Posted 07 January 2016 - 02:24 AM
Thanks, I'll try fixing some stuff. It doesn't give any errors it just closes out of the program as in the text stays there and it goes to the command line
faolan89 #4
Posted 07 January 2016 - 02:46 AM
I did what you said for fixing stuff and it works now apart from the unpack(message[3]) causing an error that says: reactorClient :37: attempt to index ? (a nil value)
faolan89 #5
Posted 07 January 2016 - 03:45 AM
From what i figured out the program isn't receiving all the info from the reactor that it should be. I made another program to see if it sends all of the info and it was able to receive and print everything that was sent.