Python picking help!

For questions about using Classic.

Post » Sun Apr 04, 2010 2:06 am

I am working on making the 2 player net pong game be four player but don't want to have to do it the tedious way of duplicating code for each player (in this case it is not really tedious, but wouldn't be practical for a 20+ person game which is the point of the exercise):

Here is Python function that updates the player position only if the player number is not yourself.

[code:34szvw2l]def Network_move(self, data):
if ((data['player'] == 1) and (System.globalvar("myPlayer") != 1)):
player1.SetPosition(data['playerx'],data['playery'])
elif ((data['player'] == 2) and (System.globalvar("myPlayer") != 2)):
player2.SetPosition(data['playerx'],data['playery'])[/code:34szvw2l]


For example, to make the above support 4 players I would have to double the code. Instead I would like to do something like (with all players in a Family) :

[code:34szvw2l]
if data['player'] != System.globalvar("myPlayer") :
Pick players by players PV == data['player']
players.SetPosition(data['playerx'],data['playery']) [/code:34szvw2l]

So something like the above where you pick a player based on their Private Variable (or something else) being the correct match. Now the above three or so lines are sufficient to handle 2, 4, 50 players.

Any ideas?
Thanks!
B
8
S
3
G
7
Posts: 835
Reputation: 5,313

Post » Sun Apr 04, 2010 5:29 am

Instead of using different objects per remote player, use just one :"players".

[code:303ifi0x]if data['player'] != System.globalvar("myPlayer") :
for w in players:
if(w.Value("player") == data['player']):
w.SetPosition(data['playerx'],data['playery'])
break[/code:303ifi0x]

But until "pick.Value("player")" works use the function object to get the private variable:
+ Function: On function "PVplayer"
+ players: Unique ID is Function.Param(1)
-> Function: Set return value to Sprite.Value('player')

and the code would become:
[code:303ifi0x]if data['player'] != System.globalvar("myPlayer") :
for w in players:
Function.AddParam(w.UID())
Function.Call("PVplayer",0)
if(Function.Return == data['player']):
w.SetPosition(data['playerx'],data['playery'])
break[/code:303ifi0x]
B
79
S
24
G
54
Posts: 4,746
Reputation: 40,755

Post » Sun Apr 04, 2010 7:26 pm

Thanks Rojo! That worked. :-) Now I'm trying to do it just by the index to improve performance.

Looks like I might just be able to do a:
players[data['player'] - 1].SetPosition(data['playerx'],data['playery'])

Thanks for the help! It's great having more Python guys around here.
B
8
S
3
G
7
Posts: 835
Reputation: 5,313


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 2 guests