Web Games

 As well as getting something running on mobile (as discussed last week), one of the things I've been looking into is getting a game working on the web.

MonoGame doesn't have an official Web project like it does for other platforms. It is possible to use Bridge.NET to compile the code into Javascript, although I'd avoided that since it still requires the code to run from a server, and the examples to do that locally used Python and any time I see something requires me to use Python, I immediately close the tab.

It's not that I've never used Python before. I was able to modify a script that somebody else had written to change it for my own purposes (accessing a TV show from an American broadcaster's site without using a VPN), but any time I have tried to do anything else, it absolutely disgusts me and I decide that I can live without whatever it was I was attempting to do.

There have been other attempts at getting MonoGame working on the web, but none of them are far enough along that they are really usable.

Similar to Bridge.NET, JSIL can convert C# code into Javascript, and had specific XNA support. The problem is, the project hasn't been updated for 5 years and MonoGame is a rewrite of XNA, so even if it does miraculously manage to convert my code, it's likely to be horrendously broken.

I also came across a project which attempts to convert a MonoGame project into a Blazor app, but it is very limited in what it converts, so it's likely it won't work on a full game.

So there's two options left.

The first is that I rewrite my code in Javascript. I wouldn't like doing it, but it would work. But preferably I'd like to not rewrite my codebase when I want my game playable in the web. (I could go ahead and make the game in Unity or Game Maker if I knew in advance I wanted to port it to the web).

The second is I try to write my own converter. I've written code that generates code before, and I've written parsers for XML and JSON (before discovering that Newtonsoft make it so much easier), but I haven't done anything on this scale.

Converting a variable declaration would pretty simple.

private string id = "hello";

would become

let id = 'hello';

Simple methods would be somewhat straightfoward too.

public void Move(int x, int y)
{
    position.x += x;
    position.y += y;
}

would turn into

function Move(x, y) {
    position.x += x;
    position.y += y;
}

Classes become a little more difficult since Javascript doesn't technically support them, even though it sort of does. So what if I used Typescript instead? Typescript is basically Javascript's more sensible younger brother that will complain at you if you try to assign a string to a number, unlike Javascript deciding to actually let you get away with doing something so silly.

I once saw a coder trying to assign a colour to a boolean, and I initially thought they were doing my trick of being so frustrated at something not working that they start attempting stupid things in the hopes they stumble across a solution. Then they did it again, and I've never been more thankful for type safety in programming languages.

The problem with Typescript is, as sensible as it is, I hate it. Javascript lets you get away anything, but Typescript is too strict. Javascript complements my style of coding, which is just writing and seeing what happens. But Typescript expects me to have an entire document written out about how I've planned to do this, so when I say "Yeah, an object will go here, I've not decided what it is yet", it tells me I need to decide right now and go and implement that object before I can continue writing whatever random nonsense I want to test out.

And whatever language I choose, I'm going to come up against issues in other libraries. In the case of System, there's usually a Javascript equivalent for whatever I want to do. Lists will become arrays, file reading / writing can be done using the JSON object and saving to and from local storage. But when it comes to MonoGame, a lot of that functionality isn't as easy to replace.

There's also dependencies to keep track of. Once converted to Javascript, files would have to be loaded in a certain order to make sure that everything works correctly. If a rectangle is being drawn to the screen in one file, but the code that defines what a rectangle is hasn't been loaded yet, then an error will get thrown because it has no concept of a rectangle.

All of this makes me want to write my code from scratch in Javascript, but if I do that for every MonoGame project I want to put on the web, eventually I'll reach a point where it will have been faster investing the time into writing a converter. Plus, if I write a converter then others can use it and it will save them time.

Or I could just not release a game playable in the web browser. The original intent behind it was so games were playable on mobile, but since I've now got builds running on iOS and Android then it's not as much of a concern. So, we'll see.

Popular posts from this blog

Getting Started with the FireRed Decompilation

Phoning It In

Tonight Matthew, I'm Going to Make...