Webassembly instead of/alongside lua?

Place to post guides, observations, things related to modding that are not mods themselves.
Post Reply
Asynchron
Burner Inserter
Burner Inserter
Posts: 18
Joined: Fri Dec 02, 2022 9:10 pm
Contact:

Webassembly instead of/alongside lua?

Post by Asynchron »

Hi

So just wondering, how complicated would be to add support for webassembly mods alongside lua mods?
If this is too much for this game, would you consider something as webassembly instead of lua, in a future game similar to factorio (if you'd make one?)

Lua is nice for having sandboxed environment, and is also simple to interact with from application side. However it is terribly slow, and inefficient for games such as Factorio, and hinders development of mods that are performance heavy. For example making a combinator controlling assembler recipes based on circuit network signals, would hog down performance quite a lot, if you have 10-20 such combinators in a game.

Webassembly for example, is a virtual machine, and therefore offers sandboxing capabilities, plus it has implementations that are lot faster than lua (wasm3 for example). It also has a benefit of choosing what language to use for making your mod, and therefore you could choose a language that has best features for your particular case (such as value types), and from the looks of one implementation of it (wasm3) it is also simple in api from application point of view.

tecanec
Manual Inserter
Manual Inserter
Posts: 1
Joined: Wed Sep 06, 2023 4:10 pm
Contact:

Re: Webassembly instead of/alongside lua?

Post by tecanec »

I doubt they would do that for Factorio.

Replacing Lua in its entirety would take a lot of work for the developers, and it would immediately make all existing mods incompatible. You could make a Lua interpreter in WebAssembly, but that would only ease the transition, not make it invisible.

Having WebAssembly alongside Lua would also take a lot of work for the developers, and the two are very different internally. (One is a RAM-based bytecode meant to be compiled into machine code and made for whole applications, the other is a garbage-collected language meant to be interpreted and made for simple scripts.) It would be hard to bridge the gap, and would require twice as much work put into maintenance.

Not to mention, even if they did add WebAssembly support, it would not be able to match the core engine in performance. Factorio requires a ton of optimizations that are better left to the engine, since it can more easily tackle the huge amount of data that must be processed on every tick (organizing such data in CPU-friendly ways is an important part of optimization nowadays), and so mods will continue to rely on whatever the engine provides for anything that must stay performant.

Asynchron
Burner Inserter
Burner Inserter
Posts: 18
Joined: Fri Dec 02, 2022 9:10 pm
Contact:

Re: Webassembly instead of/alongside lua?

Post by Asynchron »

Having WebAssembly alongside Lua would also take a lot of work for the developers, and the two are very different internally. (One is a RAM-based bytecode meant to be compiled into machine code and made for whole applications, the other is a garbage-collected language meant to be interpreted and made for simple scripts.)
Well it is a bytecode, and it can be interpreted, not necessarily compiled to machine code.
Also, the interface for Wasm3 (interpreter for webassembly) seems simple and similar to lua bindings, at least from quick overview. i.e. you initialize the environment, expose couple of methods, interpret the mod, and expect some results back, same as with lua bindings.
It would be hard to bridge the gap, and would require twice as much work put into maintenance.
Well, that's why I've asked, hoping some devs would answer whether it is feasible to add support for it, or at least consider for another game they'll do that also is as mod-able as factorio.
Not to mention, even if they did add WebAssembly support, it would not be able to match the core engine in performance. Factorio requires a ton of optimizations that are better left to the engine, since it can more easily tackle the huge amount of data that must be processed on every tick (organizing such data in CPU-friendly ways is an important part of optimization nowadays), and so mods will continue to rely on whatever the engine provides for anything that must stay performant.
Well clearly, it won't match it, but it will still be better than lua, not considering other benefits such as optimizations done by your language compiler on language level, and then on bytecode level. There are at least couple of mods right now, that already do a lot of computations, and are usable only on small scale due to lua's nature.

Post Reply

Return to “Modding discussion”