Moon Logic deprecated

by mk-fg

Adds Lua-programmable circuit network combinator. Based on Sandboxed LuaCombinator and LuaCombinator2 mods. Probably won't work in multiplayer games.

Content
2 years ago
1.0 - 1.1
4.97K
Circuit network

g [lua-qiz] How to validate signal names?

3 years ago
(updated 3 years ago)

Sorry, but I have next question.
(Yes, now i read docs and, I hope, did not miss the answer there again ^_^)

It strongly related to my previous thread.

if red['signal-N'] then
    local N = red['signal-N']
    out['signal-' .. N] = 1
end

What should we do with N > 9 ?
Name signal-23 is valid signal name?
(Not in vanilla. Yes if something like "Schall Virtual Signal" installed. Not if this disabled in it's setting…)
How to detect it?

Code like this for invalid N just makes error:

if red[N] ~= nil then
    out[N] = 1
else
    out['signal-0'] = 1
end

Now I use pcall for catch it:

if pcall(function() local r = red[N] end) then
    ....

Its work.
But… Is this the only way?
Maybe there is a simple (and correct) method that I don't see?

3 years ago
(updated 3 years ago)

Idk if it's simple or correct, but for that kind of metaprogramming, you'd probably want to access full factorio api, which is possible via "_api" table.
See last question here: https://mods.factorio.com/mod/Moon_Logic/faq
You can get full list and properties of whatever prototypes and signals that are ingame there.

But as the answer there is intended to suggest, you can easily get issues like this (and worse!):
https://mods.factorio.com/mod/Moon_Logic/discussion/607b1c3fa7cf997acf2be699
So just be careful and maybe if you want to have something complicated on top of that, just make a factorio mod - same lua api at this point :)

3 years ago
(updated 3 years ago)

Name signal-23 is valid signal name?

Btw, probably-obvious reason for raising an error on invalid signal names is simple typos - if these return 0 for any random thing, it'd be really hard to notice those, while raising an error makes them mostly-obvious (except for ones that happen to hit some other valid signal).
Pretty sure it's also a common reason for programming languages to have enum types and type systems in general.

Should be possible to expose all known signal names as a table, but idk if it's that useful - seem to be the kind of one-off thing like "how many items can be in the stack" that "_api" hack can solve, but not needed often enough to warrant extending an already-long list of special stuff in the "?" window.

3 years ago
(updated 3 years ago)

But as the answer there is intended to suggest, you can easily get issues like this

Hmm...
I think, try/catch (aka pcall) is more suitable for this case...
But thanks anyway.

Also...

if these return 0 for any random thing

Hmm... That about return nil for invalid?

just make a factorio mod - same lua api at this point :)

Maybe...
If I have a perspective to use dozens of your combinators with the same (and not so simple) program inside, maybe make my own combinator is a better way.
But before it this program need to be written and debugged. ^_^

3 years ago

Hmm... That about return nil for invalid?

Might make metaprogramming easier, but definitely sounds like less descriptive and weird way to signal errors.
I think constructing arbitrary signal names is likely an incredibly uncommon use-case, so eh, probably better to raise proper errors there.

maybe make my own combinator is a better way.

Yeah, it's a kind of folly behind this mod when going to _api and such stuff - basically allows you to make a crappier mod which you can't really share, which is probably not a good thing.
Shouldn't apply to simple "do some basic arithmetic with minimal logic on signals" types of uses though.

New response