Create match and the creator auto join the match

When a player creates a custom match,and how the creator auto join the match? thanks

in the rpc call createroom,after I call nk.MatchCreate,is there any interface to join the creater to ther match. or i should implement in the function func (m *MatchHandler) MatchInit

in the rpc call i write some code like this:

        userName, ok := ctx.Value(runtime.RUNTIME_CTX_USERNAME).(string)
		if !ok {
			return "", internal.ErrNoUserNameFound
		}
		sessionID, ok := ctx.Value(runtime.RUNTIME_CTX_SESSION_ID).(string)
		if !ok {
			logger.Error("context did not contain session ID.")
			return "", internal.ErrNoUserSessionFound
		}

		node, ok := ctx.Value(runtime.RUNTIME_CTX_NODE).(string)
		if !ok {
			logger.Error("context did not contain node.")
			return "", internal.ErrNoUserSessionFound
		}
		presence := &MatchPresence{
			Node:      node,
			UserID:    userid,
			SessionID: sessionID,
			Username:  userName,
		}
		matchID, err := nk.MatchCreate(ctx, MatchModuleName, map[string]interface{}{"request": request, "presence": presence})
		if err != nil {
			logger.Error("error creating match: %v", err)
			return "", internal.ErrInternalError
		}

and in the MatchInit func i write this

func (m *MatchHandler) MatchInit(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, params map[string]interface{}) (interface{}, int, string) {
	request, ok := params["request"].(*pb.RpcCreateMatchReq)
	if !ok {
		logger.Error("invalid match init parameter \"request\"")
		return nil, 0, ""
	}

	presence, ok := params["presence"].(*MatchPresence)
	if !ok {
		logger.Error("invalid match init parameter \"presence\"")
		return nil, 0, ""
	}

	open := 0
	if request.Open {
		open = 1
	}

	label := &MatchLabel{
		Start:     0,
		Open:      open,
		MatchName: request.MatchName,
	}

	labelJSON, err := json.Marshal(label)
	if err != nil {
		logger.WithField("error", err).Error("match init failed")
		labelJSON = []byte("{}")
	}

	state := &MatchState{
		random:       rand.New(rand.NewSource(time.Now().UnixNano())),
		label:        label,
		open:         request.Open,
		roomPassword: request.Password,
		presences:    make(map[string]runtime.Presence, maxSize),
	}

	state.presences[presence.GetUserId()] = presence
	return state, tickRate, string(labelJSON)
}

Am I right?

sessionID, ok := ctx.Value(runtime.RUNTIME_CTX_SESSION_ID).(string) when rpc will fail to get session_id…how can i do.thanks

This can work, but easier option would be to return matchId in the result of your RPC call and let creator join that match normally with Nakama: Client Relayed Multiplayer | Heroic Labs Documentation

1 Like

@hexun80149128
For instance in Unity i do something like this.

In client i call and rpc and wait for it to return match id

And server side.
image