Cannot pass additional data in auth request and tuck away in player scriptData for new player
J
Jeff Amiel
started a topic
over 6 years ago
The goal: Pass in some data (like client version number) along with authentication request - and put it into
Spark.getPlayer().setScriptData('versionNumber');
the easy part : add the version number(s) to the request script data on the Unity side
request.SetScriptData( new GSRequestData() .AddNumber("versionNumber", clientversionnumber));
where request is a GameSparks.Api.Requests.DeviceAuthenticationRequest()
The Hard part - I can't both get the value OUT of the inbound script data AND set it to the player...
I can't hook the AuthenticationResponse script - because the data.scriptData is not available there - it's already overwritten with response info?
I CAN hook the DeviceAuthenticationRequest script (although I then have to hook all the other auth scripts the same way)....but the problem is that for 'new' players, I can't access the player via Spark.getPlayer() as it doesn't exist 'yet' (it's null/empty/missing for new players). So while this works for existing players - not for new ones.
Ahg.
Essentially I am getting this error ONLY when it's a new player
{"stackTrace":"\tat 299633-modules-PlayerFuncs\n\tat 299633-request-DeviceAuthenticationRequest\n","error":"TypeError: Cannot call method \"setScriptData\" of null","script":"DeviceAuthenticationRequest"}
I'd hate for the only answer to be putting it in a different collection - which isn't cached for the player automagically...
thanks in advance!
Best Answer
C
Customer Support
said
over 6 years ago
Hi Jeff,
You should always be able to get the playerId in the AuthenticationResponse, the only time this would return as null would be if there was an error in the Authentication and the player wasn't authenticated. With correct details this should always return the playerId. To pass data from the request to the response and then onto the player you could use the following.
//Test Harness Registration
{
"@class": ".RegistrationRequest",
"displayName": "",
"password": "",
"segments": {},
"userName": "",
"scriptData":{"myKey":10} //scriptData value we want to set on the Player
}
//In the Cloud Code of RegistrationRequest
//grab the scriptData value from the request
var value = Spark.getData().scriptData.myKey
//set it to scriptData so we can use it in the response
Spark.setScriptData("myKey", value)
//In the Cloud Code of RegistrationResponse
//get the "myKey" value we've passed though
var getValue = Spark.getScriptData("myKey")
//set the value to the players scriptData
Spark.getPlayer().setScriptData("myKey", getValue)
If you have any further questions just let me know.
You should always be able to get the playerId in the AuthenticationResponse, the only time this would return as null would be if there was an error in the Authentication and the player wasn't authenticated. With correct details this should always return the playerId. To pass data from the request to the response and then onto the player you could use the following.
//Test Harness Registration
{
"@class": ".RegistrationRequest",
"displayName": "",
"password": "",
"segments": {},
"userName": "",
"scriptData":{"myKey":10} //scriptData value we want to set on the Player
}
//In the Cloud Code of RegistrationRequest
//grab the scriptData value from the request
var value = Spark.getData().scriptData.myKey
//set it to scriptData so we can use it in the response
Spark.setScriptData("myKey", value)
//In the Cloud Code of RegistrationResponse
//get the "myKey" value we've passed though
var getValue = Spark.getScriptData("myKey")
//set the value to the players scriptData
Spark.getPlayer().setScriptData("myKey", getValue)
If you have any further questions just let me know.
Thanks,
Liam
1 person likes this
J
Jeff Amiel
said
over 6 years ago
So I ended up using the technique above to grab the data in the request - and then in the response, while I dont have access to Spark.GetPlayer() for 'new' players (in an auth response), I've worked up this handy technique.
/* So Spark.getPlayer() will return null if the player is new - so we need a way to get the player during auth workflows */
safeGetPlayer : function () {
var player = Spark.getPlayer();
if (player === null) {
/* no player object yet - grab the field used by the auth functions to store the player id*/
var playerId = Spark.getData().userId;
player = Spark.loadPlayer(playerId);
}
return player;
},
Jeff Amiel
The goal: Pass in some data (like client version number) along with authentication request - and put it into
the easy part : add the version number(s) to the request script data on the Unity side
where request is a GameSparks.Api.Requests.DeviceAuthenticationRequest()
The Hard part - I can't both get the value OUT of the inbound script data AND set it to the player...
I can't hook the AuthenticationResponse script - because the data.scriptData is not available there - it's already overwritten with response info?
I CAN hook the DeviceAuthenticationRequest script (although I then have to hook all the other auth scripts the same way)....but the problem is that for 'new' players, I can't access the player via Spark.getPlayer() as it doesn't exist 'yet' (it's null/empty/missing for new players). So while this works for existing players - not for new ones.
Ahg.
Essentially I am getting this error ONLY when it's a new player
the code generating it is ...
Thoughts?
I'd hate for the only answer to be putting it in a different collection - which isn't cached for the player automagically...
thanks in advance!
Hi Jeff,
You should always be able to get the playerId in the AuthenticationResponse, the only time this would return as null would be if there was an error in the Authentication and the player wasn't authenticated. With correct details this should always return the playerId. To pass data from the request to the response and then onto the player you could use the following.
If you have any further questions just let me know.
Thanks,
Liam
- Oldest First
- Popular
- Newest First
Sorted by Newest FirstCustomer Support
Hi Jeff,
You should always be able to get the playerId in the AuthenticationResponse, the only time this would return as null would be if there was an error in the Authentication and the player wasn't authenticated. With correct details this should always return the playerId. To pass data from the request to the response and then onto the player you could use the following.
If you have any further questions just let me know.
Thanks,
Liam
1 person likes this
Jeff Amiel
So I ended up using the technique above to grab the data in the request - and then in the response, while I dont have access to Spark.GetPlayer() for 'new' players (in an auth response), I've worked up this handy technique.
Jeff Amiel
oops - link https://support.gamesparks.net/support/discussions/topics/1000055250
Jeff Amiel
hmmm..found this thread - setting the inbound data into scriptData so it can be picked up in the response.
Sounds hackish - but i will give it a try!
-
Documentation Notes
-
Design issues with user events
-
Using NoSQL
-
Runtime Collections vs Metadata Collections
-
Anonymous authentication from browser app
-
Modules
-
Movement With Unity
-
Problem with url parameters for downloadables
-
Querying NoSql GameSparks database
-
Challenge accesType
See all 2487 topics