Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 04 Jul 2011
Offline Last Active Oct 27 2014 05:46 PM

Topics I've Started

How to send input data

16 June 2014 - 05:22 PM

I know there are many posts on this topic, but none of them answered the problems I have, or I don't have enough knowledge to comprehend them. I have client/server architecture, with server being authoritative. I implemented fixed timestep "physics" (for now its just moving) on both, client and server. I read about clien side prediction and server reconciliation, and I think I understand these concepts. What I don't get is how should input be handled. I read many posts about de-jitter buffers, packing many input samples into single packet and so on. But I never found a straightforward explanation how input plays with simulation.


I use RakNet, so I have quite high level networking lib that does all the magic for me like packing many similar messages into single packet, it has reliable UDP layer if I need it, I can have it ordered, reliable, unreliable etc. so its not a problem.


What I don't get is - when do I actually sample and send input data? Client ticks simulation at some rate, server ticks at another (but I guess its usually the same tick rate, right?). I use events for input so when key forward is pressed on the client, I receive event and set player state to "moving forward" and on next simulation step I move him forward one time (according to the current state). But when should that information be sent to the server? Should I send input state every simulation step? At the beginning? At the end? At different rate? I tried using different rate (like 20 input updates each second) but then client and server are getting out of sync because client logic sometimes does more steps (for example, pressing forward and releasing it quickly allowed for one simulation step on client, but server never received that input, the same for releasing key after some time - server did less steps than client). 


If I send input every simulation frame and stick some frame id to it I should be able to have it synced but what if input stops arriving at the server (packet loss or something)? Does the server keep with the information it had on previous tick, or it does nothing? I mean, if player holds "FORWARD" do I move player forward on server until message arrives that says key is no longer held? Or rather other way around - I move player only if the server receives input tick that says to move player forward? First case will move player even if he stopped holding forward for the time until late packets arrive (if they do), second will stop player even if it should be moving (because packets didn't reach the server). What solution is most commonly used and why? 


So, to reiterate - I have big problem understanding how simulation ticks on client/server work with client input - most tutorials I read never mention how input is actually sampled and sent to the server, they just mention it is and explain in more detail problems with lag compensation. I'd like to use server reconciliation but I can't until I understand how to mark input, keep it on client and still be able to refer to it when server sends update (that would be tied to some input it received, otherwise I won't be able to confirm it on client-side).

Communication with C++ from script side

25 January 2014 - 04:45 AM



I'm trying to figure out the best way for flexible communication between script side and C++ side. What are you using when you need to call some specific C++ object method from AS script without registering it through script engine? 


I have event system, but it rather works in other direction (C++ sending events to Angelscript) and I'd like to keep it that way (one sided).


Lets consider situation where I have LoginState C++ class that handles logging logic and owns GUI overlay thats scripted in AS. In that script, I gather inputs, validate them and should call C++ method to send username/password through network interface. I don't want to expose network interface to scripting, but I'd like to have Auth(login, password) method available on script side.


Is registering LoginState as a OBJ_REF | OBJ_NOHANDLE type with a method Auth(login, password), then setting global property "LoginState state" the only way? Its a bit problematic with how script interface has to be registered, I'd need to register all possible methods prior to launching the state. 


Or I should invent some event system that works both sides? Where script can do "eventSystem.fire("auth", dict())" where dict has login/password and some method on C++ side listens for such event? I don't like event systems much, I use libRocket events for UI side, so I can launch some events from C++, but I'd rather keep it that way, because it fires events in UI widgets, and I want to keep them script-only so I don't even query them on C++ side. This means I'd need another event system for AS->C++ communication.


Maybe someone uses different methods to call some function with parameters on C++ side from AS?


PS. In case someone wants to discuss it live, I'm on #angelscript irc channel (@freenode) 24/7, my session hangs there but when I'm not afk I look there from time to time.

Network message solution for client/server architecture

16 January 2013 - 04:27 PM



What are most common approaches for solving network messaging issue for client/server architecture (more towards online RPG game than first person shooter, so amount of messages is much bigger because there is a lot more going on in the world)? By message I mean communication between client and server (back and forth).


First method - Message classes


So far I explored Ryzom and Planeshift MMOs sources and they seem to solve this by having a shared code between client and server that has a Message class that is then inherited by every message (ClientLoginMsg, CharacterSelectMsg, GetInventoryMsg). Obviously each message will be used in two ways - the sender will serialize message into bitstream thats sent over the network, and receiver will deserialize it into something usable (class members, data structures etc.). So it looks a bit like this in pseudo-code:


class Message

class LoginAuthorizeMessgae: Message
    string user;
    string pass;
    BitStream out;

    // Assume that BitStream has already MESSAGE_ID read from it, so all thats left is message content
    // Because we read MESSAGE_ID, dispatcher knows which message to instantiate and it can be sent to
    // proper handlers
    LoginAuthorizeMessage(BitStream* in)

    LoginAuthorizeMessage(string username, string password): user(username), pass(password);
       out.write(MSG_AUTHORIZE_ID);  // write message id

// Then, client side:

void SendAuthRequest(login, pass)
    LoginAuthorizeMessage msg(login, pass);

// Server side:

void ReceiveAuthRequestHandler(Message* msg)
    LoginAuthorizeMessage* msg = (LoginAuthorizeMessage*)msg;
    if (db->auth(msg->user, msg->pass)
       // ...


Problem with this approach seems to be insane number of classes that need to be maintained. Just creating a simple response message with one text field requires declaring class with 2 constructors, then its .cpp file etc. Pros of this method is that same messages can be used by both, receiver and sender, so the code can be shared between client and server. All the serialize/deserialize logic is in one place, so its easier to maintain and harder to make some mistake because you see code for both, serialization and deserialization.


I came to even more advanced solution than the one above - I have a factory class that can create an instance of a correct class based on MESSAGE_ID, then I have an event dispatcher that is able to send that exact instance to receivers. A bit of a code snippet to demonstrate:

// =================
// NetworkManager
// =================


NetworkManager::HandlePacket(Packet* packet)
   msgID = GetPacketMsgId(packet);
   // Create a class thats registered for a given msgID
   // - for example for MSG_SAMPLE it will instantiate SampleMessage class and pass
   //   bitstream from packet.data into its constructor

   NetMessage* msg = m_MsgFactory->Create(msgID, packet.data);

   // This call sends that message to methods that are registered to handle it
   // I register methods instead of msgID, so the method is called with exact
   // class that it needs (so SampleMessage, instead of Message). It doesn't 
   // require explicit casting in handler method)


// =================
// SampleMessage
// =================

class SampleMessage: public NetMessage
   int a;
   float b; 
  SampleMessage(RakNet::BitStream& msg): SampleMessage()

   SampleMessage(): NetMessage(NetMessageType::MSG_SAMPLE)
      // This sets MSG_SAMPLE id for this message, so we 
      // dont have to care about it later

// ====================
// SampleMessageHandler
// ====================
bool Init(NetMessageDispatcher& dispatcher)
    dispatcher.Subscribe(this, &SampleMessageHandler::HandleSampleMsg);
    return true;

void HandleSampleMsg(const SampleMessage* msg)
    std::cout << "Received sample message: " << msg.a << "\n";

This works really nicely, but its probably a bit slower than some direct method (that clever event dispatcher that sends exact instance instead of passing Message* which allows me to register methods that receive directly what they want is probably doing some casting that could be avoided, although these are static_casts). Also it doesn't help with a problem of enormous amount of very small classes that may be hard to keep in order. 


Second method - sendXX methods to serialize some data to bitstream, and directly reading from message in receiver


This is method I've seen in some SWG emulator, but it was emulator and I only saw server code, so don't know how well it would work with client and if it was shared in any way or it was separate. In that code, there was a huge MsgLib.h file that defined a lot of methods like:


void sendPlayerPosition(Player*, Client*)
void sendServerTime(double time, Client*)
void sendInventory(Inventory*, Client*)
void sendChatMessage(message, channel, Client*)

and so on. Each of this methods serialized required data into a network message (msg.addUint32, msg.addString, msg.addFloat etc.) and sent it directly using provided Client.


void HandlePlayerPosition(Message* msg)


This means that writing and reading happens in totally different places (server or client) so code is not really shared, all thats shared is opcodes or message ids because both sides need to be aware of them. 



The question

So, the question is, are there other methods? Are methods I described good enough and what can be done better? Am I doing something really wrong here? I find it hard to gather any knowledge on this topic, there seem to be no books on this, and only help were source codes of these three MMOs I mentioned at the beginning.


Thanks for any input! It would be very interesting to see how others solve this.


Game lobby as a separate application

27 December 2012 - 05:10 PM


A lot of online games seem to use separate application for a game lobby where you log into your game account, pick a server and then launch the game. This is also where downloading patches takes place.


I'd like to know, what are arguments for using lobby as a separate app, rather than build it into the game? I mean, it could do the same things, but as a game. This often has a side effect in some games, that forces player to quit the game if he looses connection, because reconnecting is only available through lobby.


Are there some serious things to consider if I'd like to integrate game and lobby so its one process, that can restart itself if executable has to be patched?




Angelscript with Objective-C

02 December 2012 - 03:38 PM


I recently tried to code something on iOS using Xcode and Objective-C. I thought it would be nice if I could hook scripting with AS into my game, but I encounter a lot of problems, most of them come from mixing Obj-C and C++ (Angelscript). I managed to compile AS and even create script engine, but thats as far as I can go with my limited knowledge.

For example how this should be handled in Obj-C class that's some kind of ScriptManager (creating engine, handling pool and message callback):
[source lang="cpp"] r = engine->SetMessageCallback(asMETHOD(CScriptMgr, MessageCallback), this, asCALL_THISCALL); assert( r >= 0 );[/source]
I have no idea how to pass Obj-C method to asMETHOD() macro. Can I even do this? If not, how should I "bridge" such things with AS C++ API?

Its even worse when it comes to registering Obj-C classes (NSString, GLKVector etc.) - I have no idea where to start.

Did anyone make a successful implementation of AS on iOS and has any example that would let me start with this? Because I couldn't find a clue regarding working with AS and Objective-C.