DEV Community

Cover image for How To Setup MonoGame for F#
SabeDoesThings
SabeDoesThings

Posted on

How To Setup MonoGame for F#

There are little to NO guides on using F# with MonoGame..
well that's bit of an over statement there are a few on the internet but they are either outdated or just don't work at all.

So I got to work and figured out how to setup an F# project to use MonoGame! There are probably other ways to do it but this way worked for me and I want to show how other people can do it too.

So let's get started!

Start by creating a new F# console project.

dotnet new console -lang F# -o <ProjectName>
cd <ProjectName>
Enter fullscreen mode Exit fullscreen mode

Add the MonoGame Framework

dotnet add package MonoGame.Framework.DesktopGL
Enter fullscreen mode Exit fullscreen mode

Create Game1.fs

Game1.fs

namespace <Project Name>

open Microsoft.Xna.Framework
open Microsoft.Xna.Framework.Graphics
open Microsoft.Xna.Framework.Input

type Game1() as this =
    inherit Game()

    let graphics = new GraphicsDeviceManager(this)
    let mutable spriteBatch: SpriteBatch = null

    do
        this.Content.RootDirectory <- "Content"
        this.IsMouseVisible <- true

    override _.Initialize() =
        // TODO: Add your initialization logic here

        base.Initialize()

    override _.LoadContent() =
        spriteBatch <- new SpriteBatch(this.GraphicsDevice)

        // TODO: use this.Content to load your game content here

    override _.Update(gameTime: GameTime) =
        let kstate = Keyboard.GetState()
        if kstate.IsKeyDown Keys.Escape then
            this.Exit()

        // TODO: Add your update logic here

        base.Update gameTime

    override _.Draw(gameTime: GameTime) =
        this.GraphicsDevice.Clear Color.CornflowerBlue

        spriteBatch.Begin()
        // TODO: Add your drawing code here
        spriteBatch.End()

        base.Draw gameTime
Enter fullscreen mode Exit fullscreen mode

Add to Program.fs file

open <Project Name>

[<EntryPoint>]
let main argv =
    use game = new Game1()
    game.Run()
    0
Enter fullscreen mode Exit fullscreen mode

the .fsproj file should look like this:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net9.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <Compile Include="Game1.fs" />
    <Compile Include="Program.fs" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.4.1" />
  </ItemGroup>

</Project>

Enter fullscreen mode Exit fullscreen mode

Adding Content.mgcb

Create folder called "Content"

Add dotnet tools

dotnet new tool-manifest
dotnet tool install dotnet-mgcb
dotnet tool install dotnet-mgcb-editor
dotnet tool install dotnet-mgcb-editor-windows
dotnet tool install dotnet-mgcb-editor-linux
dotnet tool install dotnet-mgcb-editor-mac
Enter fullscreen mode Exit fullscreen mode

Open MGCB Editor

First you have to have it installed

dotnet tool install -g dotnet-mgcb
dotnet tool install -g dotnet-mgcb-editor
Enter fullscreen mode Exit fullscreen mode

Next in a command line in your directory use this command

dotnet tool restore
mgcb-editor
Enter fullscreen mode Exit fullscreen mode

Generate the Content.mgcb file

Once you are inside the MGCB Editor do
File -> New
Go to your directory inside of the Content folder and name the file "Content.mgcb"

Done!

You should now have the Content file and are able to build assets for you're games.

Top comments (0)