🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

This string is too big! (VB)

Started by
0 comments, last by -Fruz- 22 years, 5 months ago
I''m creating a multiplayer network game and need to send information on where the players are located and so on, so I came up with this. I can''t use this method because it seemes to use ALOT of system recources! Any ideas? Info: PlayerNr - The player number... - 1 letter SendX and SendY - Send the player coordinates - 5 letters SendBX and SendBY - Send the ball coordinates - 5 letters tmpBv - Is the ball visible or not - 1 letter MyGoals - How many goals the player has - 1 to 2 letters
  Public Function SendData()
Dim tmpBv As Byte

SendX = Right("00000" & Player(PlayerNr).Left, 5)
SendY = Right("00000" & Player(PlayerNr).Top, 5)
SendBX = Right("00000" & PlayerB(PlayerNr).Left, 5)
sendBY = Right("00000" & PlayerB(PlayerNr).Top, 8)

If HaveBall = True Then tmpBv = 1
If HaveBall = False Then tmpBv = 0

dpMsg.WriteLong SendRecData
dpMsg.WriteString PlayerNr & SendX & SendY & SendBX & sendBY & tmpBv & MyGoals

Dp.Send PlayerIDNum, DPID_ALLPLAYERS, DPSEND_DEFAULT, dpMsg
End Function  
JFK
Advertisement
Line by line

quote: Public Function SendData()

Why does this return a Variant? Why is this even a function?

quote: Dim tmpBv As Byte

Why use en entire byte to store a boolean value? (more on this later)

quote:
SendX = Right("00000" & Player(PlayerNr).Left, 5)
SendY = Right("00000" & Player(PlayerNr).Top, 5)
SendBX = Right("00000" & PlayerB(PlayerNr).Left, 5)

Where is SendX, SendY and SendBX declared, are they variants (if not, then why not use Right$?)? Why are you using unicode instead of a plain and simple binary representation. Depending on the range you need, you could have either an integer (2 byte), a long (4 byte) or a 3 byte variable (or even single (2 byte) if necessary)

quote:
sendBY = Right("00000" & PlayerB(PlayerNr).Top, 8)

Why does this one have an 8 while the others only use 5?

quote:
If HaveBall = True Then tmpBv = 1
If HaveBall = False Then tmpBv = 0

There are plenty of better places to store this. If your coordinates are unsigned, then have a positive SendX mean HaveBall = True and a negative SendY mean HaveBall = False. If not, then use the least significant bit of SendX, and then bitshift it 1 place to the right to get the correct value, you lose range, but from the amount of range you would have gained above, it is nothing.

How about making a nicer looking message structure:

Type udtMessage
Data0 As Long
Data1 As Long
Data2 As Long
Data3 As Long
End Type

SendX = Data0, bits 0 to 23
SendY = Data0, bits 24 to 31 and Data1 bits 0 to 15
SendBX = Data1 bits 16 to 31 and Data2 bits 0 to 7
SendBY = Data2 bits 8 to 31
HaveBall = Data3 bit 0

I have purposely left out the player number and my goals. You give no indication of what is being stored in them. Since the IP is also being transmitted, do you really need to send the player number? And even so, does it need to be sent as a unicode string. Each character of unicode uses 2 bytes, and going by the way you have been using it, it is only representing 10 different values.

How many different values can there be for MyGoals? I bet it doesn''t need 4 bytes to send, I bet you could easily do it with just an integer.

Actually, if you don''t feel up to the binary manipulation, I think this would be an even more efficient packet structure:

Type udtMessage
SendX As Long ''4
SendY AS Long ''4
SendBX As Long ''4
SendBY As Long ''4
MyGoals As Long ''4
PlayerNr As Integer ''2
HaveBall As Byte ''1
End Type ''23 bytes, probably 24 though with padding

Compared to:
(4) + 2 + 10 + 10 + 10 + 10 + 1 + 2/4
49 or 51 bytes

I also don''t know what the SendRectData is.

Also, how often are you sending this, and you can probably cut down by only sending the info which has changed, this would require you to have different packet formats but isn''t very hard.

Trying is the first step towards failure.
Trying is the first step towards failure.

This topic is closed to new replies.

Advertisement