Access MatchState in RPC functions

Hey everyone, I have a simple and (hopefully) easy to answer question:

I’m currently storing all connected presences in a TypeScript List inside of my custom match state interface, and I was wondering if it’s possible to access that match state inside of an RPC function.
(For this case specifically, I want to make an RPC function that sens the presence list of the server to the client)

In case anyone knows if and how thats possible, please let me know!

Also, here’s how my function currently looks:

let getMatchPresencesRpc: nkruntime.RpcFunction = function(ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, payload: string) {
        logger.info('Get match presences Rpc recieved.');
        //This is where I would like to access the match state and then return the presence list
        return JSON.stringify({});
}

Matches are designed to be self-contained and do not expose the match state directly.

What is it that you are trying to achieve that needs the client to be able to fetch the presence list of an ongoing match?

I noticed that what I was doing propably wasn’t the right way (I originally wanted to use that rpc to let the client fetch the presence list and then instance other players according to that list, but it seems like there is already such functionality on the Godot SDK, so that seems unnecessary).

However, I have another example that I struggle to find the correct way dealing with. Say for example I wanted the player to let the client send its position to the server and let the server save that information and then relay it to other clients in the matchLoop() function, after ensuring the position provided by the client is valid. To me it would make sense to save that information in a player list inside of the game state, but since it isn’t exposed to rpc functions, I wouldn’t know how to go about saving these player positions. Any ideas how this could be done instead?

Apologies but I’m not sure I follow why an rpc is needed. The clients would send their positions via the realtime connection to the match, within the match loop you inspect the messages, do the necessary checks to validate the positional changes, and if valid, then you can store the positions within an object in the match state that maps, for example, the player IDs to their current position.

I see now. To be honest, I am quite new to Nakama and didn’t really know that the realtime connection existed and how to use it, but after reading your answer and taking a look at the official TypeScript match handler template, I think I now have a better understanding of how to use it. And you’re absolutely correct, an rpc is unnecessary for what I was trying to do.

Thanks a lot for your help and patience!

1 Like

Hi @Coxcopi,

Just to follow up with some additional information that may be of use to you as you develop your game.

Typically your players would communicate via the realtime socket connection when part of a match using the SDK specific send and receive match state functionality. For examples of this, specifically for Godot as this appears to be the engine you are using, please see the Sending match state section of the Godot SDK client guide. In contrast, RPCs tend to be used for more on-demand and non-realtime client-to-server or server-to-server communication.

Further to this, we have just revamped our server runtime match handler API documentation which you may find useful. Please see the updated TypeScript match handler API documentation.

I hope this helps.

Kind regards,
Tom

2 Likes

Thanks @tom, I’ve read throught the client guide before, but it seems as if I skipped some important parts :wink:
This really helps!