eson
eson

Reputation: 1

Python tuple unpack problem

sendnpc = (npc2alive,Orinpc3,Posnpc3)   
Data = dumps((PosYou,OriYou,Shoot,txtt,Posnpc,Orinpc,npcalive,Posnpc2,Orinpc2,sendnpc))

I'm sending this material to another computer, the problem is when I'm unpacking it.

The unpacking part of the script looks like this:

Data, SRIP = GameLogic.sClient.recvfrom(1024)
UPData = loads(Data)
PosServer = [UPData[0][0],UPData[0][1],UPData[0][2]]
OriServer = [UPData[1][0],UPData[1][1],UPData[1][2]]
Server.setPosition(PosServer)
Server.setOrientation(OriServer)
Server.KeySens = UPData[2]
Pump1Shoot = UPData[2]
txt1.Text = UPData[3]
text = text + UPData[3]
Posnpc = [UPData[4][0],UPData[4][1],UPData[4][2]]
Orinpc = [UPData[5][0],UPData[5][1],UPData[5][2]]
npc.setPosition(Posnpc)
npc.setOrientation(Orinpc)
npcalives = UPData[6]
Posnpc2 = [UPData[7][0],UPData[7][1],UPData[7][2]]
Orinpc2 = [UPData[8][0],UPData[8][1],UPData[8][2]]
npc2.setPosition(Posnpc2)
npc2.setOrientation(Orinpc2)
npc2alives = UPData[9][0]
Posnpc3 = [UPData[9][1][0],UPData[9][1][1],UPData[9][1][2]]
Orinpc3 = [UPData[9][2][0],UPData[9][2][1],UPData[9][2][2]]
npc3.setPosition(Posnpc3)
npc3.setOrientation(Orinpc3)

The unpacking part works till I get to the 9th variable from the recived data (that is inside a tuple). The problem is that because I send an tuple nested in another tuple, when I'm unpacking it it looks like this:

PosYou,OriYou,Shoot,txtt,Posnpc,Orinpc,npcalive,Posnpc2,Orinpc2(npc2alive,Orinpc3,Posnpc3)

Now my question is simple, how can I unpack this correctly?

Upvotes: 0

Views: 639

Answers (2)

Lee D
Lee D

Reputation: 116

I notice that in most cases, the position comes before the orientation. But, in that last tuple, you have the orientation before the position: (npc2alive,Orinpc3,Posnpc3). When you unpack it, you're inadvertantly swapping the two.

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799480

Nest the LHS tuples.

>>> a, b, (c, d, e) = [1, 2, [3, 4, 5]]
>>> a
1
>>> b
2
>>> c
3
>>> d
4
>>> e
5

Upvotes: 1

Related Questions