简体   繁体   中英

JS Object odd behavior

I am currently building a attempting to build a Multiplayer game with Phaser and Eureca io. I am at the stage where i am trying to nail the authentication of the players and what they are controlling, i am doing this by on the server having a method that returns the correct player id.

The method on the server is -

 eurecaServer.exports.getPlayer2Id = function(name , id)
 {

   if(name == "Fly" && id == player2id)
   {
     console.log("correct fly player");
     return player2id;
   }
   else
   {
     console.log("Wrong fly player");
     return id;
   }
}

On the client side at the moment i am only testing, but when i call the function it returns an object. When simply looking at the object in the console it displays this. Object {status: 0, result: null, error: null, sig: "eY0IjunQt7"} so it is telling me my result is null which is odd, but when expanding it i get.

Object {status: 0, result: null, error: null, sig: "eY0IjunQt7"} callback: function() error: null errorCallback: function() onReady: (fn, errorFn) result: "jVcZvzetc8AAK45NAAAH" sig: "eY0IjunQt7"

As you can see when expanding the result is not null, and is exactly what im looking for, i have tried, JSON.stringify but it gives me the first result, where it tells me the result is null.

Any ideas how i can get my actual result or why this is happening.

Thanks.

Edit:

The client side component for server is defined in the main game file

var eurecaClientSetup = function() {

var eurecaClient = new Eureca.Client();

eurecaClient.ready(function (proxy) {
    eurecaServer = proxy;

});

Then in the object class it is called here.

this.name = "Fly"
this.id = index;  //Passed in on object creation
this.currPlayer = eurecaServer.getPlayer2Id(this.name, this.id);
console.log(JSON.stringify(this.currPlayer));
console.log(this.currPlayer);

Any server operation is going to be asynchronous, regardless of the framework*. Different frameworks have different ways of getting the result once it is available. In Eureca.io, it seems to be using an .onReady() call after the server function name.

In other words, what you need to do is change your client code to the following:

this.name = "Fly"
this.id = index;  //Passed in on object creation
var _this = this; // needed because "this" inside function below won't be the same
eurecaServer.getPlayer2Id(this.name, this.id).onReady(function (id) {
    _this.currPlayer = id;
    console.log(_this.currPlayer); // shows the right thing
    // do other stuff here now you have the right player id
});

* Technically, you can do a synchronous/blocking AJAX request, but it is bad practice in 99% of cases.

I'm the author of eureca.io, the answer given by GregL is correct. I just want to add that onReady() call will be deprecated, eureca.io support promise like call (using then() function).

so you can use this syntax instead, which is more standard.

this.name = "Fly"
this.id = index;  //Passed in on object creation
var _this = this; // needed because "this" inside function below won't be the same
eurecaServer.getPlayer2Id(this.name, this.id).then(function (id) {
    _this.currPlayer = id;
    console.log(_this.currPlayer); // shows the right thing
    // do other stuff here now you have the right player id
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM