TGR Pong with Mirror Networking Update!

A quick update on The Game Realm Pong with Unity and Mirror Networking.

Well its been more than a few weeks, but I finally got some real progress done with my pong game using Mirror Networking. I finished the Tic Tac Toe game using Mirror and used that as a base. There were still quite a few changes I had to make in order for the game to be more efficient, like combining everything into a NetPlayer game object. Creating the Ball game object, tweaking the UI.

TGR Pong in Unity

There were quite a few hurdles that I had to go to Mirror Networking for help with on Discord. Mainly just to understand the API. Things like passing messages between clients, client authority, syncing movement between clients, instantiating network objects, etc.

TGR Relay Server In Unity

The main issue I was having was running everything through a relay server. I guess its not very common or people just aren’t sharing their wisdom. The reason for the relay server is to get away from router blocking with player 2 player connections and alleviate the Android/ Iphone hosting games issues. Of course using a relay server will increase the latency and eventually we will see how the pong game does when I run the clients through the relay server app on a VPS server.

So here is a video of the exciting game in action. It shows how well the objects are sync’d across both clients. This is where I failed when using LiteNetLib Networking. I wasn’t able to write a good algorithm that kept the objects sync’d like this. Thats when I knew I need to go to Mirror, they gave a pong example!

As of right now, the TGR Pong game is playable with 2 clients and keeps score. So all I have left is to enable player statistics, logging in, and make sure singleplayer is still working.

I will hopefully get this completed this Memorial Weekend! Speaking of, thank you to all the service men and woman and your families for the sacrifices and things you do for us! We love you guys and gals!!!!!

Memorial Day Thank You

Networking Rework Again!

Mirror Networking

Well, here I go again.
I finished moving my multiplayer TicTacToe to the new networking system using litenetlib and it worked beautifully! I had to do a complete rewrite on the relay server also. In the end, everything worked great and the world was happy!

I then excitedly decided to work on the multiplayer pong game. The whole reason for the TicTacToe game was that it is one of the easiest games to write, its turn based and perfect to use for my first multiplayer game. So when I finished that, I figured I need to work on something that had some live action, synchronized objects, player movement, etc. Thats where pong was a perfect choice.

I got all the way to the end where I was trying to sync the player paddles and the ball. I spent a few weeks trying different things but ultimately in the end, I decided to look at other Networking solutions, preferably something that already had syncing of variables under the hood. With LiteNetLib, you have to write your own syncing algorythms, and that is definitely not something I could pull off. I got pretty close a few times, but something would always cause it to get out of control.

Photon Pun2
The first thing I took a look at was PUN2, or Photon for Unity Networking, version 2. Due to the type of architecture I wanted, I needed to create a seperate server that manages the clients, not so much the game, but the clients themselves, somewhat of a matchmaker with more control. Photon Server seemed to fit the bill perfectly, at first anyways. After diving into it, I came to realize real quick the lack of documentation. Without this, its damn near an impossible task. Getting PUN2 working with the server was pretty easy, but in order to get the server to do what I wanted, was requiring too much research, forum posting and begging for information. I finally decided to go with Mirror.

The downside of Mirror is pretty close to the same as Photon, lack of documentation. Very abstract or high level examples that are tough to understand and not too much information available, at least for what I was looking for. I finally decided to sponsor it and get access to premium help, this was well worth it. In the end, after a few weeks, I got going in a good direction and should hopefully be able to get the Multiplayer TicTacToe rewritten using Mirror soon. A few good things about Mirror is that it was based on the original Unity Multiplayer UNET, its open source for those that get off on that sort of thing, tons of examples (If you can understand them, not for the basic type like me) and once you get a grasp of it, it gets easier to understand and implement.

I just wanted to give a quick update, document my progress and take a break from spewing code, lol.

Ok, back to it! See ya on the next one!

Multiplayer Tic Tac Toe

Well it seems I never did a post on my Tic Tac Toe game so now would appear to be as good as time as any to do a little write up about it, explain the reason behind it and some of the things I ran into while creating Multiplayer Tic Tac Toe!

TGR Multiplayer Tic Tac Toe

I originally picked Tic Tac Toe as my base game as I dove head first into multiplayer coding. Its just about as simple as a game can get and wouldn’t require too much game code as opposed to the multiplayer code required to get things working across networks and devices. During this time I spent months researching all the different architectures, methods and solutions people were using out there from third party addons like Photon, Node.JS, Mirror, Litenet, etc to the integrated Unity UNet solution. I did as much research on each of them as any sane person could tolerate, looked at examples, tested demos and attempted to write basic chat programs with some. In the end I just chose to use Unitys UNet. It was easy to understand and easy to implement. Of course this was after a lot of trial and error.

The main basis for multiplayer is that you have multiple devices sending and receiving messages to each other. The base for my network was how these devices would interpret the messages and act accordingly. One of the first hurdles I ran into was while looking into peer to peer networking. There are quite a lot of issues with this structure which can grow depending on which devices your setting up the network on. With mobile phones, based on what I remember (This was over a year ago (I think)), mobile phones have a tough time hosting games while computers have a hard time punching through the router to host games and allow people outside the network to join. While some of the available network solutions had answers for these kinds of situations, I opted for a simpler remedy, its a called a relay server.

Most big multiplayer networks like Steam for example, will push players onto their relay servers when they can’t get around router issues, from what I read anyways. This was the perfect answer for my situation, especially since I was looking at creating simple turn based card or board games. The downside to this, for me anyways, is that it required a hosted server that I could use to run the relay server on, outside of my home network to make it easier for people to play against each other.

Ultimately I succeeded in writing my relay server that could handle people hosting tic tac toe games or joining tic tac toe games. I ran into a few issues with allowing people to log in in order to save stats, a logged in player playing against a non logged in player, registering players and quite a few other things that needed ironing out. In the end, I was very happy with the overall structure of my multiplayer network.

A bit after I finished the Multiplayer Tic Tac Toe game and my Relay Server, quite a bit after actually, unity decided to trash the UNet and make it obsolete. I said of course they did, thats just because I decided to use it! They are promising a new type of integrated networking system but after they trashed what they already had which I used, why would I use another network system they create? What if they decided to trash that one in the end too?

Here I was again, looking over all the different networking systems I could use. This meant having to port my existing Relay Server and Multiplayer Tic Tac Toe game over to the new library. In the end I decided to go with LiteNetLib due to how light weight it is, how easy it is to understand and how quick it was to implement. Of course my completion of the port is still a work in progress, but once I can get back to being focused on it, I will update this post!

Thanks for reading and if you have any questions, please leave a comment below!

Edit 3/28/20:
Well, its been awhile but after dabbling with mirror a bit and deciding not to use it, I completed the integration of LiteNetLib. So now my networking system is complete using LiteNetLib. I also changed the login sytem so that a user can just use a username, email and password to save their stats, no more logging in or registering to the website.

Edit 5/10/20:
Lol, I completed TicTacToe using LiteNetLib and moved on to Pong. Things didn’t go too good when I tried to get the objects sync’d between clients. I went back to Mirror since they have a Pong example and you can use SyncVar to keep things sync’d!

The Hearts Card Game Finished!

Its been quite a while but after taking a little break from working on my role playing game, I decided to work on my Hearts Card game. I mainly made this for my wifey who loves to play the game. I originally made this game in Java and Monkey X which helped me figure out how to create some of the methods required to complete the game in Unity.

Hearts Card Game 1

The main thing that took me so long was figuring out how to use Unity Game Objects for everything in the game. From the cards, the deck to the players, they are all represented in the game as game objects which allows for some really nice object manipulation and keeping track of whats going on in the game. I wrote a few scripts to help manage the objects in game like a card script for assigning and tracking specific card variables and positions. I also wrote a deck script for keeping track of the status of the deck, handles the shuffling and the dealing of the cards. The player script keeps track of each player, their specific variables like scores and names along with positions and avatars. The last script I wrote was the main script that handles the actual game play and puts all the individual game objects to work along with keeping track of them and their states.

Hearts Card Game 2

I originally used a deck of cards I found online when I originally wrote the Java and MonkeyX version but after using it for my Unity version, I noticed how low of a resolution they were so I had to get a new deck. I found a public domain deck at Byron Knolls Blog. I was able to use this deck as a base and do a few customization’s to it. The cards are a higher resolution and look pretty good. I just wanted to add an inner rectangle on each card and make a custom card back with The Game Realm Logo.

One of the main issues I had was getting the game to work properly in a web browser using the Unity WebGL build. The game wouldn’t scale properly no matter what and if the user changed the browser window size, the game wouldn’t scale with the new window size. I finally got the settings correct inside the Unity Editor using screen overlay and scale with screen size on the canvas. I then had to use the A Better Template unity package from Greggman which allowed the game to scale properly with each window size. The problem was though that if the user resized the window after starting the game, the scale would be off. I fixed this by forcing a page reload if the browser window gets resized.

Basically, overall, it took me about a month and a half to get the game completed. Get some bugs worked out and get it on the website. I will be looking at adding some multiplayer after a bit. I will be working on a few more single player games first, like The Dice Game which is next in line.

I appreciate you taking a look at the game and reading the post! If you have any questions, please feel free to ask or comment below!

Thank you!