If you have specialised knowledge of integrating scripting into a Rust program (a game, specifically) with a relatively high performance target and relatively low concern for protection against malware (basic restrictions of host APIs is nice but ultimately not necessary), please hit me up I have questions as I’m not experienced with the specific field

specifically I’m switching away from Rhai because it’s too slow for my usage and because my game is an automation game that has all the content built with scripting, ideally I’d want a very high performance target, anything short of JIT or native won’t be enough. the game’s logic mostly consists of independent scripts that send messages to each other via an actor system, and rendering is done by asynchronously collecting rendering commands from scripts. wasm isn’t out of the picture but it’s clunky and I don’t know how to make it performant or how to minimise serialisation overhead.

#rust #gamedev #rustgamedev #scripting

on security:

I have low concerns for vulnerability protection as I’ll always force the source code of mods to be published (as I don’t want to load binaries, the game can spend all the time it wants to compile the scripts at runtime if needed) and it’s better to moderate mods and have reputation systems instead of building walls, and also because I don’t have the resources to deal with vulnerabilities so I’m just going to never trust mods and make it very clear that you should never install mods you don’t trust

@[email protected] factorio-style full sandboxing is probably a good idea to consider imho, just makes security a nonissue
@twinkle
I never did finish integrating Lua in my project. I was using mlua which supports luajit backends.

@twinkle That sounds exactly like Roto! https://codeberg.org/NLnetLabs/roto

(disclaimer: I work on Roto)

Also, it's not very stable, not fully featured and maybe not fast enough for your use case. Still I'd love to hear what you'd need from Roto to fit your use case

roto

The statically-typed, compiled embedded scripting language for Rust, used by Rotonda.

Codeberg.org

@terts oh, yes I’ve heard of Roto actually, and I really heavily considered it for my game, too (a rust gamedev friend is using it for their game and they recommended me it :D)

and yeah, some of the features it’s lacking pulled me back, here’s a short list:

  • iirc it doesn’t have for loops? not sure
  • no maps/dicts blows but it’s not the end of the world right now
  • constants would be nice… my game currently uses Rhai and hijacks the AST by replacing strings with integer IDs (with an interner) and it currently achieves this by having the script declare a list of names and string IDs in a function and the game injects constants with the names and IDs returned from that function. though it’s also possible to just replace all strings in the entire script with IDs instead
  • I’d like (mutable) references… for the most part my game’s host side and client side (imagine each tile in my game having its own scripting context and memory) share a generic data storage DataMap, which is just a mapping of some integer ID -> an enum Datum, and this Datum can hold lists and maps too. this is absolutely the one that most of the Rust-native langs just don’t have but I really don’t want to try having to serialse/deserialise in such a hot loop
  • alternative to the above: if the script context could own some user-defined state and I could read from it in an immutable reference, that’d be lovely too :) the biggest problems i face when thinking about doing any script integration work is “the script should really own this piece of data but this is often not a good idea, or if it was it would involve way too much finagling to read the state back on rust side”

@twinkle Cool that you heard of it! And good feedback! I'll go by them one by one.

- We have for loops over lists now, but no ranges. Definitely planned!
- Dicts are also planned but not started yet.
- I have a PR up for constants but I want to test it a bit more.
- I shink a shared datamap should be possible, especially if it's not generic, but I'm not 100% sure I understand.
- There's an issues about state, but it's pretty hard. Would love some input! https://codeberg.org/NLnetLabs/roto/issues/245

User-defined state

I really like the idea of the project, I was going to do something similar myself. One thing I'd really like to see is some form of user defined state, so that your code can remember some state, and read/write it later again. This probably needs some support from the caller, or can perhaps be st...

Codeberg.org
@twinkle if wasm checks most of the boxes, especially with wasmtime or another JIT enabled wasm runtime, then the issue of serialization stems from copying data back and forth. Because both the host and the guest are rust, or something enough rust like to not use utf16, you can use the wasm runtime to read the strings directly from the linear memory pointers a function returns, and from what I know, also write to linear memory blocks and pass that over when the code should read from it, but I'm unsure about the last part