Jump to content

  • Log In with Google      Sign In   
  • Create Account


[C#] Is my game structure any good?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 KazenoZ   Members   -  Reputation: 152

Like
0Likes
Like

Posted 29 February 2012 - 11:20 AM

Hello,

Got a more theoratical based question this time;

At work, I've got a lot of free time, and nothing to do, as well as a VS Proffesional install, so I dabble from time to time with various things, and learn alot.
Last week I started work on a simple card game called Duraq(I'm not sure if that's how it's called in English, but that's besides the point), this is the first time I'm working with networking, so I chose a rather simple project, and I'm quite far along by now. But I have doubts about my structure of my code, since this is an area I've never touched before.

Unfortunately, this project is at work, so I can't show any bits of codes, and not even get the program out of there, due to security risks and whatnot, so I'll do my best to explain what I did without any code examples.


This is made with C# WinForms on VS 2005 Proffesional(I have no way to acuire newer versions or any other compiler, nor external libraries for that matter, due to said security risks).


tl;dr - I'm making a card based online game at work, have no code examples to show here, and have doubts I would like to get opinions on regarding the program structure.




Now, getting to it;
With this game, I decided to take an approach that would let me incorporate various other card games on this same engine later, should I even want to, by completely seperating the server/client project from the game logic.

The Solution current consists of 2 projects, the server/client executable, and the game logic as a dll.
The dll consists of generic structures that are common to pretty much any card game such as the Deck, Discards, Playing Field, Player, Hand and Card, each have generic methods in them that use the same logic(Draw() for deck, PlayCard() for hand, AddPlayer() for players).
The point is to call methods like the AddPlayer() from the dll from the server/client project, so there's actually a connection between the networking program to the game it'll display, but still keeping it vague, so that all logic specific to the game stays inside the dll, and can be replaced later.

The dll also has a method that creates a UserControl that it returns to the client to show the player, so that isn't done in the client program either.



So far so good, I think, shouldn't be anything wrong with this design, right?
My problems start at this point:
First, the way the dll is constructed, atop all the structures I mentioned before, I have a single superclass(We'll refer to it as DuraqGame from here on out), what it does is contain all of the info of the game in the dll in one place and manage all the game logic from within it, meaning that the connection with the server/client program is done solely through this one class.
The problem is, this class is static. I just found it to be easier to deal with in this manner, so I don't have to worry about passing instances, since I'll only be using 1 instance of this class anyway.
What would you guys say about that? This is also the first time I've been working with a static class of the kind, so I'm not sure of the regular practices in the area, but it does work very well so far and gives me much less of a headache.


The other issue is with synching the clients with the server.
Obviously, I can't send whole objects through the TCP/IP networking in .NET, but both the server and the client rely on the same DuraqGame superclass to get the representation of the game(The UserControl I mentioned earlier) printed on the screen. What is the standard way to sync your server data with your clients?
What I did was make a couple methods in the DuraqGame class that compress all the data to string, and decompress it back into the values in the class.
That way, I get the server to send an update to all the clients as a string, then each clients parses that string into its' own copy of the DuraqGame class, from which it can then request the UserControl to display on its' local screen.




So, that should be it, I'm sorry if I'm a little unclear about some stuff, I'm kind of a learn-by-doing sort of person, and not very booksmart, so I might've gotten some terminology wrong at places =\
Still, I would like all the feedback I can get about both issues. Gotta learn what you can, right?

Sponsor:

#2 slayemin   Members   -  Reputation: 2441

Like
1Likes
Like

Posted 29 February 2012 - 09:46 PM

...so, if you can't take the project from work due to the nature of security, are you ever planning on deploying your game or is it just a learning exercise? What's the point of building a reusable code library if you can't really reuse it on other non-work related projects?

As far as code structure for your project goes, its similar to what I'm doing in my projects so it sounds like you're on the right track.

Static keywords are important for classes which are accessed by multiple threads (ie, your network connection threads). Just be careful about how crazy you get with them. Treat them like global variables. Also, if multiple threads are accessing the same shared object at the same time, you'll have to worry about serializing their access via "locks" (lock keyword, spin locks, mutexes, semaphores, etc).

You can manually serialize your game objects into string and byte arrays, as you're currently doing. It works and its light weight, but it also puts a significant amount of overhead in your development time and effort for every object you want to send over the network. Since you're using .NET, you should look into the [Serializable] parameter. You can use a built-in serializer to convert your object into a serial stream and then either write it to disk or a network stream. Then, when you read in the serialized data, you deserialize it into an object, get its type, and then cast that object into your game object and everything is done for you. This helps you avoid having to write customized parser code. However, this comes at a bit of cost: The serialized data is often quite bloated compared to what you could get with customized data serialization. If you aren't too concerned with bandwidth/diskspace, this isn't an issue.

Eric Nevala

Indie Developer | Dev blog


#3 KazenoZ   Members   -  Reputation: 152

Like
0Likes
Like

Posted 29 February 2012 - 11:48 PM

Well, basically, yea, it's mostly a learning exercise, but I AM going to deploy it on the local network at work, so Iunno, it's always good to think ahead. But generally, yes, it's mostly for just learning. This isn't work related by the way, my job has nothing to do with programming =\

Right, multithreaded concepts, another thing I'll look into next then =)


So, basically, you're saying that if I can make a custom parser that's not too difficult, then that would be the better way to go anyway?
Well, I'm very intrigued by the serializable option you mentioned, I'm gonna look into it anyway, since this IS a learning project. But for future reference, a custom parser is better(Since I've become quite experienced in writing those anyway)?


Also, thanks alot for your time, I know it was a long post to read, but you've been very helpful =)

#4 slayemin   Members   -  Reputation: 2441

Like
2Likes
Like

Posted 01 March 2012 - 03:39 PM

The only reason a customized serializer would be better is because you know the details of the object your serializing and you can control the order of how you serialize the object. This allows you to serialize the object into a very small byte array. Example:

class Knight
{
	static uint NextID;
	uint UniqueID;

	int Health;
	int Attack;
	int Speed;
	Vector2 Position;

	public Knight()
	{
		  UniqueID = ++NextID;
		  Health = 10;
		  Attack = 5;
		  Speed = 4;
		  Position = new Vector2(5,5);
	}

	 public byte[] Serialize()
	 {
		  byte[] ret = new byte[24];			   //you know this size because you know the number of variables and their byte sizes
		  ret += UniqueID.ToByteArray();	 //not syntactically correct, just for illustration purposes
		  ret += Health.ToByteArray();
		  ret += Attack.ToByteArray();
		  ret += Speed.ToByteArray();
		  ret += Position.X.ToByteArray();
		  ret += Position.Y.ToByteArray();
		  return ret;
	 }

	 public Knight Deserialize(byte[] data)
	 {
		  Knight k = new Knight();						 //probably should have made an empty constructor now that I think about it...

		  k.UniqueID = uint.parse(data[0], 4);		  //not syntactically correct, but reads the first four bytes as an unsigned int
		  k.Health = int.parse(data[4],4);					//next 4 bytes are the health
		  k.Attack = int.parse(data[8],4);
		  k.Speed = int.parse(data[12],4);
		  k.Position = new Vector2(int.parse(data[16],4), int.parse(data[20],4));

		  return k;
	 }
}

The size of my example Knight class is 24 bytes. If I use the [Serializeable] attribute for the class and use the built in method, the serialized data size is ~150bytes.

[Serializable()]
class Knight
{
	static uint NextID;
	uint UniqueID;

	int Health;
	int Attack;
	int Speed;
	Vector2 Position;

}

void main void()
{
	 Knight k = new Knight();		  //just use the default values assigned by constructor

	 BinaryFormatter serializer = new BinaryFormatter();
	 FileStream fs = File.Create("test.txt");
	 serializer.Serialize(fs, k);		  //writes the serialized data of the knight to the file stream
	 fs.Close();

	 //now, the knight is stored as binary data in a file, ready to be deserialized...
	 fs = File.Open("text.txt", FileMode.Open);
	 while(fs.Position < fs.Length)
	 {
		  object o = serializer.Deserialize(fs);
		  Type t = o.GetType();
		  switch(t.Name)
		  {
			   case "Knight":
			   {
					Knight asdf = (Knight)o;		  //just casting the object into a knight object
					break;
			   }
			   //add more case statements for each object you've got serialized
		  }
	 }
	 fs.close();
}

Now, the built in serializer works pretty damn well. If you have object inheritance, it will serialize the inherited data automagically. If you decide to add more member variables to your class, it will automatically include that in the new serial stream (but, watch out for attempting to deserialize an outdated binary). At the cost of a few extra bytes, you as a programmer don't have to worry about maintaining the serialization and deserialization of your objects.

However, if you do want that extra amount of granular control over your serialized data size, you can do the serialization/deserialization manually. 24 bytes vs 150 bytes can make a big difference when you're sending 1000 objects through a network, sixty times a second. If your object gets more member variables, you need to update your serialization and deserialization methods and then test it to make sure it works as planned. The first few times, it may not annoy you but after doing it a lot, it'll annoy and wear you down.

I won't say which method is better because each has their pros and cons. I leave it up to you to decide which method you'd like to use based on the situation your in Posted Image

Edit: The reason the built in serializer creates bigger binary data is because it has to include meta-data about your class. It uses this meta data to deserialize your class. When you manually serialize/deserialize your classes, the meta data is already baked into your deserializer so you can get away with smaller binary data sizes.

Eric Nevala

Indie Developer | Dev blog


#5 KazenoZ   Members   -  Reputation: 152

Like
0Likes
Like

Posted 02 March 2012 - 08:50 AM

Thank you very much for all the help, I'll look into this more later =)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS