python network(for multiplayer games)

this is a python network so u can play from 2 different computers on the same game but u both have to be connected to the same INTERNET IP or SERVER for it to work witch is better then split screen x]
the code below is the setup script name it Setup.py

from GameLogic import * 
#START THIS SETUP ONE TIME AS A SERVER AND ONE TIME AS A CLIENT

#BEVORE YOU START THIS SETUP AS A CLIENT REPLACE THE * IN THE 
#LINE BELOW WITH THE SERVER IP 
#(FOR EXAMPLE: GameLogic.IP = "192.123.100.1")



GameLogic.IP = "*********"





#THE CODE BELOW SETS UP IF YOU PLAY THE CLIENT OR THE SERVER
#TO START THE SERVER PRESS F1 TO START THE CLIENT F2

cont = GameLogic.getCurrentController()
obj = cont.getOwner()
scene = getCurrentScene()
Server = cont.getSensor("F1")
Client = cont.getSensor("F2")

if Server.isPositive():
	Server = scene.getObjectList()["OBServer"]
	Server.Activ = 1
if Client.isPositive():
	Client = scene.getObjectList()["OBClient"]
	Client.Activ = 1

this is the Server script name it Server.py

#------------Setup--------------#
from GameLogic import * 
from socket import *
from cPickle import *
cont = GameLogic.getCurrentController()
obj = cont.getOwner()

if obj.OneTime == 0:
	Host = ''
	ServerPort = 10000
	GameLogic.sServer = socket(AF_INET,SOCK_DGRAM)
	GameLogic.sServer.bind((Host,ServerPort))	
	GameLogic.sServer.setblocking(0)
	obj.OneTime = 1
	
PosYou = obj.getPosition()

scene = getCurrentScene()
Client = scene.getObjectList()["OBClient"]

PosClient = [0,0,0]
#------------RECEIVE/SEND--------------#

try:
	Data, CLIP = GameLogic.sServer.recvfrom(1024)
	UPData = loads(Data)
	PosClient = [UPData[0],UPData[1],UPData[2]]
	Client.setPosition(PosClient)	
	Data = dumps((PosYou))

	GameLogic.sServer.sendto(Data,CLIP)	
except:
	pass
#---------------THE-END----------------#

this is the Client script name it Client.py(duh)

#------------Setup--------------#
from GameLogic import * 
from socket import *
from cPickle import *
cont = GameLogic.getCurrentController()
obj = cont.getOwner()

if obj.OneTime == 0:
	ServerIP = GameLogic.IP
	Serverport = 10000
	Clientname = ''
	ClientPort = 10001
	GameLogic.sClient = socket(AF_INET,SOCK_DGRAM)
	GameLogic.sClient.bind((Clientname,ClientPort))
	GameLogic.host = (ServerIP,Serverport)	
	GameLogic.sClient.setblocking(0)
	obj.OneTime = 1
	
PosYou = obj.getPosition()

scene = getCurrentScene()
Server = scene.getObjectList()["OBServer"]

PosServer = [0,0,0]
#------------RECEIVE/SEND--------------#	
Data = dumps((PosYou))
GameLogic.sClient.sendto(Data,GameLogic.host)

try:
	Data, SRIP = GameLogic.sClient.recvfrom(1024)
	UPData = loads(Data)
	PosServer = [UPData[0],UPData[1],UPData[2]]
	Server.setPosition(PosServer)
except:
	pass			
#---------------THE-END----------------#

then load them all into ur game like thishttp://i48.tinypic.com/5vdicm.jpgand then after that name ur 2 players. name one Clienthttp://i46.tinypic.com/2lbc2ad.jpg and the other Serverhttp://i49.tinypic.com/jt4ads.jpg and now add these properties http://i45.tinypic.com/ffdba1.jpg after that add an sensor called propety and name it sActiv and in the property box type Activ and in the value box type in 1 and it should look like thishttp://i46.tinypic.com/2hrj2g9.jpg after ur done with that connect it with with every controller u have so it wont mess up on the connection .
after add 2 sensors they both have to be keybord sensors and ont should be called F1 and other called F2 (duh) and it has to be a capitol “F” or it wont work:eek: and they should look like thishttp://i48.tinypic.com/4talqw.jpgand then connect them to the setup.py control like thishttp://i48.tinypic.com/1zbs28y.jpg DONT CONNET ANYTHING TO THE SETUP.PY CONTROL BUT THE F1 and F2 SENSORS!!and that pretty much raps it up YAY!!!

i would like the blender foundation for the book and tutorials in the book they really made me improve allot :D:yes: THANKS A MILLION x]

the script and simple game is ready in the attachment it’s 2 posts down srry[ATTACH]128265[/ATTACH]

Attachments

BasicNetwork.blend (142 KB)

sounds cool but pls post a blend!

If you post a blend it will be very nice, but thanks! I can use this setup for all kinds of games?

yes u can do this sutup for any game u want and srry forgot about the attachment >.<

owhhhhhhhhhhaaaaaaaaa, this is amazing I was wokring on a game a while back, and I didnt finish it because of the online stuff that was killing me, Thanks for share this, I ll try it out since I have te time.
Thanks!

Works beautifully, you should be proud!

so tthis works only if the players are connected to the same network? well this is not so cool! i thought this will work with each PC and his connection!

Wrong, it’s up to you to figure out your external ip, enable port forwarding if you are using a router, and create an exception in your firewall if using one.

For some reason it didn´t work on my friends computer
got this on the console

Compiled with Python version 2.6.1.
Checking for installed Python... got it!
Detected GL_ARB_texture_env_combine
Detected GL_ARB_texture_cube_map
Detected GL_ARB_multitexture
Detected GL_ARB_shader_objects
Detected GL_ARB_vertex_shader
Detected GL_ARB_fragment_shader
Detected GL_ARB_vertex_program
Detected GL_ARB_depth_texture
Detected GL_EXT_separate_specular_color

Blender Game Engine Started

Method getOwner() is deprecated, please use the owner property instead.
        Setup.py:20
Method getSensor(string) is deprecated, please use the sensors[string] property
instead.
        Setup.py:22
Method isPositive() is deprecated, please use the read-only positive property in
stead.
        Setup.py:25
Method getObjectList() is deprecated, please use the objects property instead.
        Setup.py:26
Method ob.attr = val is deprecated, please use ob['attr'] = val instead.
        Setup.py:27
Python script error from controller "cServer.py#CONTR#1":
Traceback (most recent call last):
  File "Server.py", line 3, in &lt;module&gt;
  File "C:\Python26\Lib\socket.py", line 46, in &lt;module&gt;
    import _socket
ImportError: DLL load failed: The specified module could not be found.

Blender Game Engine Finished

Any idea why this happens?
It seems like something is wrongly installed or something.
I have python 2.6.5. installed on a WinXP Professional 64 bit SP2

Edit: It works on my computer at home weird.

hmm im not sure but i have blender 2.49 try reinstalling it.idk wat could be wrong but if it still dosnt work tell me so i can look for a better solution and try reinstalling python too

YES! This is awesome! LAN games here we come! This is a great gift for the community :smiley: On behalf of me and my nerdherd I thank you :stuck_out_tongue:

HOLY ****!!! This is the (third) most aazing post that i have seen, i will use this on my game so i can make space battles in multiplayer !!! :D!

Soon this script will be modified as to allow many players to join at any given time :smiley: Well I’ll give it a shot!

lol thanxs guys for the good comments and what iplur said could might be done if anyone wants to give it a shot with me pm me and i will send u my internet IP so we can see if its possible xP and pls dont u try hacking me when i give internet IP or ur toast :ba::ba:
AND I MEAN TOAST

P.S. if u want to make a co-op game tell me so i can modify this for u cuz this wont work for u if playing co-op games :stuck_out_tongue: lol

Dude co-op is a must since 1983! Mario Bros anyone? Wait that wasn’t co-op was it? hmmm… anyway why won’t co-op work with this method?

PS: NO! I got same error as Jorgie I’ll look into it.

i see that ppl are having problems so i will make a video and post it as soon as i can.i myself havent goten this error but i think it might be beause ur not naming the object"AKA player"Client or Server u need two and they both need one of those names or it wont wok and the fisr letter has to stay capitolized :stuck_out_tongue: but i will make vid anyways

It says it cannot find [_socket something .dll] so naturally I looked it up. However in my Python directory it’s called [_socket something .pyd] does that help in any way?:spin:

.pyd vs .dll

hmmm…well i will see wat version im using and look and see if i have that .dll in the mean while make a vid of the error so i can see how it happend ok :]
-----------------------------------------------------------------[edit]
ok all i know is that u guys did something wrong so i will make vid i might have to upload later cuz my computer is laggy

I just pressed P :frowning:

Edit: I looked up the wrong directory, but (now found the correct one) it’s still called _socket.py not .dll :confused:

This looks awesome, but when I when to implement it, I was stupid not enough to save. My game sometimes crashes blender when pressing ‘p’, so yeah, i lost what I did (only adding this). I would like to download the blend file but can’t find it in this forum. Is it still up somewhere?