Hello,
I have problem with python script in game engine. That script running OK (I see it in console), but Game engine freeze.
Does anybody knows where the problem is?
thaks for any help
Peter
import socket
import string
HOST = ‘localhost’
PORT = 8778
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
cont = GameLogic.getCurrentController()
own = cont.getOwner()
bone = GameLogic.getCurrentScene().getObjectList()[“cube”]
while 1:
data = s.recv(4096)
b = string.find(data, “X=”)
c = string.find(data, “_”)
P = data[b+2:c]
X = float§
print X
d = string.find(data, “Y=”)
e = string.find(data, “_Z”)
Q= data[d+2:e]
Y = float(Q)
print Y
f = string.find(data, “Z=”)
g = string.find(data, “_end”)
R= data[f+2:g]
Z = float®
print Z
bone.setPosition([X,Y,Z])
If you trigger the script through the regular gamelogic python controller and have the sensor set to allways, the script will rerun automaticly for every frame… so no need to loop.
Now your script just loops internally on the first frame… so the game has no chance to progress.
Scripts in BGE have to finish as soon as possible, because BGE waits for them. And you have infinite cycle inside the script, so it never finish and so BGE cannot continue.
Another problem is that recv() blocks processing of script until it receives some data. So it can also cause lagging, even if server sends data constantly. You can use socket’s method setblocking() to change this behaviour.