Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Cutting down network traffic from the Client (SendPlayerDir)


Rob Janes
 Share

Recommended Posts

I created a SendData() debugger form for both the client and server, which prints out the header of each packet being sent, and how often it's being sent. The were two major client side packets being sent. 1\. Movement, 2\. SendPlayerDir. When a player walks up against a blocked tile, it does collision testing, SendPlayerDir was being sent a ridicious amount of times, it would litterally send this packet every few miliseconds while doing collision testing against a blocked tile (while the player's keys were down). This is WAY too much network traffic for the same check that could be performed once and send only one packet versus hundreds. To fix this. Add a global variable in modGlobals (client side) called

```
Public Last_Dir as Long
```

In your modGameLogic, sub CanMove

You should notice that your code where it checks DirUp, DirDown, DirLeft, DirRight it will read this:

```

'*********************

'******MOVE DOWN******

'*********************

If DirDown Then

Call SetPlayerDir(MyIndex, DIR_DOWN)

' Check to see if they are trying to go out of bounds

If GetPlayerY(MyIndex) < Map.MaxY Then

If CheckDirection(DIR_DOWN) Then

CanMove = False

Call SendPlayerDir

Exit Function

End If

Else

'************* REST OF CODE HERE

```

What we want to do is remove the Call SendPlayerDir after it does CheckDirection(DIR_DOWN) and rather add the following line to the top just after the SetPlayerDir(MyIndex, X) piece of the code.

If Last_Dir <> GetPlayerDir(MyIndex) Then

Call SendPlayerDir

Last_Dir = GetPlayerDir(MyIndex)

End If

So it should resemble.

```

'*********************

'******MOVE DOWN******

'*********************

If DirDown Then

Call SetPlayerDir(MyIndex, DIR_DOWN)

If Last_Dir <> GetPlayerDir(MyIndex) Then

Call SendPlayerDir

Last_Dir = GetPlayerDir(MyIndex)

End If

' Check to see if they are trying to go out of bounds

If GetPlayerY(MyIndex) < Map.MaxY Then

If CheckDirection(DIR_DOWN) Then

CanMove = False

Exit Function

End If

Else

'******** REST OF CODE HERE

```

What this does it now it only calls the SendPlayerDir if we have changed direction from however we were previously facing, but still sets the CanMoveFlag during the Directional Checking.
Link to comment
Share on other sites

> He means why put the Public variable in modGlobals..I will have to agree on that, why put a new global var that will only be called in one Module. A dim would be a more reasonable type as it's only being called in the PlayerMovement.

You do know dims only last the lifetime of the method they are called in..

I could however understand him making this a private variable declared in this module.
Link to comment
Share on other sites

> Dimming the Last_Dir here would be useless as the CanMove sub is called through ProcessMovement which is called on the GameLoop. You'd need to store it globally else the Last_Dir would reset to 0 each time around.

Exactly my point ![;)](http://www.touchofdeathforums.com/community/public/style_emoticons/<#EMO_DIR#>/wink.png)
Link to comment
Share on other sites

Can someone write if I have to replace?

```

'*********************

'******MOVE DOWN******

'*********************

If DirDown Then

Call SetPlayerDir(MyIndex, DIR_DOWN)

' Check to see if they are trying to go out of bounds

If GetPlayerY(MyIndex) < Map.MaxY Then

If CheckDirection(DIR_DOWN) Then

CanMove = False

Call SendPlayerDir

Exit Function

End If

Else

'************* REST OF CODE HERE

```

to

```

'*********************

'******MOVE DOWN******

'*********************

If DirDown Then

Call SetPlayerDir(MyIndex, DIR_DOWN)

If Last_Dir <> GetPlayerDir(MyIndex) Then

Call SendPlayerDir

Last_Dir = GetPlayerDir(MyIndex)

End If

' Check to see if they are trying to go out of bounds

If GetPlayerY(MyIndex) < Map.MaxY Then

If CheckDirection(DIR_DOWN) Then

CanMove = False

Exit Function

End If

Else

'******** REST OF CODE HERE

```
Link to comment
Share on other sites

  • 3 weeks later...
SendPlayerData is used way too much, you should probably make more packets for sending data. It is a waste of bandwidth and the player jumps around a lot when you do that. I optimized all the packets/variables of my source code of Origins, but I didn't finish upgrading it to what Jscinder did to the source. I moved on by, then. ![:P](http://www.touchofdeathforums.com/community/public/style_emoticons/<#EMO_DIR#>/tongue.png)
Link to comment
Share on other sites

> SendPlayerData is used way too much, you should probably make more packets for sending data. It is a waste of bandwidth and the player jumps around a lot when you do that. I optimized all the packets/variables of my source code of Origins, but I didn't finish upgrading it to what Sekaru did to the source. I moved on by, then. ![:P](http://www.touchofdeathforums.com/community/public/style_emoticons/<#EMO_DIR#>/tongue.png)

Not to mention it's a synchronous, blocking socket to begin with.
Link to comment
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
 Share

×
×
  • Create New...