• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
KazenoZ

[C#] Is my game structure any good?

4 posts in this topic

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?
0

Share this post


Link to post
Share on other sites
...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.
1

Share this post


Link to post
Share on other sites
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 =)
0

Share this post


Link to post
Share on other sites
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:

[code]
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;
}
}
[/code]

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.

[code]
[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();
}
[/code]

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 [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

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.
2

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0