If you got en error in dmengine that is normal. Try html5 build.
My module for playfab:
-- Put functions in this file to use them in several other scripts.
-- To get access to the functions, you need to put:
-- require "my_directory.my_file"
-- in any script using the functions.
local PlayFabClientApi = require("PlayFab.PlayFabClientApi")
local IPlayFabHttps = require("PlayFab.IPlayFabHttps")
local PlayFabHttps_Defold = require("PlayFab.PlayFabHttps_Defold")
IPlayFabHttps.SetHttp(PlayFabHttps_Defold) -- Assign the Defold-specific IHttps wrapper
PlayFabClientApi.settings.titleId = "XXXX" -- Please change this value to your own titleId from PlayFab Game Manager
local M={}
local random = math.random
M.uid=""
M.nickname="Player"
M.login=false
M.PlayFabId=""
M.DisplayName=""
function M.uuid()
math.randomseed(os.time()+os.clock()+random(1,1000))
local a=random(1,10)+random(1,10)+random(1,10)
print (a)
local template ='xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
return string.gsub(template, '[xy]', function (c)
local v = (c == 'x') and random(0, 0xf) or random(8, 0xb)
return string.format('%x', v)
end)
end
function M.getPlayerProfile(PlayFabId, onSuccess, OnFailed)
PlayFabClientApi.GetPlayerProfile({PlayFabId=PlayFabId}, function(result)
pprint(result)
if result.PlayerProfile then M.DisplayName=result.PlayerProfile.DisplayName end
onSuccess(result)
end, OnFailed)
end
function M.sigIn(OnLoginSuccess, OnLoginFailed)
print("lb.sigIn:",M.login)
if M.login then
OnLoginSuccess()
return
end
local loginRequest = {
CustomId = M.uid,
CreateAccount = true
}
PlayFabClientApi.LoginWithCustomID(loginRequest, function(result)
M.login=true
pprint(result)
M.PlayFabId=result.PlayFabId
OnLoginSuccess(result)
end, OnLoginFailed)
end
function M.getLeaderboard(board, onSuccess, OnFailed, MaxResults, StartPosition)
PlayFabClientApi.GetLeaderboard({
StatisticName=board,
MaxResultsCount=MaxResults or 25,
StartPosition=StartPosition or 0
},onSuccess, OnFailed)
end
function M.getLeaderboardAroundPlayer(board, onSuccess, OnFailed, MaxResults)
PlayFabClientApi.GetLeaderboardAroundPlayer({
StatisticName=board,
MaxResultsCount=MaxResults or 10
},onSuccess, OnFailed)
end
function M.sendScore(value, onSuccess, onError)
PlayFabClientApi.UpdatePlayerStatistics({Statistics= {{StatisticName= "loot", Value= value },{StatisticName= "daily", Value= value }}}, onSuccess, onError)
end
function M.changeName(nickname, onSuccess, onError)
if M.login then
PlayFabClientApi.UpdateUserTitleDisplayName({DisplayName=nickname}, function(result)
print("New DisplayName:",result.DisplayName)
M.DisplayName=result.DisplayName
M.nickname=M.DisplayName
onSuccess(result)
end,
function(error) print(error.errorMessage)
onError(error)
end)
end
end
return M
Usage. First init() in the game code. I’m not login player in playfab before he made first result. So what I did there is generate uid for player.
local lb = require "main.leaderboard.lb"
....
--
game.loadFile()
--
if lb.uid=="" or lb.uid==nil then
lb.uid=lb.uuid()
print("UID=",lb.uid)
---- save uid
game.saveFile()
---
end
print("Nickname=",lb.nickname)
Gameover, send the score with sigIn player before that:
lb.sigIn(
function()
lb.sendScore(game.totalcoins,
onSendSuccess,
function(error) print(error.errorMessage) end
)
end,
function(error) print(error.errorMessage) end
)
.....
local function onLeaderboardSuccess(result)
print("Daily leaderboard:")
--pprint(result)
gui.set_enabled(gui.get_node("loading"),false)
local board=result.Leaderboard or {}
local nametext=""
local scoretext=""
for i=1, #board do
local dn=board[i].DisplayName or "noname"
local pos=tonumber(board[i].Position)+1
nametext=nametext..tostring(pos)..". "..dn.."\n"
local value=board[i].StatValue
scoretext=scoretext..value.."\n"
if board[i].PlayFabId==lb.PlayFabId then
gui.set_position(gui.get_node("me"), vmath.vector3(120, 369-(i-1)*27, 0))
gui.set_enabled(gui.get_node("me"), true)
end
end
gui.set_text(gui.get_node("name"), nametext)
gui.set_text(gui.get_node("score"), scoretext)
print(nametext,scoretext)
end
local function onSendSuccess(result)
print("score sent:",game.totalcoins)
flow.start(function()
print("flow.start")
flow.delay(.5)
print("flow.delay")
lb.getLeaderboardAroundPlayer("daily", onLeaderboardSuccess, onFailed, 6)
end)
end