My goal was to create a simple multi-player connection.
I read the documentation but I’m struggling to make sense of it.
I have ‘cube.gltf’ and server.py / client.py and a file called direct.dc all in the same folder
I’ve been using chatGPT and ran what felt like thousands of iterations.
the following is the furthest I got.
here is the Server.py file:
#!/usr/bin/env python3
from direct.showbase.ShowBase import ShowBase
from direct.distributed.DistributedObject import DistributedObject
from direct.distributed.DistributedNodeAI import DistributedNodeAI
from direct.distributed.DistributedObjectAI import DistributedObjectAI
from direct.distributed.ClientRepository import ClientRepository
from panda3d.core import load_prc_file_data, NetDatagram, Datagram, ConnectionWriter, ConnectionReader, PointerToConnection, QueuedConnectionManager, QueuedConnectionListener, QueuedConnectionReader, QueuedConnectionListener
from panda3d.core import QueuedConnectionManager, QueuedConnectionListener, ConnectionWriter, QueuedConnectionReader, PointerToConnection, NetAddress
from panda3d.core import Vec3
# Disable audio
load_prc_file_data("", "audio-library-name null")
class MyAIRepository(ClientRepository):
def __init__(self):
ClientRepository.__init__(self, dcFileNames=['direct.dc'])
class MyDistributedObject(DistributedNodeAI):
def __init__(self, air):
DistributedNodeAI.__init__(self, air)
self.accept('arrow_left', self.move, [-1, 0, 0])
self.accept('arrow_right', self.move, [1, 0, 0])
self.accept('arrow_up', self.move, [0, 1, 0])
self.accept('arrow_down', self.move, [0, -1, 0])
def move(self, x, y, z):
self.setX(self.getX() + x)
self.setY(self.getY() + y)
self.setZ(self.getZ() + z)
self.sendUpdate('setPos', [self.getX(), self.getY(), self.getZ()])
class MyDistributedObjectAI(DistributedObjectAI):
def __init__(self, air):
DistributedObjectAI.__init__(self, air)
def sendGameData(self, data):
print(data)
def b_setLocation(self, x, y, z):
# Add your implementation here
pass
# Initialize the Panda3D server
worldServer = ShowBase(windowType='none')
# Set up the server repository
worldServer.cr = MyAIRepository()
worldServer.cr.isClient = 0
# Create the DistributedObject on the server
worldServer.distributedObject = MyDistributedObject(worldServer.cr)
# Initialize the AI (AIRepository)
air = MyAIRepository()
# Create the DistributedObjectAI on the server
myDistributedObjectAI = MyDistributedObjectAI(air)
myDistributedObjectAI.generate()
myDistributedObjectAI.b_setLocation(0, 0, 0)
myDistributedObjectAI.announceGenerate()
# Run the Panda3D server
worldServer.run()
Here is the client.py file:
#!/usr/bin/env python3
from panda3d.core import load_prc_file_data, Vec3, URLSpec
from direct.showbase.ShowBase import ShowBase
from direct.distributed.ClientRepository import ClientRepository
from direct.distributed.DistributedSmoothNode import DistributedSmoothNode
# Configure Panda3D to use the null audio library
load_prc_file_data("", "audio-library-name null")
class MyMainClientRepository(ClientRepository):
def __init__(self):
dcFileNames = ['direct.dc']
# Set up the main client repository
ClientRepository.__init__(
self,
dcFileNames=dcFileNames,
threadedNet=True)
# Set the IP or hostname of the server we want to connect to
hostname = '192.168.1.227'
tcpPort = 9099
# Build the URL from the server hostname and port.
self.url = URLSpec('http://{}:{}'.format(hostname, tcpPort))
# Attempt a connection to the server
self.connect([self.url],
successCallback=self.connectSuccess,
failureCallback=self.connectFailure)
def connectSuccess(self):
# Successfully connected.
base.cr = MyGameClientRepository()
base.cr.isClient = 1
def connectFailure(self, statusCode, statusString):
# Connection failed.
print("Failed to connect to the server. Status code: {}, Status string: {}".format(statusCode, statusString))
class MyGameClientRepository(ClientRepository):
def __init__(self):
dcFileNames = ['direct.dc']
# Set up the game client repository
ClientRepository.__init__(
self,
dcFileNames=dcFileNames,
threadedNet=True)
# Set the IP or hostname of the server we want to connect to
hostname = '192.168.1.227'
tcpPort = 9099
# Build the URL from the server hostname and port.
self.url = URLSpec('http://{}:{}'.format(hostname, tcpPort))
# Attempt a connection to the server
self.connect([self.url],
successCallback=self.connectSuccess,
failureCallback=self.connectFailure)
def connectSuccess(self):
# Successfully connected.
self.gotCreateReady()
def gotCreateReady(self):
# Ready to enter the world.
# Expand your interest to include any other zones.
base.localAvatar = MyDistributedSmoothNode()
base.localAvatar.reparentTo(render)
class MyDistributedSmoothNode(DistributedSmoothNode):
def __init__(self):
DistributedSmoothNode.__init__(self)
self.setName('localAvatar')
self.startSmooth()
# Initialize Panda3D
base = ShowBase()
# Set up the main client repository
base.cr = MyMainClientRepository()
# Run the Panda3D application
base.run()
Besides a file called cube.gltf (my model) I have this file called direct.dc
from panda3d.direct import DCFile
typedef struct {
string value_a;
uint8 value_b;
int8 value_c/100;
} gameDataModel;
dclass DGameObject : DistributedNode {
sendUpdateToAiGame(gameDataModel data);
};
dclass DGameObjectAI : DistributedObject {
setAnounceGenerate(string helloMsg) broadcast ram;
messageRoundtripToClient(gameDataModel data) clsend;
sendGameData(gameDataModel data) clsend;
};
dclass MyDistributedObject : DistributedNode {
// Add methods and fields as needed
};
dclass MyDistributedObjectAI : DistributedObject {
// Add methods and fields as needed
};
Does anybody out there have a working script or any way I can get this puppy to purrr.
Thank you in advance.