question

hampusbankler avatar image
hampusbankler asked

SetTitleInternalData instantly updating?

I have a cloud function that gets an id from the server, and increments the one kept on the server "NextId", so that the next time this function is called, it should return another id.

If the time between the calls is large enough, it seems that it always works as expected. If I call this function with small delays (even like 3 seconds) it starts to sometimes return the same value (approx 50% of the times). I'm not sure, but it seems like the first call hasn't completed updating the server field before the next call reads i? Maybe.

When I tried calling it five times with 0.5 seconds in between, i got: 46, 47, 48, 47, 48 (so yeah, even going backwards sometimes)

I'm not sure if my syntax is wrong (javascript is outside my comfort zone), or if there's a limitation of how fast the server can handle the calls? Is there any way I can make it so I'm garantueed to get a new id on every call?

handlers.requestId = function (args, context) {
	var dataArray = server.GetTitleInternalData({
        Keys: ["NextId"]
    });
	
	var idString = dataArray.Data["NextId"];
	var id = parseInt(idString);
	
	var nextId = id + 1;
	
	server.SetTitleInternalData({
		Key: "NextId",
		Value: nextId
	});
	
	return { Id: id };
}
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Joshua Strunk avatar image
Joshua Strunk answered

"if there's a limitation of how fast the server can handle the calls?"

The answer is not fast at all. TitleData is both sharded and cached it is not meant to be used in this way. Links to relevant discussions on this topic.

Writable Shared Title Data

Writing Title Data in Cloud Script

Unexpected Results Returned from Cloud Script

"Is there any way I can make it so I'm guaranteed to get a new id on every call?"

You could attach the increment to each individual player and use there playfabID as part of it?

A tiny bit more risky would be to use some random functions to attempt GUID generation. The idea behind this method is when implemented correctly the chance of collisions drops to mathematically impossible range.

2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

hampusbankler avatar image hampusbankler commented ·

Thanks for the reply.

https://community.playfab.com/questions/956/211248347-Writing-Title-Data-in-Cloud-Script.html

This one was indeed very similar to my problem.

Clarification: In my use case, I don't need "just any" unique id. I need the next one. 1, 2, 3, 4 and so on (resetting to 0 when it reaches a certain cap, like 100). The number correspond to a string in an array (on the client side), generating names in a round-robin fashion. It's acceptable if it misses a number (1, 2, 4, 5), but it should never return the same number twice (1, 2, 2, 3).

No other workaround other than using yet another web service?

0 Likes 0 ·
brendan avatar image brendan hampusbankler commented ·

Correct - Title Data cannot be updated by a player-originating action, as it's not meant for frequent updates, nor does it "lock" the data for updates. Your best plan, if specifically want to have an incremental ID of some type for users would be to use an external web service (which you could then call via Cloud Script).

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.