{
Hi,
I am trying to store dispatched codes and data during a match. When a player wants to switch back into any match, the aim is to have the player receive all match state updates and basically the game will play out automatically up to the point where the game was left off (this may sound weird - but it’s how I hope this will work).
In the matchLoop code in the server TS file, whenever a message is sent (i.e. dispatcher.broadcastMessage), I also call another function: saveDispatchedMessages
let matchLoop: nkruntime.MatchLoopFunction = function (context: nkruntime.Context, logger: nkruntime.Logger, nakama: nkruntime.Nakama, dispatcher: nkruntime.MatchDispatcher, tick: number, state: nkruntime.MatchState, messages: nkruntime.MatchMessage[]) {
let gameState = state as GameState;
for (let message of messages) {
dispatcher.broadcastMessage(message.opCode, message.data, null, message.sender);
saveDispatchedMessages(message.opCode, message.data, context.userId);
}
return gameState.endMatch ? null : { state: gameState };
}
The code for the saveDispatchedMessages function is as follows:
function saveDispatchedMessages(code: number, data: ArrayBuffer, userID: string) {
const matchState = [
{
code: code,
data: data,
},
]
var write: nkruntime.StorageWriteRequest = {
collection: "MatchStates",
key: "Dispatched",
value: matchState,
userId: userID,
permissionRead: 1,
permissionWrite: 0,
}
}
If I want to receive the match states to bring a match up to the point where I left it off. I use this:
let reloadMatchStates: nkruntime.RpcFunction = function (context: nkruntime.Context, logger: nkruntime.Logger, nakama: nkruntime.Nakama, dispatcher: nkruntime.MatchDispatcher, userID: string): string {
let storageReadReq: nkruntime.StorageReadRequest = {
collection: "MatchStates",
key: "Dispatched",
userId: userID,
}
let objects: nkruntime.StorageObject[] = nakama.storageRead([storageReadReq]);
//goal is to use dispatcher.broadcastMessage for each object
}
And basically I want the server to dispatch.broadcastMessage each of the objects (containing the code and data).
It’s not quite working yet, and I am not 100% sure if this is going to work or if I am on the right track.
It would be awesome to have so advice on this. If there is an example showing how to save each dispatched message and then retrieve this data to resend each previously dispatched message, that would be great.
Once a match ends, I will then delete this saved data.
}
- Versions: Nakama {3.5}, {Windows, Mac, Linux binary or Docker}, {client library (SDK) and version}
- Server Framework Runtime language (If relevant) {Go, TS/JS, Lua}
{code or log snippet}
Media: