PDA

View Full Version : Need a little help with INDY



Brassawiking
25-02-2006, 04:38 PM
Hi I´m new to the forum but I have hanged around here for some time now, getting some inspiration and help. I´ve recently started with implementing some network code into my game-project with the INDY tcp components.

I´ve managed to get a simple server-client dataflow, but if the client isnt running at the same computer as the server, the dataflow gets to slow and results in making the game sluggish for the client. I realize it is because I´m sending every variable by themself and send around 100 times per second.

The basic setup that I want to work is just sending player cordinates in a 2d game with a decent fast rate.

So my question is if someone could tell my how to write a good network code for a realtime network game with INDY and what i should think about when im writing the network code. I´m using Delphi 7.

I should point out that I´m more or less a beginner at advanced pascal.

Thanks for any help!

AthenaOfDelphi
25-02-2006, 04:52 PM
Hi Brassawiking,

I'm no expert at game related network coms, but I think one of the key things is to keep the amount of data you send small (less than 1024 bytes) so that it fits into one packet.

Rather than send each item seperately, which may result in lots of packets, you could pack the data into a string.



function wordToString(data:word):string;
begin
result:=chr(data div 256)+chr(data mod 256);
end;

function stringToWord(srcData:string;startPos:integer):word ;
begin
if &#40;length&#40;srcData&#41;<startPos+1&#41; then
result&#58;=0
else
result&#58;=ord&#40;srcData&#91;startPos&#93;&#41;*256+ord&#40;srcData&#91;sta rtPos+1&#93;&#41;;
end;



You can encode other values such as integer (although integer is trickier because of sign) in a similar manner. Then you can piece together a packet as a single string...



packet&#58;=wordToString&#40;playerX&#41;+wordToString&#40;playerY &#41;;



At the other end, assuming this is the only data you send...



playerX&#58;=stringToWord&#40;packetData,1&#41;;
playerY&#58;=stringToWord&#40;packetData,3&#41;;



In doing this, you can send a single string. This will reduce the number of packets you send.

I can't help further because as I say, I'm no expert. One place you could look is the Tutorials section.

Brassawiking
25-02-2006, 05:13 PM
Hi Athena!

Thanks for the tip on formating the variables into a string. I will give that a try and see where it ends up :) .

Brassawiking
25-02-2006, 07:08 PM
Well I just jumped in to report that a single packet transport hit the nail right on :D. Thanks for the help Athena. I will make a post later about my game and introduce myself a bit better.

AthenaOfDelphi
25-02-2006, 08:05 PM
Glad it helped. Theres a lot more to creating a good network transport protocol though. Like I said, I'm no expert, most of my serialised data transfers have been done at work using RS-232/RS-485 and a derivative of the HDLC protocol, so its far from optimal for network transfers.

When the rest of the site is back online there are various articles about network programming, so you may want to have a nose through them.

Huehnerschaender
25-02-2006, 09:54 PM
Another trick is to send the data not that often, but let the "client" calculate the new "positions" etc. by the last known values.
This is very useful if the client has to get values from many other mobile units (or players) via network.
When new packets arrive the actual values and positions are updated. There is really no need to send X and Y positions 100 times a second. send it e.g. 20 times and calculate the values between by the last known speed and angle. You can see this in recent games, for example "World of Warcraft". When a player gets a "lag" or disconnect, he seems to run against walls etc. But thats the way to make communication in multiplayer games fast enough for internet.

Greetings,
Dirk