-1

I'm using this code to send information to Pure Data, in the Python console I see the two different variables, however Pure Data keeps receiving them added together not as two separate numbers.

import bge

# run main program
main()

import socket

# get controller 
cont2 = bge.logic.getCurrentController()
# get object that controller is attached to 
owner2 = cont2.owner
# get the current scene 
scene = bge.logic.getCurrentScene()
# get a list of the objects in the scene 
objList = scene.objects

# get object named Box 
enemy = objList["enemy"]
enemy2 = objList["enemy2"]

# get the distance between them 
distance = owner2.getDistanceTo(enemy)
XValue = distance  
print (distance)
# get the distance between them 
distance2 = owner2.getDistanceTo(enemy2)
XValue = distance2  
print (distance2)    

tsr = str(distance + distance2)     
tsr += ';'
host = '127.0.0.1'
port = 50007
msg = '123456;'

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
s.send(tsr.encode())
s.shutdown(0)
s.close()

I need to send up to 10 different distances from objects, this is to do with finding distances from enemies

Max N
  • 1,134
  • 11
  • 23
adaadamad
  • 1
  • 1
  • 3
    please try to trim down your problem to a *minimal* example. e.g. it's rather irrelevant that you query `bge` to get your `enemies`, and what's that `msg` and `XValue` assignments doing? – umläute Mar 05 '16 at 21:46
  • Does this answer your question? [How to concatenate two integers in Python?](https://stackoverflow.com/questions/12838549/how-to-concatenate-two-integers-in-python) – ggorlen Aug 18 '21 at 20:27

2 Answers2

3

The problem is entirely in your python code:

You have two variables distance1 and distance2 (let's assume that distance1=666 and distance2=42 and then you construct a string:

tsr = str(distance1 + distance2)

Now this will first evaluate the expression distance1+distance2 (summing them up to 708) and then create a string from that value ("708"). So your Python script sends the munged data.

So your first step is to convert your values to strings before "adding" them (since adding strings is really appending them):

tsr = str(distance1) + str(distance2)

But this will really give you a string "66642", as you haven't told the appender to separate the to values with a whitespace.

So one correct solution is:

tsr = str(distance1) + " " + str(distance2)
tsr += ";"
umläute
  • 28,885
  • 9
  • 68
  • 122
0
var1="5Hello3How3Are3you8I'm FINE2is4that3so?3yes"
#initial measurement

m=var1[0]
m=int(m)
print var1[1:1+m]
INIT_LEN=1
LENGTH=m
n=1
NUMBER_FRAMES=9-1 #number of bytes 9

while n<=NUMBER_FRAMES:
  INIT_LEN=INIT_LEN+LENGTH
  l=var1[INIT_LEN]
  INIT_LEN=INIT_LEN+1
  LENGTH=int(l)
  print var1[INIT_LEN:INIT_LEN+LENGTH]
  n=n+1

If youre willing to transfer multiple strings concatinated as single concatinated string I suggest you go through the current code.