Search code examples
c#visual-studiof#monogame

Record instance in F# is null in C#


I'm working on an assignment for school in which we make a small game using monogame, with the added challenge of working in F#. The game logic is fully immutable F#, and the entrypoint is in C# by use of the Game class in monogame. I have however come across a weird problem concerning Record types in F#. In the logic (F#) I have something along the lines of:

...
module Vectors =
    type Vector = {
        x : double
        y : double
    }

    let Zero : Vector = {x=0.0; y=0.0}
... 

And in C# I have some code accessing Zero:

...
player.vector = Vectors.Zero;
...

Weirdly enough, when I try to now use player.vector, it comes up as null. When debugging Vector.Zero is also null. I've looked around for a few solutions, and it may be some trivial mistake, but I can't seem to find it.


Solution

  • When you build an F# project as an exe, but use it as a library, occasionally some initialisation code doesn't get called.

    This can be fixed by changing the project to build as a library.