I said I’ve tried both typescript runtime and lua runtime so I just assume maybe its a problem with the defold sdk.
But here’s my client code
I create and join a match like this,
function start_a_run()
nakama.sync(function()
local ok, err = socket.connect()
if ok then
local payload ={
}
nakama.rpc_func(client, "create_match_rpc", json.encode(payload), nil, function(result)
local response = json.decode(result.payload)
print("response here:")
print(response["matchid"])
if response["matchid"] ~= nil then
nakama.sync(function()
matchid=response["matchid"]
match = socket.match_join(response["matchid"])
if match.error then
print(match.error.message)
end
in_match()
monarch.show(hash("game"))
end)
end
end)
end
if err then
print(err.message)
end
end)
end
trying to send match data like this,
function update_inputs()
if match==nil or matchid == nil or not match_loaded then
return
end
local data = json.encode({
x = input.x,
y = input.y
})
nakama.sync(function()
local mid= match.match["match_id"]
print("send to", mid)
local result = socket.match_data_send(mid , opcodes.send_input, data)
if result.error then
print(result.error.message)
pprint(result)
end
end)
end
my create match rpc
local function create_match(context, payload)
local modulename = "normal_run"
local setupstate = { initialstate = payload }
local matchid = nk.match_create(modulename, setupstate)
return nk.json_encode({ matchid = matchid })
end
nk.register_rpc(create_match, "create_match_rpc")
my entire match handler
const enum opcodes {
position=1,
two,
receive_input
};
type Relic = {
};
type Input = {
x: number,
y: number
};
type Player = {
x: number,
y: number,
relics: Relic[],
input: Input
};
type SendState = {
players: {[userId: string]: Player}
}
const matchInit = function (ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, params: {[key: string]: string}): {state: nkruntime.MatchState, tickRate: number, label: string} {
logger.debug('Lobby match created');
const presences: {[userId: string]: nkruntime.Presence} = {};
const players: {[userId: string]: Player}={};
return {
state: { presences,players },
tickRate: 10,
label: 'normalrun'
};
};
const matchJoinAttempt = function (ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, dispatcher: nkruntime.MatchDispatcher, tick: number, state: nkruntime.MatchState, presence: nkruntime.Presence, metadata: {[key: string]: any }) : {state: nkruntime.MatchState, accept: boolean, rejectMessage?: string | undefined } | null {
logger.debug('%q attempted to join Lobby match', ctx.userId);
return {
state,
accept: true
};
}
const matchJoin = function (ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, dispatcher: nkruntime.MatchDispatcher, tick: number, state: nkruntime.MatchState, presences: nkruntime.Presence[]) : { state: nkruntime.MatchState } | null {
presences.forEach(function (presence) {
let player : Player = {
x:0,
y:0,
relics: [],
input: {x:0,y:0}
}
state.presences[presence.userId] = presence;
state.players[presence.userId] = player;
logger.debug('%q joined Lobby match', presence.userId);
});
return {
state
};
}
const matchLeave = function (ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, dispatcher: nkruntime.MatchDispatcher, tick: number, state: nkruntime.MatchState, presences: nkruntime.Presence[]) : { state: nkruntime.MatchState } | null {
presences.forEach(function (presence) {
delete (state.presences[presence.userId]);
logger.debug('%q left Lobby match', presence.userId);
});
return {
state
};
}
const matchLoop = function (ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, dispatcher: nkruntime.MatchDispatcher, tick: number, state: nkruntime.MatchState, messages: nkruntime.MatchMessage[]) : { state: nkruntime.MatchState} | null {
// logger.info('match loop');
Object.keys(state.presences).forEach(function (key) {
const presence = state.presences[key];
// logger.info('Presence %v name $v', presence.userId, presence.username);
});
//logger.info('message count: %v', messages.length);
messages.forEach(function (message) {
logger.info('Received %v from %v', message.data, message.sender.userId);
dispatcher.broadcastMessage(2, message.data, [message.sender], null);
});
let send : SendState = { players: state.players }
if (tick%100 == 0)
{
dispatcher.broadcastMessage(opcodes.position, JSON.stringify(send), null, null);
}
return {
state
};
}
const matchTerminate = function (ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, dispatcher: nkruntime.MatchDispatcher, tick: number, state: nkruntime.MatchState, graceSeconds: number) : { state: nkruntime.MatchState} | null {
logger.debug('Lobby match terminated');
const message = `Server shutting down in ${graceSeconds} seconds.`;
dispatcher.broadcastMessage(2, message, null, null);
return {
state
};
}
const matchSignal = function (ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, dispatcher: nkruntime.MatchDispatcher, tick: number, state: nkruntime.MatchState, data: string) : { state: nkruntime.MatchState, data?: string } | null {
logger.debug('Lobby match signal received: ' + data);
return {
state,
data: "Lobby match signal received: " + data
};
}
my init module (again I switched to typescript to try and figure out what was wrong.
let InitModule: nkruntime.InitModule =
function(ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, initializer: nkruntime.Initializer) {
logger.info("Hello World!");
initializer.registerMatch('normal_run', {
matchInit,
matchJoinAttempt,
matchJoin,
matchLeave,
matchLoop,
matchSignal,
matchTerminate
});
}