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] top-N signal values

3 years ago

I am attempting to gather greatest 5 signals on a the red wire network. I am very new to Lua and have been struggling with the necessary code. I attempted to copy the red table to a new table and then sort that table with the expectation of being able to grab the top items, but that either has ended in errors or unexpected results. Any help would be greatly appreciated.

3 years ago
(updated 3 years ago)

Yeah, I'd probably try the same thing as you did - copy and sort, e.g.:

local top_signals, s = {}

-- Populate and reverse-sort (highest to lowest) top_signals table
for sig, v in pairs(red) do top_signals[#top_signals+1] = {sig=sig, v=v} end
table.sort(top_signals, function(a, b) return a.v > b.v end)

-- Set only current top-5 signals on both output wires
out = {}
for n = 1, 5 do
  s = top_signals[n]
  if not s then break end
  out[s.sig] = s.v
end

Somewhat non-intuitive bit might be the need for nested tables, as table.sort() would need to use top_signals table indexes for ordering.
Also out = {} is important to flush previous signals there, so that when/if top signal keys change, earlier ones won't be left with their old values.
Didn't test it, but something like that should work.

3 years ago

Got it, that makes sense. The table appears to populate correctly, but I am getting a runtime error on the sort line. The error code is
attempt to index field '?' (a nil value)

3 years ago

No idea. Post a screenshot of the code window, with all inputs/outputs and error visible there maybe?

Though in general, when something doesn't work, I'd suggest simplifying the problem as much as possible, to the point of toy examples, until you understand why/how they work or not. E.g. make some mock tables, try sorting those, store output in any global to see in a variable window, check what works, what fails, how it all works, etc.

3 years ago
(updated 3 years ago)

Oh, looks like I just forgot how table.sort() works, should be:
table.sort(top_signals, function(a, b) return a.v > b.v end)

Nothing ever works until you actually run it :)

3 years ago

Fantastic, that works perfectly. I really appreciate the help!

3 years ago
(updated 3 years ago)

No problem.

Note that you might want to also set some delay= in there, as in most cases signals change slowly as stuff gets shuffled around on the belts and such, on the order of dozens, hundreds or even thousands of ticks, so there's often no need to run such code on every single game tick.
Though probably shouldn't be a problem either, unless you build a lot of those all over the place.

3 years ago

Thanks for the tip. I have it tied to the the net LTN requester/provider combinators which have a delay set to 120 so I matched that delay. At the moment this has been my only usage of the moon logic combinator so the impact on UPS is negligible, but as I become more familiar with Lua I am sure it will be implemented in greater quantities.

New response