PDA

View Full Version : Noob asking a few questions



Diaboli
20-09-2006, 09:09 PM
Hi!

I'm (trying) to make a 2D multiplayer RPG game, and it is kinda working.
you can log in, move around and see other players.
I have a dilemma though:

Should i make movement-management serverside? i will need to have collision-checking serverside, because of some of the future features i am planning.

I have made a timer on the server, that takes care of movement, but heres the problem: it kinda "lags"...

I think the problem is caused by the timer not having enough priority to move each time it is supposed to (interval = 30). how can i make a timer that WILL "tick" (execute its OnMovementTimerTimer function) each time?

Or should i just go back to using the client to make movements?

I am programming in Delphi6, using DelphiX and TServerSocket/TClientSocket

EDIT:
Just remembered:
How do i make DelphiX sort the sprites so that they align the right way from back to front. (a player that is actually standing behind another, might be drawn on top of him and vice versa)

Thanks for any help
Diaboli...

Traveler
21-09-2006, 08:41 AM
I have no experience with server/client programming, but from what I understand is that the client sends everything to the server and the server relays everything to other clients, which are in close proximity to the origional client.

In the event of lag, you'll find that the server isn't able to process all information fast enough. As a result you'll see characters running into walls or some odd direction and, after a few seconds, jump back to their correct location as new information from the client is processed.


As for sorting, I think the best option would be to write your own sorting routine. ie, before drawing, write them to a list, sort them and then draw them.

cairnswm
21-09-2006, 08:46 AM
DelphiX has its own sorting process. I think they refer to it as the Z parameter (not sure havn';t used DelphiX in ages) - I actually copied and reused the DelphiX logic in my own sprite engines.

My multiplayer logic controls the player movements locally and send regular messages to the server on where the player is, which direction they are moving etc. The server then updates its own position for that player and infoms the other clients as well. The server can check that it would be possible for the player to do that change in position so that people cannot hack it.

Diaboli
21-09-2006, 08:48 AM
I found that DelphiX had its own Z-Value... i only set that equal to Y, and it worked... as for the movement stuff, i think i'll make the server send the playerpos to all clients when executing the movement timer... i dont know if this'll fix it, but i think so....

problem is, i dont know how many players i can have before it starts lagging...

my code for the movement timers ontimer is now:


procedure TfMain.MovementTimer2Timer(Sender: TObject);
var
i: Integer;
begin
for i:=0 to Length(Players)-1 do
begin
if Players[i].Online then
begin
if Players[i].dir = 0 then
Players[i].x:= Players[i].x+Players[i].speed
else if Players[i].dir = 2 then
Players[i].x:= Players[i].x-Players[i].speed
else if Players[i].dir = 1 then
Players[i].y:= Players[i].y-Players[i].speed
else if Players[i].dir =3 then
Players[i].y:= Players[i].y+Players[i].speed
end;
end;
for i:=0 to Length(Players)-1 do
begin
if Players[i].Online then
begin
SendPlayerPos(i);
end;
end;
end;

The reason i dont have the SendPlayerPos in the first loop is, obviously, that i need to move all players before sending it...

K4Z
23-09-2006, 07:19 AM
Without breaking into a networking essay...

The main way to reduce lag, and processing, is to send less messages.
i.e: It's not a good idea to be sending messages every 30 milliseconds, this will bog the server down quite a bit, epsecially with lots of players.

A better way would be to just send a single message to the server whenever someone presses a button. Then the server sends that message to every other player, and each computer (or just the server) processes that message.

e.g:
Player A presses and holds the UP button, a single message is sent to the server.
The server relays that message to all other clients.
Each client flags Player A as walking UP, and each tick moves Player A up on their screen.

Player A releases the UP button, a single message is sent to the server
etc, etc.


Another way, mainly to stop hacking, is to have the server process the message aswell and keeps it's own game state. (From the sound of things, you are already doing this). And every 1 seconds or so, the server sends true positions to all clients, syncronizing.

Hope that helps. :P

Diaboli
23-09-2006, 09:15 PM
now why didnt I think of that :P

Thanks for the help... now to organize the code a little better so it can be read later too :P

BTW... how can i catch global keypresses? would be nice, so i dint have to select form...
(i put a edit on the form, and it wont work unless i sopy/paste the keydown/keyup procedures to the tedit's onkeydown/up events...

K4Z
24-09-2006, 03:45 AM
I'm not sure exactly what you mean by global keypresses, but you could try using GetAsyncKeyState().

Don't have Delphi on hand ATM, but I think basic setup is something like:


procedure Timer();
var
key : word;
begin
Key := GetAsyncKeyState(VK_UP);
if Key <> 0 then begin
//Move player up
end;

Key := GetAsyncKeyState(VK_DOWN);
if Key <> 0 then begin
//Move player down
end;

//etc
end;


Just use Delphi's VirtualKeys for each key.