Captive Imagination
July 31, 2010, 11:51:53 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: jSeamless 1.0 Beta 7 is Available http://www.jseamless.org
 
   Home   Help Search Calendar Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: StandardGame and multiple GameStates  (Read 1057 times)
0 Members and 1 Guest are viewing this topic.
Wasserleiche
Newbie
*
Offline Offline

Posts: 13


View Profile
« on: January 01, 2010, 12:30:38 PM »

Hi everyone!

I am creating a game for over a year now (with some big breaks) and now I want to add networking to it. I "studied" the Flagrush-Multiplayer tutorial but I have no idea how to apply this whole thing to my game. The thing is: Flagrush uses BaseGame with no GameStates so far, I am using StandardGame with one rendering-GameState and multiple GUI-GameStates (GUI should not be important here *g*). Somewhere I read something about a Network-GameState where nothing is rendered, but how does such a GameState look like? And where do I have to implement SyncObjectManager?
My next problem is: I realise that I have to create and remove objects in an implementation of SyncObjectManager, but what aspects of those objects do I have to apply there (like translation, rotation and scale)? And how do these values correspond to those that are currently in the game?
As you see, I didn't understand well how SyncObjectManager works or what it does. Maybe someone could teach me or give me a hint where to look at Smiley
Hope you can help me getting started Wink
Logged
Creativ
Newbie
*
Offline Offline

Posts: 3


View Profile
« Reply #1 on: January 02, 2010, 09:02:02 AM »

Hi,

I'm also still new to JGN and i use StandardGame + different GameStates for some other stuff, that isn't really important for networking.
This Thread really helped me:
http://forum.captiveimagination.com/index.php/topic,661.0.html

There is also a small example with JGN + StandardGame. Maybe this will help you.

And to your second Problem: In the SyncObjectManager you pretty much just have to create the Object and return it. So e.g. you could create a Box, add it to the Scene and just return it.
The Synchronization is done the JMEGraphicalController. If you look at it, you will see two methods: applySynchronizationMessage and createSynchronizationMessage. In the basic JMEGraphicalController it will only apply the Location and the Rotation of the Object, but you can also add some other stuff that should be synchronized.

But since i'm also new to JGN this could be wrong Grin

I hope it helps
Logged
Tumaini
JGN Developer
Jr. Member
*****
Offline Offline

Posts: 51


View Profile
« Reply #2 on: January 02, 2010, 11:45:00 AM »

Hello and welcome to JGN!
I'm using JGN with StandardGame myself and I'm using one GameState on each end for network.
In that GameState you don't need to render anything (so you override the render method from GameState with an empty one).
My network GameState implements JGNConnectionListener (for listening for connects and disconnects) and MessageListener (for receiving messages from the clients).

SyncObjectManager is used by SynchronizationManager to handle creation and removal of sync'ed objects.
You add your SyncObjectManager to the SynchronizationManager like this:

Code:
ObjectGraphicalController controller = new ObjectGraphicalController();
serverSyncManager = new SynchronizationManager(server, controller);
serverSyncManager.addSyncObjectManager(new SyncObjectManager() {
public Object create(SynchronizeCreateMessage scm) {
if(scm instanceof ObjectOneSyncCreateMessage) {
return new ObjectOne();
}
                                else if(scm instanceof ObjectTwoSyncCreateMessage) {
return new ObjectTwo();
}
return null;
}

public boolean remove(SynchronizeRemoveMessage srm, Object object) {
if(object instanceof ObjectOne) {
object.removeFromParent();
}
else if(object instanceof ObjectTwo) {
                                        object.cleanUp();
object.removeFromParent();
}
return false;
}
});
JGN.createThread(server, serverSyncManager).start();

In the create method the object should be created and returned to the manager, so it can be wrapped in a SyncWrapper and updated from within the SynchronizationManager.

I've written a basic tutorial on sync on the wiki that you can take a look at: http://code.google.com/p/jgn/wiki/SimpleSynchronization
Logged
Wasserleiche
Newbie
*
Offline Offline

Posts: 13


View Profile
« Reply #3 on: January 03, 2010, 11:01:45 AM »

Thx for your responses!
But there remain some questions:
1)
In my game more than one object-type has to be synchronized. How is this done by those SyncObjectManagers? Tumaini postet a piece of code where the SynchronizeCreateMessage determines what object-type has to be created/removed. But how is this managed? How creates those SynchronizeCreateMessage?
2)
I don't start my game and am immedietly in the game itself, but I first start a menu where I can choose wether I create a new game (and be a server myself) or I join a game (so I am only a client connected to a server). How do I realise this choise?
Logged
Tumaini
JGN Developer
Jr. Member
*****
Offline Offline

Posts: 51


View Profile
« Reply #4 on: January 03, 2010, 02:08:32 PM »

1) If you look closer at the code I posted, you can see that in the create method of SyncObjectManager it checks whether the SynchronizeCreateMessage is an instance of ObjectOneSyncCreateMessage or ObjectTwoSyncCreateMessage.
You'll have to extend SynchronizeCreateMessage to fit your own needs. You don't have to necessarily make a new message for each synced object type, but this is one way to do it.

When you register an object authorative from one side (client or server) you use:
Code:
serverSyncManager.register(object, new SynchronizeCreateMessage(), 50);
where you specify the object to be synced to all clients (if on server) or to all other clients and the server (if on a client), then the SynchronizeCreateMessage or your own extension of it and finally the update rate, in milliseconds.

2) Well if you don't connect to the server immediately, there's no problem.
I've found it's usually best to start the sync manager thread close to the start of the server/connection to the server, but it could work anyway. If you just have a menu I don't see a reason for you to connect to the server that handles sync at that time.
You could always connect to another server though, if you e.g. want to check online status or something.
Logged
Wasserleiche
Newbie
*
Offline Offline

Posts: 13


View Profile
« Reply #5 on: January 11, 2010, 12:12:43 PM »

@Tumaini:
Thanks for your answer, but I still don't know how to extend those SyncCreate-/SyncRemoveMessages the right way. I would like to pass some information with those messages, like the path of the model I use or something like that. But I guess its not just extending the class and adding some fields with getters to it, right? Those messages will be send through a network, so somehow they will be stored bytewise (or whatever) so they can be transmitted. Since I don't think that the whole object itself will be transmitted and rebuild at the other side so those fields I created could be reused, I would like to know how to transmit more information with those messages. Is it even possible? Or do I really have to create an extra class for each model I use? That would be awfull since I would like to have my game easy extendable with content without having to modify the code each time.
Logged
Tumaini
JGN Developer
Jr. Member
*****
Offline Offline

Posts: 51


View Profile
« Reply #6 on: January 11, 2010, 05:22:30 PM »

Actually, you just extend them like you do with other classes.
Some things you need to remember:

*Each message needs a "no-args-constructor", an empty constructor with no arguments, for reflection used by JGN.
*Each field needs a getter (possibly a setter as well)
*You need to register each message with JGN (JGN.register(MyMessage.class))
If you forget one of these you might receive various errors, so be careful to remember them.

The message you receive at the other end is the same as the one you sent, you can call the same methods and get the same results.
Although getPlayerId() in Message only returns a player id on the server.

Example:
Code:
import com.captiveimagination.jgn.synchronization.message.SynchronizeCreateMessage;

public class MySyncCreateMessage extends SynchronizeCreateMessage{
private int objectId;
private String modelPath;

public MySyncCreateMessage() {
//No-args constructor for reflection
}

public MySyncCreateMessage(int objectId, String modelPath) {
this.objectId = objectId;
this.modelPath = modelPath;
}

public void setObjectId(int num) {
this.objectId = num;
}

public int getObjectId() {
return objectId;
}

public void setModelPath(String str) {
this.modelPath = str;
}

public String getModelPath() {
return modelPath;
}
}

Here you have two extra fields that you can set and get.
Set them on one end, send it over and get it on the other end. As simple as that! Smiley
Logged
Wasserleiche
Newbie
*
Offline Offline

Posts: 13


View Profile
« Reply #7 on: January 12, 2010, 05:34:45 AM »

Thanks! Someone should write this down into a tutorial because I never would have found this out by myself. I don't know how you are supposed to learn how to use jgn, but certanly not by studying the whole code itself Wink
Logged
Tumaini
JGN Developer
Jr. Member
*****
Offline Offline

Posts: 51


View Profile
« Reply #8 on: January 12, 2010, 10:46:52 AM »

Yes, the wiki is a little sparse, but I've added two pages recently and I hope some other people who use JGN might want to add some info there as well.
I added this to the wiki tutorial on sync.
http://code.google.com/p/jgn/wiki/SimpleSynchronization?ts=1263314789&updated=SimpleSynchronization
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.214 seconds with 21 queries.