3

What would this look like in UnityScript?

void playerDataLoaded( List< GameCenterPlayer > players )
{
    foreach( GameCenterPlayer p in players )
    // do something with p
}

Players is a list not an array.

I'm trying to handle an event response from Apple GameCenter that returns a list of plater attributes. These are the errors:

"length is not a member of object player"

"Type 'Object' (player) does not support slicing"

Uses iOS Game Center, Unity, and prime31 GameCenter plugin.

Community
  • 1
  • 1

5 Answers5

5
function removePlayersDataLoaded(players){
  for(var i = 0; i < players.length; i++){
    var p = players[i];
    // do something with p
  }
}
jvenema
  • 42,243
  • 5
  • 64
  • 107
  • It is worth nothing that this is correct because JavaScript does not have a good `foreach` replacement. Using `for (var p in players)` is [not recommended because it gives inconsistent results](http://stackoverflow.com/a/243778/272072). – Scott Rippey Dec 16 '11 at 00:22
  • It may be correct for a generic case but my specific case (which I probably should have clarified more so, my bad) it does not work. – user1001828 Dec 16 '11 at 02:00
  • Without knowing what "players" is, we can't answer the question. Your question states it's an array; if it doesn't have a .length property, then that's not the case... – jvenema Dec 16 '11 at 17:22
  • Sorry for being vague before. It turns out players is a list not an array. I think the revised original code snippet first listed should now give you a better sense of the details, maybe? – user1001828 Dec 16 '11 at 17:47
  • 1
    In C#, there's a distinction between a list and an array; native JavaScript has no concept of a "list". Are you using a library that's providing a list implementation? – jvenema Dec 16 '11 at 21:38
0

While C#'s List<> isn't identical to the below, something like this

function Player(playerName, gameCenterPlayer)
{
   this.playerName = playerName;
   this.GameCenterPlayer = gameCenterPlayer; // i'm going to assume this has been calculated elsewhere, and all we need now is a boolean.
}

function playerDataLoaded(players) {
  this.players = players
  this.getCenterPlayer = function() {
var i = 0
  while (i < players.length) {
  if (players[i].GameCenterPlayer) {
console.log(players[i].playerName + " is the center player!");
}
i++;
}
}

}

var player1 = new Player("Ted", 0);
var player2 = new Player("Jane", 1);
var player3 = new Player("Doug", 0);
Players = new playerDataLoaded([player1, player2, player3]);

Players.getCenterPlayer()
Jay Edwards
  • 712
  • 8
  • 17
0

It would be something like:

var remotePlayersDataLoaded = function(players) {
  for (var i = 0; i < players.length; i++) {
    var player = players[i];
  }
};
Rich O'Kelly
  • 38,811
  • 8
  • 78
  • 108
0

Using .forEach to call an iterator for each member in the list

function removePlayersDataLoaded(players) {
    players.forEach(doSomethingWithPlayer);

    function doSomethingWithPlayer(p) {
        // code
    }
}

Or using a for loop over the array

function removePlayersDataLoaded(players) {
    for (var i = 0, len = players.length; i < len; i++) {
        var p = players[i];
        // code
    }
}
Raynos
  • 156,883
  • 55
  • 337
  • 385
-2

I believe it is something like this:

function remotePlayersDataLoaded(players) {
for (p in players)
  {
  // do your thing with p
  } 
}

Could be mistaken however, does it complain?

YahooMania
  • 105
  • 3
  • 13
  • 1
    You fixed the syntax, the code is still full of anti patterns – Raynos Dec 16 '11 at 00:19
  • wow the braces, really? I fixed that before I saw your message. http://www.w3schools.com/js/js_loop_for_in.asp http://www.w3schools.com/js/js_functions.asp – YahooMania Dec 16 '11 at 00:21
  • Then can you point out what anti patterns I have. I did now know 2 lines of line, one being a signature could be FULL of anti patterns. The information is still there, it just depends on how you use it. – YahooMania Dec 16 '11 at 00:27
  • 2
    Your using `for ... in` to loop an array, that's bad practice mainly for performance reasons. Your not declaring the local variable `p` so it's an implied global. And you have a horrible white space style. But you should know all this, and if you don't then actually learn javascript before you show others how to do it. – Raynos Dec 16 '11 at 00:29
  • Well from the question I saw, the asker wanted to convert a method signature and and a for loop to Javascript. Also isn't players a list? StackOverFlow is a free website for anyone who needs help. If someone took time out to help me I would appreciate it instead of scrutinizing their credentials. – YahooMania Dec 16 '11 at 00:35
  • You may want to fix your answer with the suggested corrections. Your entitled to edit and improve your answer. – Raynos Dec 16 '11 at 01:01
  • I should probably point out that playerDataLoaded is an event response from Apple GameCenter which claims to be an Object. Sorry for lack of detail and not knowing proper terminology but JS is new to me as is c#. – user1001828 Dec 16 '11 at 01:22