Computer Core

by Relik77

Computer is a modification for Factorio that’s all about computer programming. It allows you to write programs using the Lua programming language.

Utilities
6 years ago
0.15 - 0.16
25

g How should I get/return circuit signals?

6 years ago

Is there a way to treat it as signal combinator stuff by using this mod? I see there are 2 points where I can attack circuit wires but can't figure out how to use them

6 years ago
(updated 6 years ago)

Hi,

Indeed, there are two points for attaching red and green wires. This in order to be able to use one of the points in "reading" and the other in "writing"

These points are respectively named "right" and "left". Apis are dedicated to work with.
With the left point, there is:
- lan.readLeftSignals(wire)
- lan.writeLeftSignals(signals)

With the right point, there is:
- lan.readRightSignals(wire)
- lan.writeRightSignals(signals)

"wire" can mean the wire to read and can have several values :
- defines.wire_type.red
- defines.wire_type.green
(see http://lua-api.factorio.com/0.15.36/defines.html#defines.wire_type)

For writing, the target point behaves like a combinator

A small example to illustrate:
We consider that a signal is received on the left point with a red wire and we emit on the point of right (after modifying)

function loop()
local signals = lan.readLeftSignals(defines.wire_type.red)
-- signals like {{signal={type="item",name="uranium-fuel-cell"},count=1}}

term.write(signals) -- Show signal value on term

for _, signal in pairs(signals) do
    signal.count = signal.count + 1
end

lan.writeRightSignals(signals) -- Write signal on right point

os.wait(loop, 1)

end
loop()

Does it help you?

6 years ago

Thank you! Now I think I can handle it out myself!

By the way, great mode really. I'm not a big fan of using bunch of combinators and computer looks reallt cool

6 years ago

{{signal={type="item",name="uranium-fuel-cell"},count=1}}

Is that some sort of array or something? How do I use that information in an if-statement for example?

6 years ago

Hi,

Yes it's en array in Lua. So you can :

local tab = {{signal={type="item",name="uranium-fuel-cell"},count=1}}
if #tab > 0 then // same as "if (count(tab) > 0)" in php or "if (tab.length > 0)" in js
   // you can get tab[1] for read first value of array, or use a "foreach" loop
   for index, value in ipairs(tab) do // ipairs is an iterator for array, pairs is an iterator for object
      if value["count"] > 0 then
         term.write(value["signal"]["name"] .. " = " .. value["count"])
      end
   end
end
5 years ago

I keep getting an "Unknown command..." error message if I type:
lan.readRightSignals(defines.red_wire.type)
or
lan.readRightSignals()

5 years ago
(updated 5 years ago)

I keep getting an "Unknown command..." error message if I type:
lan.readRightSignals(defines.red_wire.type)
or
lan.readRightSignals()

Where are you typing this? Into a file or directly on the console?

New response