Communication between Javascript and Unity3d

Communication between Javascript and Unity3d

Path to becoming a game developer

So, let's get something straight. I don't have the time to regularly play video games. I'm old, married, and have other responsibilities. Plus, not much has come out that has piqued my interest (although, I am looking forward to Humankind).

But, I have begun to take an increased interest in the process of making a video game. Like what does it take to get a piece of software like that released? Do they do unit tests? Is it an agile process or some game specific workflow? How do you organize the code so that it not dictated by the game engine restrictions but how the code functions? Its interesting stuff when you come from a background of making consumer/corporate products.

So, the more I read the more I think, “Hey, I can do this. I always wanted to make my own game.” I do some more reading and find out browsers Augmented Reality support has jumped leaps and bounds from where it was just five years ago. Now, I wanna make Augmented Reality interfaces straight outta Iron Man.

Maybe we can do this with the future releases of ARKit

What tools to Use

Now when it comes to game engines, there are a lot to choose from: you can go with Unreal Engine to Phaser to pygame. It seems like every popluar language has a game engine library made for it. You can find an engine for just about any language out there right now.

Personally, I wanted three main items from the game engine I choose:

  • Since I was making this for the browser, it had to export to wasm
  • It had to be able to export individual models to GLTF (the standard for the web)
  • It had to have an immensive catalog of available assets (meaning an ecosystem with artist/creators)

And the winner is…

I could have picked Babylon.JS or Phaser.JS becuase two definitely compile down to wasm. The problem is that importing art on both of them seems confusing.

I could have picked Unreal Engine because it fulfilled all three requirements, but then I remembered that I don't like working in C++. Its not my favorite language, by far.

“And now we are going to talk about string manipulation in C++”

So that left Unity3d has the only option. Take a look at Unity Assets and you see why it was a simple decision. You can make any type of game you want and still be able to find artwork to match your theme.

The only downside I could see is that the scripting language is C#. I have rarely come into contact with people who use a Microsoft stack in my work life. I have no experience with it.

So it comes as a surprise that I would say C# — the language that powers ASPPages, doesn't have good networking code. Well, it is what it is. There are many stackoverflow questions dealing with how to use network P2P or client/server. There is a builtin cloud solution that Unity tried to make default (because it is paid). There have also sprung up various open source game servers that have a Unity3d Package to facilitate usage in the engine.

I know networking code like the back of my hand, as I am a web developer. Surely C# can just open a socket and listen for responses, right? Yeah, you can but its not as straightforward. Because game engines run at a fixed rate (30frames/per second), making blocking TCP calls are not good. So you have to run the request in a background thread so that the main runtime loop of the engine can keep on going. Ok, so that is similar to Android development where you need to spawn a new thread to open an HTTP connection.

So what if we could pass messages to our game engine another way that was much quicker and could be sent within the normal event loop of the game engine?

Enter Golang

So I decided to use another typed language, Golang, to do the networking. The language itself was designed around concurrency, which helps when talking about simultaneous network connections. Networking is much easier in Golang than C#. Plus there are a wide range of frameworks that allow a developer to quickly spin up a HTTP server.

Holding it down

So this is how I set everything up. You are gonna need to have node and npm installed.

The system will look like this: Golang <–> WebPage <–> Unity3d wasm

The JS on the page with the embedded Unity3d wasm will act as a pass-through, sending messages to the websocket server and sending the responses back to the Unity3d wasm.