In local testing and prototyping, I find that I was creating the same patterns over and over again when building out a simple networking example. Recently, as part of getting SimEthereal ready for a more thorough release, I wanted to build an example application… partially as documentation and partially to use as a test base to fix one or two last niggling issues.
On the way there, this turns out to be a pretty good starter base that any new JME game could probably use as a cut-pasted initial code base. Given that, I’ve purposely left any game specific stuff out of it and given it a special tag in its current form. If it turns out to be useful and needs updates then I’ll create a branch from that tag… but for now the tag should suffice.
Here is where the code lives:
If you build it, you will also (currently) need the SNAPSHOT builds of Lemur and SiO2 because of yet-to-be-released bug fixes in those libraries.
Lemur GIT is here: GitHub - jMonkeyEngine-Contributions/Lemur: Lemur is a jMonkeyEngine-based UI toolkit.
SiO2 GIT is here: GitHub - Simsilica/SiO2: Base pack of useful reusable game code that can help bootstrap any JME game project.
In both cases, checkout the repo, run ‘gradle install’, and you should be good to go.
You can run sim-eth-basic by checking out the repo, going into the sim-eth-basic direcory, and running ‘gradle run’.
Edit: I went ahead and rolled an actual release for this: Release Network base code and example app bundle. · Simsilica/Examples · GitHub
From there you can even download the app and try it: https://github.com/Simsilica/Examples/releases/download/NetworkBase-v1/sim-eth-basic.zip
What You Get
This is an application with a main menu that allows you to either host a game with some particular settings or connect to an existing game.
There is a “Hosting” screen that allows the hoster to join or leave but could be expanded with other hosting setting as the game might need.
There is a login screen that currently just accepts a player name but could be expanded.
There is an in-game menu that pops up once connected and has tab support for adding options, help, whatever. Currently it just allows the player to leave the session or close the game.
All of the menus and screens are wired together with appropriate app states and there are error popups and confirmation dialogs and so on. There is even a status message stream at the bottom of the screen for in-game messages. I’ve even included a DebugHudState that can easily be used to add dynamic debug content… though it’s currently not hooked up to a key toggle.
The basic networking setup code is in place managed by appropriate app states and so on. Hosting a game creates the server using a GameServer class and connecting to a game uses a GameClient class. These can be modified by the real game to add the services as needed. The GameServer uses SiO2’s game service manager class which is like a lightweight app-state manager that is not JME specific (and doesn’t have the 80% of other cruft that app states have that are unnecessary in the back end).
The UI uses Lemur so can be restyled just by using a different default style.
How Would I Use It
Get the sim-eth-basic directory in your favorite way then copy+paste the directory into your own.
Use your favorite IDE to refactor that package name to something that makes sense. Modify the build.gradle file to point to your new mainClassName
Edit the GameConstants class to have appropriate values for your game.
Edit MainMenuState to change the app name or modify the main menu as needed.
At that point you have a running networking app that just doesn’t do anything.
Add your own back-end services to GameServer. For things like game logic, physics, etc. do this with GameSystems added to the GameSystemManager. For custom network hosting things, add those to the SpiderMonkey Server services.
Add your own client-side services to GameClient’s internal Client.
Add your client app states as children to GameSessionState.
What is SiO2
SiO2 is a base utility library that includes some useful JME things and probably deserves its own post. I envision this kind of like Google’s Guava is for the rest of the Java world. These are useful utilities that almost every game will find useful, like:
com.simsilica.sim: GameSystem management code. This can be used to manage game services in a JME-agnostic way. You can either manage your GameSystem updates from an app state or there is a build in GameLoop background thread.
com.simsilica.sate: Useful app states including a CompositeAppState for managing a linked group of app states (like all of the states for a game session). It also has a DebugHudState for easily adding watches for dynamic values in various places on the screen.
com.simsilica.event: a really light weight event bus that can use traditional listeners or reflection delegates.
There are some other Zay-ES specific utility classes also. There is a compile time dependency on Zay-ES but you can exclude it from your games if you don’t use it.
In Conclusion
This package should make it easier to get started making a networked game. It doesn’t do the hard work for you and you still already have to know how to make a networked game… but this is the stuff I found myself repeating over and over again and sometimes not getting it quite right. Having a base will be nice to shorten all of those times by a couple days.
If there is interest in this, I will update it with a new tag when I’ve added the basic game communication (sending login info to the server, letting the client know they are ‘in’, sending player joined messages, etc.)
…and I’ll hide this at the bottom just in case: Patreon