need help with 2d network game

Advanced game related topics
User avatar
treebolt
User
User
Posts: 50
Joined: Thu Feb 21, 2008 4:38 pm
Location: Palm Harbor, FL

need help with 2d network game

Post by treebolt »

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?
Foz
Addict
Addict
Posts: 1359
Joined: Tue Nov 13, 2007 12:42 pm
Location: Manchester, UK

Post by Foz »

Post up your example of what you're doing that causes a crash.
User avatar
treebolt
User
User
Posts: 50
Joined: Thu Feb 21, 2008 4:38 pm
Location: Palm Harbor, FL

Post by treebolt »

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.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

InitSprite() is missing.
but causing a crash? phew...
oh... and have a nice day.
Foz
Addict
Addict
Posts: 1359
Joined: Tue Nov 13, 2007 12:42 pm
Location: Manchester, UK

Post by Foz »

Why on earth are you combining a console app and a window app in the same app?
User avatar
treebolt
User
User
Posts: 50
Joined: Thu Feb 21, 2008 4:38 pm
Location: Palm Harbor, FL

Post by treebolt »

ok thanks I did forget the initsprite and initkeyboard.... thanks and sorry for the wasteful thread

It was causing a crash, not an error.... wierd

The console was just to display the server setting up
User avatar
treebolt
User
User
Posts: 50
Joined: Thu Feb 21, 2008 4:38 pm
Location: Palm Harbor, FL

Post by treebolt »

I have another question.

when something like this happens

Code: Select all

*buffer = AllocateMemory(1000)
PokeS(*buffer,stringToBeSent)
SendNetworkData(ClientID,*buffer,1000)
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?
Foz
Addict
Addict
Posts: 1359
Joined: Tue Nov 13, 2007 12:42 pm
Location: Manchester, UK

Post by Foz »

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 :)
User avatar
treebolt
User
User
Posts: 50
Joined: Thu Feb 21, 2008 4:38 pm
Location: Palm Harbor, FL

Post by treebolt »

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'
milan1612
Addict
Addict
Posts: 894
Joined: Thu Apr 05, 2007 12:15 am
Location: Nuremberg, Germany
Contact:

Post by milan1612 »

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'
You can use StringByteLength() for this,
and btw. "test" would be 5 bytes as PB strings
are zero-terminated.
Windows 7 & PureBasic 4.4
zefiro_flashparty
User
User
Posts: 74
Joined: Fri Mar 04, 2005 7:46 pm
Location: argentina

Re: need help with 2d network game

Post by zefiro_flashparty »

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
Amd Vishera fx8350 ,16Gbram, Gtx650 ti, 2gb,Win 10pro. 13tbs. 8)
Nituvious
Addict
Addict
Posts: 1030
Joined: Sat Jul 11, 2009 4:57 am
Location: United States

Re: need help with 2d network game

Post by Nituvious »

These are really old topics.
▓▓▓▓▓▒▒▒▒▒░░░░░
Post Reply