Header menu logo Mibo

Program Module

Functions for creating and configuring Elmish game programs.

A program defines the complete architecture of a Mibo game: initialization, update logic, subscriptions, rendering, and MonoGame component integration.

Example

 Program.mkProgram init update
 |> Program.withSubscription subscribe
 |> Program.withRenderer (Batch2DRenderer.create view)
 |> Program.withTick Tick
 |> Program.withAssets
 |> Program.withInput
 |> ElmishGame |> _.Run()

Functions and values

Function or value Description

mkProgram init update

Full Usage: mkProgram init update

Parameters:
    init : GameContext -> 'Model * Cmd<'Msg> - Function that receives GameContext and returns initial (Model, Cmd)
    update : 'Msg -> 'Model -> 'Model * Cmd<'Msg> - Function that receives a message and model, returns (Model, Cmd)

Returns: Program<'Model, 'Msg>

Creates a new program with the given init and update functions.

This is the starting point for building an Elmish game. The init function creates the initial model and startup commands, while update handles messages.

init : GameContext -> 'Model * Cmd<'Msg>

Function that receives GameContext and returns initial (Model, Cmd)

update : 'Msg -> 'Model -> 'Model * Cmd<'Msg>

Function that receives a message and model, returns (Model, Cmd)

Returns: Program<'Model, 'Msg>
Example

 let init ctx = struct (initialModel, Cmd.none)
 let update msg model = struct (model, Cmd.none)
 let program = Program.mkProgram init update
val init: ctx: 'a -> struct ('b * 'c)
val ctx: 'a
val update: msg: 'a -> model: 'b -> struct ('b * 'c)
val msg: 'a
val model: 'b
val program: obj

withAssets program

Full Usage: withAssets program

Parameters:
Returns: Program<'Model, 'Msg>

Registers the IAssets service for loading and caching game assets.

The assets service provides texture, font, sound, and model loading with automatic caching. It also supports custom asset types and JSON deserialization. Assets are automatically disposed when the game exits.

program : Program<'Model, 'Msg>
Returns: Program<'Model, 'Msg>
Example

 program |> Program.withAssets

 // Then in your code:
 let texture = Assets.texture "sprites/player" ctx
 let font = Assets.font "fonts/main" ctx
val texture: obj
val font: obj

withComponent factory program

Full Usage: withComponent factory program

Parameters:
Returns: Program<'Model, 'Msg>

Adds a MonoGame component to the program.

Components are added before Initialize is called, so they participate in the normal MonoGame lifecycle (Initialize, LoadContent, Update, Draw).

factory : Game -> IGameComponent
program : Program<'Model, 'Msg>
Returns: Program<'Model, 'Msg>
Example

 program |> Program.withComponent (fun game -> new AudioComponent(game))

withComponentRef componentRef factory program

Full Usage: withComponentRef componentRef factory program

Parameters:
Returns: Program<'Model, 'Msg>

Adds a component with a reference that can be accessed from update/subscribe.

This allows Elmish code to interact with MonoGame components in a type-safe way without relying on global state.

componentRef : ComponentRef<'T>
factory : Game -> 'T
program : Program<'Model, 'Msg>
Returns: Program<'Model, 'Msg>
Example

 let audioRef = ComponentRef<AudioComponent>()

 program |> Program.withComponentRef audioRef (fun game -> new AudioComponent(game))

 // Later in update:
 match audioRef.TryGet() with
 | ValueSome audio -> audio.Play("sound")
 | ValueNone -> ()
val audioRef: obj
union case ValueOption.ValueSome: 'T -> ValueOption<'T>
val audio: obj
union case ValueOption.ValueNone: ValueOption<'T>

withConfig configure program

Full Usage: withConfig configure program

Parameters:
Returns: Program<'Model, 'Msg>

Configure MonoGame game settings (resolution, vsync, window, etc).

The callback receives the Game instance and GraphicsDeviceManager for configuration. This is called during the game constructor, before Initialize.

configure : Game * GraphicsDeviceManager -> unit
program : Program<'Model, 'Msg>
Returns: Program<'Model, 'Msg>
Example

 program |> Program.withConfig (fun (game, graphics) ->
     graphics.PreferredBackBufferWidth <- 1920
     graphics.PreferredBackBufferHeight <- 1080
     graphics.IsFullScreen <- false
     game.IsMouseVisible <- true
 )

withDispatchMode mode program

Full Usage: withDispatchMode mode program

Parameters:
Returns: Program<'Model, 'Msg>

Configures how the runtime schedules messages dispatched while processing a frame.

Use DispatchMode.Immediate for maximum responsiveness (default), or DispatchMode.FrameBounded to guarantee that messages dispatched during processing are deferred to the next MonoGame Update call.

mode : DispatchMode
program : Program<'Model, 'Msg>
Returns: Program<'Model, 'Msg>

withFixedStep cfg program

Full Usage: withFixedStep cfg program

Parameters:
Returns: Program<'Model, 'Msg>

Enables a framework-managed fixed timestep simulation.

When enabled, the runtime will dispatch the mapped message zero or more times per MonoGame Update call to advance simulation in stable increments.

This is complementary to Program.withTick: you can use fixed-step messages for simulation and keep Tick for per-frame tasks (UI, camera smoothing, etc).

cfg : FixedStepConfig<'Msg>
program : Program<'Model, 'Msg>
Returns: Program<'Model, 'Msg>

withInput program

Full Usage: withInput program

Parameters:
Returns: Program<'Model, 'Msg>

Registers the input polling component for keyboard, mouse, touch, and gamepad.

The input service polls hardware each frame and publishes deltas via IInput. This is required for using the Keyboard, Mouse, Touch, and Gamepad subscription modules. This helper is idempotent - calling it multiple times has no effect.

program : Program<'Model, 'Msg>
Returns: Program<'Model, 'Msg>
Example

 program |> Program.withInput

 // Then subscribe to input:
 Keyboard.onPressed KeyPressed ctx
 Mouse.onLeftClick MouseClicked ctx
 Gamepad.listen GamepadInput ctx

withInputMapper initialMap program

Full Usage: withInputMapper initialMap program

Parameters:
Returns: Program<'Model, 'Msg>

Configures the game to register an IInputMapper service.

This registers IInput automatically (equivalent to Program.withInput).

The mapper is ticked via a MonoGame GameComponent.

If you want to stay fully "Elmish" (no service access), consider using InputMapper.subscribe instead and handle a single message.

initialMap : InputMap<'Action>
program : Program<'Model, 'Msg>
Returns: Program<'Model, 'Msg>

withRenderer factory program

Full Usage: withRenderer factory program

Parameters:
Returns: Program<'Model, 'Msg>

Adds a renderer to the program.

Renderers are called each frame to draw the current model state. Multiple renderers can be added (e.g., 2D UI on top of 3D scene).

factory : Game -> IRenderer<'Model>
program : Program<'Model, 'Msg>
Returns: Program<'Model, 'Msg>
Example

 program |> Program.withRenderer (Batch2DRenderer.create view)

withSubscription subscribe program

Full Usage: withSubscription subscribe program

Parameters:
Returns: Program<'Model, 'Msg>

Adds a subscription function to the program.

The subscription function is called after each model update. It should return subscriptions based on the current model state. The runtime manages subscription lifecycle automatically through SubId diffing.

subscribe : GameContext -> 'Model -> Sub<'Msg>
program : Program<'Model, 'Msg>
Returns: Program<'Model, 'Msg>
Example

 let subscribe ctx model =
     Keyboard.onPressed KeyPressed ctx

 program |> Program.withSubscription subscribe
val subscribe: ctx: 'a -> model: 'b -> 'c
val ctx: 'a
val model: 'b

withTick map program

Full Usage: withTick map program

Parameters:
Returns: Program<'Model, 'Msg>

Adds a per-frame tick message to the program.

The tick function is called once per frame and can dispatch a message containing the GameTime for time-based updates.

map : GameTime -> 'Msg
program : Program<'Model, 'Msg>
Returns: Program<'Model, 'Msg>
Example

 type Msg = Tick of GameTime | ...
 program |> Program.withTick Tick

Type something to start searching.