Skip to content

Getting started

[dependencies]
factorio-rs-gui = "0.1"

Also install the factorio-rs-gui library mod from the Factorio mod portal and enable it with your mod. Your build emits require("__factorio-rs-gui__/…") and lists factorio-rs-gui in info.json dependencies.

For local monorepo work (examples in this repo):

factorio-rs-gui = { path = "../../ecosystem/factorio-rs-gui" }
use factorio_rs::factorio_api::{lua_fn, lua_fn0};
use factorio_rs::prelude::*;
use factorio_rs_gui::shared::button::Button;
use factorio_rs_gui::shared::frame::Frame;
use factorio_rs_gui::shared::text::Text;
use factorio_rs_gui::shared::widget::Widget;
const ROOT: &str = "my_mod_counter";
fn app() -> impl Into<Widget> {
let count = factorio_rs_gui::state!(0);
let label = format!("Count: {}", count.get());
let increment = lua_fn(move |event: OnGuiClickEvent| {
let _ = event;
count.set(count.get() + 1);
});
Frame::new()
.caption("Counter")
.centered()
.direction(GuiDirection::Vertical)
.child(Text::new(&label))
.child(Button::new("Increment").on_click(increment))
}
#[factorio_rs::event(OnPlayerCreated)]
pub fn on_player_created(event: OnPlayerCreatedEvent) {
if let Some(player) = game.get_player(IndexOrName::Index(event.player_index)) {
factorio_rs_gui::shared::runtime::mount(
player.gui().screen(),
ROOT,
lua_fn0(app),
);
}
}
#[factorio_rs::event(OnTick)]
pub fn on_tick(_event: OnTickEvent) {
factorio_rs_gui::shared::runtime::install(ROOT, lua_fn0(app));
}

No OnGuiClick stub: mount / install register clicks via script.on_event. Do not also define #[factorio_rs::event(OnGuiClick)] (it would replace that handler). Extra click logic: runtime::on_click.

From this repository (builds the library mod + the counter):

Terminal window
cd ecosystem/factorio-rs-gui && factorio-rs build && factorio-rs install
cd examples/gui_counter
factorio-rs build && factorio-rs install --open

Or depend on crates.io, enable the portal mod, and factorio-rs build / install your own project.

Next: State, Lifecycle, Widgets.