Simple computercraft question.

You say "simple question" so I'm a little worried I'm going overboard here, but I can't think of a way to explain this without just giving you some code.... so here's a reference against how I'd do it in case you can't figure it out for yourself.

I don't use much ComputerCraft (prefer OpenComputers) but they're very similar. The code should be mostly working but I haven't tested any of it and there are few blanks you will need to fill in before it'll even think about working.

So first off you want to create a blank 'gate object' and a list to store your gates (with each gate storing a gate name and a redstone cable color). I'd set it up like this:

Gate = { gate_name, gate_color}

function Gate (o)

`o = o or {}`

`setmetatable(o, self)`

self.__index = self

return o

end

The first line is obviously the most important, it defines the Gate object. The next function after that is something I copy and pasted from the Lua website and it's required to set up objects like that. You really shouldn't need to change that like... ever. It just kind of "sets up" the new Gate object to let us make copies of it, which we'll do next.

GateList =

{

`[1]=Gate:new{gate_name="1A", gate_color=`[`colors.red`](https://colors.red)`},`

`[2]=Gate:new{gate_name="1B", gate_color=`[`colors.green`](https://colors.green)`},`

`[3]=Gate:new{gate_name="2", gate_color=`[`colors.blue`](https://colors.blue)`},`

`[4]=Gate:new{gate_name="Stables", gate_color=colors.white}`

}

This all assumes you use a bundled cable for redstone communication but there's no reason you couldn't just replace colors with sides or something and use standard cables if you so desired.

Now you can run your chat loop and starts to work magic on the gates. Take the users inputted string and split it out on every blank space (" ") that's detected. There are code snippets online that can do it for you... I'd just copy and paste that function into the top of the document and then you can then set up your main input loop at the bottom of the page, something like this:

while(true) do

`local input = string.lower(read())`

`local input_parts = split(input, " ")`

`local command_type = input_parts[0]`

`local full_command = command_type + " " + input_parts[1]`

`if(full_command == "open gate" or full_command == "close gate" ) then`

    `local control_gate = input_parts[2]`

    `for i=0,#GateList do`

        `local thisGate = GateList[i]`

        `if(control_gate == "all" or string.lower(thisGate.gate_name) == control_gate) then`

HandleGate(command_type, thisGate)

        `end`

    `end` 

`end`

end

Nearly there. Now we need the HandleGate function that will toggle the specified gate based on the user command. I dunno how you set redstone stuff in ComputerCraft so the relevant SetRedstone lines will need changing below.

function HandleGate(command, gate)

local gateColor = gate.gate_color;

`if(command == "open") then`

SetRedstoneOutput(sides.top, gate.gate_color, 255)

print("Gate '" + gate.gate_name + "' has been OPENED.");

`elseif(command == "close") then`

SetRedstoneOutput(sides.top, gate.gate_color, 0)

print("Gate '" + gate.gate_name + "' has been CLOSED.");

`end`

end

I've not tested any of this and it'll probably crash if you type unexpected things into the chat box, but that should at least point you in the right direction...

Hope that helps some.

/r/feedthebeast Thread