Sign In Register

How can we help you today?

Start a new topic
Answered

node sdk - how do get the player object, set changes?

Hi there - 


In the API docs I see a spec for the player object, but the node sdk doesn't have a Spark object (i think), and i don't see request classes for these... how is this done?

Thanks,

--Rob


Best Answer

Hi Robert,


I'm going to share the solution provided through the ticketing system here as it may prove valuable to other users...


Normally these types of actions would be done through custom events. For example, you could create an event called 'ExpUp' that increases the player's experience by a set amount. Then add an attribute to this event, let's call it 'amountGained'. Now, in the cloud code for this event, you can use this figure to increase the player's experience, something like:


var player = Spark.getPlayer();

//this assumes the player's script data has a field called 'exp' tracking the player's experience

var exp = player.getScriptData("exp");


exp += Spark.getData().amountGained;


player.setScriptData("exp", exp);


//if you want to return the new value in your response

Spark.setScriptData("exp", exp);


//or Spark.setScriptData("playerData", player.getScriptData("exp"));


To call this function from nodejs you'd use:


gameSparks.sendAs(playerId, "LogEventReqest", {"eventKey":"expUp", "amountGained":50}, onExpUp, onExpUpError);


Then you can define how to handle your response and/or error in the callback functions specified. 


Regards,

Vinnie

Answer

Hi Robert,


I'm going to share the solution provided through the ticketing system here as it may prove valuable to other users...


Normally these types of actions would be done through custom events. For example, you could create an event called 'ExpUp' that increases the player's experience by a set amount. Then add an attribute to this event, let's call it 'amountGained'. Now, in the cloud code for this event, you can use this figure to increase the player's experience, something like:


var player = Spark.getPlayer();

//this assumes the player's script data has a field called 'exp' tracking the player's experience

var exp = player.getScriptData("exp");


exp += Spark.getData().amountGained;


player.setScriptData("exp", exp);


//if you want to return the new value in your response

Spark.setScriptData("exp", exp);


//or Spark.setScriptData("playerData", player.getScriptData("exp"));


To call this function from nodejs you'd use:


gameSparks.sendAs(playerId, "LogEventReqest", {"eventKey":"expUp", "amountGained":50}, onExpUp, onExpUpError);


Then you can define how to handle your response and/or error in the callback functions specified. 


Regards,

Vinnie

Again, thanks so much for your help - excellent product, excellent service :)

Hi guys, I'm implementing a password reset solution - currently we are emailing a user with a token (as per your tutorial), I'm just updating this so we email a link to a page we host that provides a form the user fills in with a new password, then sends the final reset request to Gamesparks from there, currently this is a MEAN stack service using the NodeJS Server SDK / API. 


I'm sending a password reset request like so:

 

const sendMessage = (password, token) => {
	let data = {
		userName:   '',
		password:   '',
		scriptData: {
			action:   action,
			token:    token,
			password: password,
		},
	};
	
	let json = JSON.stringify(data);
	
	console.log('sendMessage()', json);
	gs.sendAs('', requestType, json, onResponse);
};

 

BUT this always returns a NULL response from Gamesparks, I'm connecting using the API Key for the app and the 'server-send' credential as the secret - this credential appears to be the only one that connects and initalises the NodeJS API correctly on our service.


Can you explain why I'm getting a null response at all?


Cheers!


Chris

Login to post a comment