1
$\begingroup$

i working on a multi player game project its strategy so i need to sending data for more that one object in the same time i created a client and server and that's worked for one object in the server and one object in the client but after i added the second object to the server i get a proplem not error just 3d objects position proplem and the controller for server objects get swapped for example the server have two objects that's i can get access to control it ["Cube"] and ["Server"] and the server have only one object that's called ["Client"] the proplem is when i run server then i controll my objects the engine swaping the objects in the client screen for example whe i click up arrow to move the ["Server"] object to up the ["Cube"] object who moves in the Client screen and when i click w key to move ["Cube"] object to up the ["Server"] object who moves in the client screen

that's my client code the ["client"] object are connected to it in the game engine

#------------Setup--------------#
from pickle import dumps, loads
from mathutils import Vector
from GameLogic import * 
import GameLogic as GameLogic
import bge
from socket import *
import socket
import pickle


cont = GameLogic.getCurrentController()
owner = cont.owner
#obj = cont.owner

if not owner['OneTime']:
  ServerIP = GameLogic.IP
  Serverport = 2052
  Clientname = ''
  ClientPort = 2053
  GameLogic.sClient = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

  GameLogic.sClient.bind((Clientname,ClientPort))
  GameLogic.host = (ServerIP,Serverport)    
  GameLogic.sClient.setblocking(0)
  owner['OneTime'] = 1

PosYou = list(owner.worldPosition)

scene = getCurrentScene()
Cube = scene.objects["Cube"]
Server = scene.objects["Server"]


PosServer = [0,0,0]
PosCube = [0,0,0]
#------------RECEIVE/SEND--------------#    

Data = pickle.dumps((PosYou))

GameLogic.sClient.sendto(Data,GameLogic.host)

try:
    #thefucking proplem
    Data, SRIP = GameLogic.sClient.recvfrom(1024)



    UPData = pickle.loads(Data)
    PosServer = [UPData[0],UPData[1],UPData[2]]
    Server.worldPosition = PosServer 
    Data2, SRIP2 = GameLogic.sClient.recvfrom(1024)
    UPData2 = pickle.loads(Data2)

    PosCube = [UPData2[0],UPData2[1],UPData2[2]]
    Cube.worldPosition = PosCube 




except OSError:
    pass            
#---------------THE-END----------------#

that's my server code the ["Server"] object are connected to it in the game engine

#------------Setup--------------#
from GameLogic import * 
import GameLogic as GameLogic
import bge
from socket import *
import socket
import pickle

from pickle import *
cont = GameLogic.getCurrentController()
owner = cont.owner
#obj = cont.owner

if not owner['OneTime']:
  Host = "localhost"
  ServerPort = 2052
  GameLogic.sServer = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
  GameLogic.sServer.bind((Host,ServerPort)) 
  GameLogic.sServer.setblocking(0)
  owner['OneTime'] = True

PosYou = list(owner.worldPosition)

scene = getCurrentScene()
Client = scene.objects["Client"]
Cube = scene.objects["Cube"]
PosClient = [0,0,0]
PosCube = [0,0,0]
#------------RECEIVE/SEND--------------#

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


except OSError:
    pass
#---------------THE-END----------------#

the second object code the ["Cube"] object and that's connected two the object in the game engine and i get this error when i run it

File "secondob.py", line 18, in AttributeError: 'module' object has no attribute 'sServer'
#------------Setup--------------#
from GameLogic import * 
import GameLogic as GameLogic
import bge
from socket import *
import socket
import pickle

from pickle import *
cont2 = GameLogic.getCurrentController()
owner2 = cont2.owner
#obj = cont.owner





GameLogic.sServer.setblocking(0)
PosYou2 = list(owner2.worldPosition)


scene = getCurrentScene()
Client = scene.objects["Client"]
PosClient2 = [0,0,0]
#------------RECEIVE/SEND--------------#

try:

    Data2, CLIP2 = GameLogic.sServer.recvfrom(1024)


    UPData2 = pickle.loads(Data2)
    PosClient2 = [UPData2[0],UPData2[1],UPData2[2]]

    Client.worldPosition = PosClient2     
    Data2 = pickle.dumps((PosYou2))
    GameLogic.sServer.sendto(Data2,CLIP2)  
except OSError:
    pass
#---------------THE-END----------------#
$\endgroup$
  • $\begingroup$ Welcome to Blender SE! Don't forget to take the tour, so that you can avoid some of the simple mistakes ;-) $\endgroup$ – Matt May 23 '16 at 13:38
  • $\begingroup$ Please edit your question. Right now, there's no punctuation which makes it VERY difficult to read. If you make your question hard to read, it won't get read and it won't get answered. Help us help you! $\endgroup$ – Matt May 23 '16 at 13:39
  • $\begingroup$ Lastly, this question is NOT off topic (I think), but it is pretty advanced. While the Blender community here is pretty helpful, we generally don't get into advanced scripting like this. Don't delete the question! We'll certainly do our best! But it might be wise to also post this question over at StackOverflow and see if they can help, too. $\endgroup$ – Matt May 23 '16 at 13:42
1
$\begingroup$

The problem is global definitions in the beginning of the script. Move cont and owner definitions inside a function and you the function every time to get the reference or pass the reference around in functions. But you can't define them globally outside a function, otherwise 2 objects using the same script share the reference. Monster has written a good guide on this on blenderartists that should be a good read.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.