There's no trivial supported way to do it, as there's no shared global environment for these combinators atm - code and vars on them are supposed to be all-local.
But if you want to go into "there be dragons" territory, can use kinda-hidden "_api" var (see also FAQ on it), which you can use to pull code from any specific combinator (e.g. dedicated one in the corner, only used for templating), via something like _api.global.combinators[uid].code
.
You can then turn that code string into a lua function (or code object) via lua's load()
, and I think best place to store those is probably under some custom table that you can create under _api, e.g. _api.my_funcs = {}
.
But anywhere those are used, make sure to check if not _api.my_funcs then ...(load those from somewhere)... end
, because they'll be wiped on save/load.
And also maybe add/check a "code version" var somewhere, for reloading it on changes.
"wiped on save/load" is (or was?) a limitation in factorio save/load system, which doesn't allow to serialize lua code objects, so this mod also does load()
on all combinator code strings when game is loaded, and you'd probably need to do this for any such shared funcs as well.
I'm not sure if maybe these days this limitation might no longer exist - didn't check - and if it doesn't, then you can also probably save code objects more permanently under _api.global
.