need help with 2d network game
need help with 2d network game
I've made some 2d PureBasic games before, now Im trying to make one that works across a network.
I created a network server but when I try to use OpenScreen() or OpenWindowedScreen() the program crashes. Any thoughts / suggestions?
I created a network server but when I try to use OpenScreen() or OpenWindowedScreen() the program crashes. Any thoughts / suggestions?
Code: Select all
If InitNetwork()
Else
MessageRequester("Error!","Failed to initialize network!")
EndIf
OpenConsole()
port = 6878
If CreateNetworkServer(0,port)
PrintN("Network Egg server started!")
ExamineIPAddresses()
PrintN("IP: "+IPString(NextIPAddress()))
Else
PrintN("failed to create server! press Enter to terminate")
crap.s = Input()
End
EndIf
CloseConsole()
OpenScreen(800,600,32,"game") ;<<<<<<<<<<< This causes the crash
Repeat
ClearScreen(RGB(0,30,90))
FlipBuffers()
;-Server commands
Nevent = NetworkServerEvent()
Select Nevent
Case #PB_NetworkEvent_Data
ReceiveNetworkData(Connection,buffer,1000)
Case #PB_NetworkEvent_Disconnect
MessageRequester("Game Over","Player 2 give up!")
End
EndSelect
;-Keyboard Controls
ExamineKeyboard()
If KeyboardPushed(#PB_Key_Escape)
End
EndIf
Delay(1)
ForEver
Last edited by treebolt on Thu Feb 21, 2008 5:52 pm, edited 2 times in total.
- Kaeru Gaman
- Addict

- Posts: 4826
- Joined: Sun Mar 19, 2006 1:57 pm
- Location: Germany
I have another question.
when something like this happens
how do I figure out how much memory I need to use for allocating / sending? if I use too much will it slow down the program?
when something like this happens
Code: Select all
*buffer = AllocateMemory(1000)
PokeS(*buffer,stringToBeSent)
SendNetworkData(ClientID,*buffer,1000)
This is where you need to sit down and work out what *needs* to be sent, in as little as possible.
Basically, you need to send as little information as possible - but possibly at 60 frames per second, this quickly mounts up.
Take a multiplayer pacman game for instance. To display everything correctly, you need, per player, their position (x and y as word values - 4 bytes) and the orientation (up down, left and right - 1 byte). So that's a total of 5 bytes, every frame. At a locked 60 frames per second, then you are sending 300 bytes per second. Not a lot, I have to admit, but then that is what ONE person will be sending. Now lets up the scale to 10 people playing at the same time - that's now 3000 bytes of information being sent to the server every second.
Now what about the information of the rest of the opponents? Well, that will be 9 people's worth of information. So, 5 bytes * 9 people is 45 bytes. So every second, you will be receiving 45 x 60 = 2700 bytes per second. So that isn't a lot for what the client will be receiving.
BUT, the server will be doing most of the sending and receiving. We've already established that it's receiving 3000 bytes per second, but it then has to pump out 2700 bytes to each person - thats then 27,000 bytes every second.
So on modern hardware - not a lot. Heck you should be able to (in theory) use a modem for that. But that is a very simple game.
Now if you upgrade that to a shooter, then the math explodes. You need to cater for *every* bullet position, type, power, as well as the player position, instructions (raise shield, powerup, etc). If you worked all that into 100 bytes per frame, then that's 6000 bytes per second to send and 54,000 bytes to receive on the client. On the server, it would need to be able to handle receiving 60,000 bytes per second and to send 540,000 bytes every second.
Obviously at this rate you will be needing heavy duty servers. Or you start to trim back to what you need and let the clients work out what was done as much as possible. You have Ghz of processing power but limited bandwidth - so retreating to math projections would be far more useful when you are going to be sending a stream of bullets all going in the same direction. You just send each bullet as it appears and it's trajectory and then all the clients will then know that the bullet will be moving per frame in a particular speed and angle. This massively reduces traffic.
I hope this helps
Basically, you need to send as little information as possible - but possibly at 60 frames per second, this quickly mounts up.
Take a multiplayer pacman game for instance. To display everything correctly, you need, per player, their position (x and y as word values - 4 bytes) and the orientation (up down, left and right - 1 byte). So that's a total of 5 bytes, every frame. At a locked 60 frames per second, then you are sending 300 bytes per second. Not a lot, I have to admit, but then that is what ONE person will be sending. Now lets up the scale to 10 people playing at the same time - that's now 3000 bytes of information being sent to the server every second.
Now what about the information of the rest of the opponents? Well, that will be 9 people's worth of information. So, 5 bytes * 9 people is 45 bytes. So every second, you will be receiving 45 x 60 = 2700 bytes per second. So that isn't a lot for what the client will be receiving.
BUT, the server will be doing most of the sending and receiving. We've already established that it's receiving 3000 bytes per second, but it then has to pump out 2700 bytes to each person - thats then 27,000 bytes every second.
So on modern hardware - not a lot. Heck you should be able to (in theory) use a modem for that. But that is a very simple game.
Now if you upgrade that to a shooter, then the math explodes. You need to cater for *every* bullet position, type, power, as well as the player position, instructions (raise shield, powerup, etc). If you worked all that into 100 bytes per frame, then that's 6000 bytes per second to send and 54,000 bytes to receive on the client. On the server, it would need to be able to handle receiving 60,000 bytes per second and to send 540,000 bytes every second.
Obviously at this rate you will be needing heavy duty servers. Or you start to trim back to what you need and let the clients work out what was done as much as possible. You have Ghz of processing power but limited bandwidth - so retreating to math projections would be far more useful when you are going to be sending a stream of bullets all going in the same direction. You just send each bullet as it appears and it's trajectory and then all the clients will then know that the bullet will be moving per frame in a particular speed and angle. This massively reduces traffic.
I hope this helps
These tips are great! Very useful for the program I am trying to create! =)
so what you are saying is, one character = 1 byte, so if i wanted to send a string I would use something like...
so what you are saying is, one character = 1 byte, so if i wanted to send a string I would use something like...
Code: Select all
*buffer = AllocateMemory(4)
PokeS(*buffer,"test")
SendNetworkData(ClientID,*buffer,4) ; << 4 letters in the word 'test'
You can use StringByteLength() for this,treebolt wrote:These tips are great! Very useful for the program I am trying to create! =)
so what you are saying is, one character = 1 byte, so if i wanted to send a string I would use something like...
Code: Select all
*buffer = AllocateMemory(4) PokeS(*buffer,"test") SendNetworkData(ClientID,*buffer,4) ; << 4 letters in the word 'test'
and btw. "test" would be 5 bytes as PB strings
are zero-terminated.
Windows 7 & PureBasic 4.4
-
zefiro_flashparty
- User

- Posts: 74
- Joined: Fri Mar 04, 2005 7:46 pm
- Location: argentina
Re: need help with 2d network game
u need add
If InitSprite() = 0
MessageRequester("Error", "Can't open screen & sprite enviroment!", 0)
End
EndIf
If InitKeyboard() = 0
MessageRequester("Error", "Can't open keyboard!", 0)
End
EndIf
to create windows :p and activate keyboard
iff add this u see a blue screen
If InitSprite() = 0
MessageRequester("Error", "Can't open screen & sprite enviroment!", 0)
End
EndIf
If InitKeyboard() = 0
MessageRequester("Error", "Can't open keyboard!", 0)
End
EndIf
to create windows :p and activate keyboard
iff add this u see a blue screen
Amd Vishera fx8350 ,16Gbram, Gtx650 ti, 2gb,Win 10pro. 13tbs. 