Saving Player Information

Started by
4 comments, last by Codemaster Jamal 6 years ago

Hello, my name is Jamal and I've been interested in programming video games for a long time. I've attempted to make games in the past however, they never really got far or at least close enough to them being released. This is mainly because I was biting off more than I could chew. I know that before I ask this question, many people are going to discourage me from making an MMO. Please save those comments because I know exactly how hard it is to make an MMO. Which is why I am starting off small and then working towards making my game greater.

 

Anyhow, I plan on releasing a 2D Java game that is going to be hosted on my websites server. I wanted to make the game multiplayer(obviously) but I also wanted to make a system in which the player would log-in and then be able to access their characters in their account and play the story. I'm recently learning about sockets however, I was wondering if anyone had a method or even the slightest idea of how to save a player's information so that it could be loaded back into the game?

I already have an idea where whenever the player logs in, you create a new initiate of a Player Object and then pass certain variables through. For example, the player logs in from one GameState and then goes into another GameState in which the Player() is called. When the Player() is called, it passes these values and these values tell the game where the player needs to be, etc.


public class Player
{
	String name;
	String race;
	int attack;
	int defense;
	
	public Player(String name, String race, int attack, int defense)
	{
		this.name = name;
		this.race = race;
		this.attack = attack;
		this.defense = defense;

		//etc
	}
}

I could always store these values into a SQL database so that way whenever the player logs in, I can just pull these values from there however, I was wondering if there was a better method for saving the player's data. Any thoughts?

Advertisement

There are several ways to go about this.

The simplest way is to use whatever serialization framework you want (java.io.Serializable) to write to a memory stream, and then dump that memory stream to some persistent storage -- file system, or key/value store, or BLOB column in a database. This is simple to implement, may work well with versioning if your serialization system is good, but makes it very hard to query your data directly in the database. Databases don't know how to de-serialize for a WHERE clause, if you're trying to answer questions like "how many characters are created but haven't gained any experience points" or whatever. Systems that serialize the character to JSON or XML instead of Serializable byte streams are more or less in the same class.

The slightly more involved way is to use an object/relational mapper. Hibernate is the most common one on Java systems, I think. This makes it somewhat easy to build objects that fit together (inventory is a number of Items that all belong to a Character; Characters are played by Players, ...) ORMs also generally solve schema migration for you -- when you add a new property, what do you need to do in the database? The main drawback of ORMs is that objects and relational databases really have different shapes, and performance will be pretty slow. There will also be times when you run into something that "should be easy" but actually can't be done at all.

The most low-level option is to write your own database management. Define the database schema using relational thinking, and only after that, think what kinds of objects or other data structures you'll want to use in the program that emerge from the data model you've defined. This means finding a SQL interface (JDBC is common in Java) and writing your own code to get objects-from-database and database-from-object-changes. This gives you the most "power" and the most ability to make your data model great, assuming that you are well versed in this kind of systems design. The draw-back is that the code to interface with the database is tedious and error-prone to write.

Given that you're on Java, you probably will want to try Hibernate, because it's the Java thing to do.

 

enum Bool { True, False, FileNotFound };

How depending on the requirements of your game and how many players you are thinking of supporting at a time. @hplus0603 already gave some pretty good choices.

I am getting a feeling that this might be your first networked game, and by multiplayer, it really is just you and your friends, not the entire Internet. I'd say just pick one, just for the learning experience. You don't know which method is the best for your game. We can tell you the best solution is probably using SQL tables with replications so you can scale your server horizontally, and query individual fields for analytics purposes. You can also optionally go the Key-Value NoSQL route and save them as JSON. But, this is probably too large of a requirement for your game, and also by yourself to support. Not to mention the monetary cost of such architecture.

If I were you, I'd just save them into a human-readable format onto disk (a simple `FileOutputStream`), and read it back when you load the game.

Let me help you make it. I've played around a lot using jMonkeyEngine.

Java Game Development, jMonkeyEngine Developer, Discord: Nebulousram#0636

 

I'll be posting a response in depth about my project later on. Thank you for all of those who replied.

This topic is closed to new replies.

Advertisement