Sign In Register

How can we help you today?

Start a new topic
Answered

Getting the response out of a callback function

Hi, i want to use LogEventRequest inside a RealTime Module to obtain our player active platoon, this is my code:

 

this.RecieveActivePlatoon= function(player){

var PlayerInventory= RTSession.newRequest().createLogEventRequest().setEventKey("INVENTORY").setPlayerId(player.getPlayerId());

 

PlayerInventory.send(function(response){

var ActivePlatoon= new Object();

var object=response.scriptData.return;

 

  for (var i in object)

  {

      for(var j in object[i])

      {

          if(object[i][j].Active==true){

               ActivePlatoon=Object.create(object[i][j]); // ActivePlatoon to use outside

            }

      }

  }

});

};

 

How i can get ActivePlatoon outside from the function(response)? I know its a callback function and for definition i can't return anything outside the response, but i need to use ActivePlatoon on external custom logic.

Any suggestion on how to get out the data?


Best Answer

Ah, sorry.
I repeated a variable name in the last example. Here is something more complete.
Something like this...

var USAStuff = []
for(var platoonName in response.scriptData.return.USA)
{
    var platoon = { "platoonName" : platoonName };
    platoon.active = response.scriptData.return.USA[platoonName].Active;
    platoon.troops = [];
    for(var troop in response.scriptData.return.USA[platoonName])
    {
        if(troop.indexOf("Troop") !== -1 &&  response.scriptData.return.USA[platoonName][troop] !== null){
            platoon.troops.push({ "name" : response.scriptData.return.USA[platoonName][troop].Name,
                                  "level" : response.scriptData.return.USA[platoonName][troop].Level
            });
        }
    }
    USAStuff.push(platoon);
}


 Hey There,

Can you send us an example of what the JSON response from "INVENTORY" looks like normally, lets say, if you called it from the test-harness instead?
If i can see that, i can figure out how to parse it.

Thanks,
Sean

Hi Sean, Thanks for fast response!
This is the Response from event "INVENTORY" in the Test Harness:

  

{
  "@class": ".LogEventResponse",
  "scriptData": {
    "return": {
      "USA": {
        "Platoon1": {
          "Troop1": {
            "Name": "Willy",
            "Level": 1
          },
          "Troop2": {
            "Name": "Hurricanes",
            "Level": 1
          },
          "Troop3": {
            "Name": "Brownie",
            "Level": 1
          },
          "Troop4": null,
          "Troop5": null,
          "Active": true
        },
        "Platoon2": {
          "Troop1": {
            "Name": "Willy",
            "Level": 1
          },
          "Troop2": {
            "Name": "Hurricanes",
            "Level": 1
          },
          "Troop3": {
            "Name": "Brownie",
            "Level": 1
          },
          "Troop4": null,
          "Troop5": null,
          "Active": null
        },
        "Platoon3": {
          "Troop1": {
            "Name": "Willy",
            "Level": 1
          },
          "Troop2": {
            "Name": "Hurricanes",
            "Level": 1
          },
          "Troop3": {
            "Name": "Brownie",
            "Level": 1
          },
          "Troop4": null,
          "Troop5": null,
          "Active": null
        }
      },
      "UK": {
        "Platoon1": {
          "Troop1": {
            "Name": "Dingo",
            "Level": 1
          },
          "Troop2": {
            "Name": "WonderBoys",
            "Level": 1
          },
          "Troop3": {
            "Name": "Brenny",
            "Level": 1
          },
          "Troop4": null,
          "Troop5": null,
          "Active": null
        },
        "Platoon2": {
          "Troop1": {
            "Name": "Dingo",
            "Level": 1
          },
          "Troop2": {
            "Name": "WonderBoys",
            "Level": 1
          },
          "Troop3": {
            "Name": "Brenny",
            "Level": 1
          },
          "Troop4": null,
          "Troop5": null,
          "Active": null
        },
        "Platoon3": {
          "Troop1": {
            "Name": "Dingo",
            "Level": 1
          },
          "Troop2": {
            "Name": "WonderBoys",
            "Level": 1
          },
          "Troop3": {
            "Name": "Brenny",
            "Level": 1
          },
          "Troop4": null,
          "Troop5": null,
          "Active": null
        }
      }
    }
  }
}

 After i have obtained response data from event "INVENTORY", our custom logic filter the response to get "ActivePlatoon": at the end of loops "ActivePlatoon" seems like:


image

 

We need to use this "ActivePlatoon" inside other modules to Initialize our custom game logic.(eg. TroopsAI, Experience,...).


Inside the realtime script our code appears like this:

 

RTSession.onPlayerConnect(function(player){
    var Test=PlayerState.RecieveActivePlatoon(player);
    RTSession.getLogger().debug(Test);    //Actually empty
}   

 

Sorry for the poor grammar and thanks in advance for your help, we really appreciate it.


TD team



Can you test something for me?
If you put something like this into the response, do you get a valid doc in the RT logs?

for(var key in response.scriptData.return.USA)
{
    RTSession.getLogger().debug(response.scriptData.return.USA[key]) 
}

I think something like this should work for you. Its just a matter of iterating over each sub-object.
Unfortunately i think this might fail when it comes to those null values in your JSON. Maybe you can try parsing them or removing them from the JSON

var USAStuff = []
for(var platoon in response.scriptData.return.USA)
{
    var platoon = { "platoonName" : platoon };
    platoon.troops = [];
    for(var troop in response.scriptData.return.USA[platoon])
    {
        platoon.troops.push({ "name" : response.scriptData.return.USA[platoon][troop].Name,
                              "level" : response.scriptData.return.USA[platoon][troop].Level
        });
    }
    USAStuff.push(platoon);
}

Yes i can! 

This is a response data with your first test: for loop (for 1 player):


image


image


image


Each screen represents a different platoon.


With the parsing test this is the result:


image


As you can see the troops result empty.

Answer

Ah, sorry.
I repeated a variable name in the last example. Here is something more complete.
Something like this...

var USAStuff = []
for(var platoonName in response.scriptData.return.USA)
{
    var platoon = { "platoonName" : platoonName };
    platoon.active = response.scriptData.return.USA[platoonName].Active;
    platoon.troops = [];
    for(var troop in response.scriptData.return.USA[platoonName])
    {
        if(troop.indexOf("Troop") !== -1 &&  response.scriptData.return.USA[platoonName][troop] !== null){
            platoon.troops.push({ "name" : response.scriptData.return.USA[platoonName][troop].Name,
                                  "level" : response.scriptData.return.USA[platoonName][troop].Level
            });
        }
    }
    USAStuff.push(platoon);
}

Hi, i've just put one condition inside for loop to filter active platoon. 

My code now:

var USAStuff = [];
for(var platoonName in response.scriptData.return.USA)
{
    var platoon = { "platoonName" : platoonName };
    if(response.scriptData.return.USA[platoonName].Active==true){
        
    platoon.active = response.scriptData.return.USA[platoonName].Active;
    platoon.troops = [];
    for(var troop in response.scriptData.return.USA[platoonName])
    {
        if(troop.indexOf("Troop") !== -1 &&  response.scriptData.return.USA[platoonName][troop] !== null){
            platoon.troops.push({ "name" : response.scriptData.return.USA[platoonName][troop].Name,
                                  "level" : response.scriptData.return.USA[platoonName][troop].Level
            });
        }
    }
    USAStuff.push(platoon);
    }
}
 RTSession.getLogger().debug(USAStuff);
}); 

 And RT logger is: 

image


Now i have the same initial problem:

How can i get  my "USAStuff" outside  my "playerState" RTModule? And use it in RealTimeScript when players connect?

Hi! In the end i found the solution to the problem i was having, i simply called a custom function "SetActivePlatoon" from another module inside the callback, so i could use the result in my custom logic.


Sorry for the late update but i was working full force these days, and thank you so much for your help! Without it i wouldn't be able to solve my issues!


TD Devteam.

Login to post a comment