Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

LOOTCREATE FOR GODOT 3 SCRIPT

A topic by lurgx created 70 days ago Views: 68 Replies: 3
Viewing posts 1 to 3
Submitted

Hi everyone, this is the code I use to connect my game to the leaderboard of my lootcreate account. I hope it helps those who use Godot 3 and gives an idea to those who use other engines.


extends Node2D

# Use this game API key if you want to test it with a functioning leaderboard

var game_API_key = "your_apidev key"#dev

var development_mode = true

var leaderboard_key = "name of your leaderboard" #dev

var session_token = "your token leaderboard"

var score = gl.score#autoload

# HTTP Request node can only handle one call per node

var auth_http = HTTPRequest.new()

var leaderboard_http = HTTPRequest.new()

var submit_score_http = HTTPRequest.new()

var set_name_http = HTTPRequest.new()

var get_name_http = HTTPRequest.new()

func _ready():

_authentication_request()

func _process(_delta):

if(Input.is_action_just_pressed("ui_accept")):

gl.score = 1000

_upload_score(gl.score)


if(Input.is_action_just_pressed("ui_up")):

_change_player_name()


func _authentication_request():

# Check if a player session has been saved

var player_session_exists = false

var file = File.new()

var player_identifier 

if file.file_exists("user://LootLocker.data"):

file.open("user://LootLocker.data", File.READ)

player_identifier = file.get_as_text()

# var player_identifier = file.get_as_text()

file.close()

else:

file.open("user://LootLocker.data", File.WRITE)

player_identifier = file.get_as_text()

# var player_identifier = file.get_as_text()

file.close()

if(player_identifier.length() > 1):

player_session_exists = true

## Convert data to json string:

var data = { "game_key": game_API_key, "game_version": "0.0.0.1", "development_mode": development_mode }

# If a player session already exists, send with the player identifier

if(player_session_exists == true):

data = { "game_key": game_API_key, "player_identifier":player_identifier, "game_version": "0.0.0.1", "development_mode": true }

# Add 'Content-Type' header:

var headers = ["Content-Type: application/json"]

# Create a HTTPRequest node for authentication

auth_http = HTTPRequest.new()

add_child(auth_http)

auth_http.connect("request_completed", self, "_on_authentication_request_completed")

# Send request

auth_http.request("https://api.lootlocker.io/game/v2/session/guest", headers, true, HTTPClient.METHOD_POST, to_json(data))

# Print what we're sending, for debugging purposes:

# print("DATA : ",data)

# warning-ignore:unused_argument

# warning-ignore:unused_argument

# warning-ignore:unused_argument

func _on_authentication_request_completed(result, response_code, headers, body):

var json = JSON.parse(body.get_string_from_utf8())

if response_code == 200:

# Guarda player_identifier y session_token solo si la autenticación fue exitosa

var file = File.new()

file.open("user://LootLocker.data", File.WRITE)

file.store_string(json.result.player_identifier)

file.close()

# Guarda el session_token en la memoria para futuras solicitudes

session_token = json.result.session_token

print("Sesión iniciada. Token: ", session_token)

else:

print("Error en la autenticación: ", response_code, " - ", json.result)

auth_http.queue_free()


print("JSON RE:",json.result)

func _get_leaderboards():

print("Getting leaderboards")

var url = "https://api.lootlocker.io/game/leaderboards/"+leaderboard_key+"/list?count=10"

var headers = ["Content-Type: application/json", "x-session-token:"+session_token]

# Create a request node for getting the highscore

leaderboard_http = HTTPRequest.new()

add_child(leaderboard_http)

leaderboard_http.connect("request_completed", self, "_on_leaderboard_request_completed")

# Send request

leaderboard_http.request(url, headers, true, HTTPClient.METHOD_GET, "")

# warning-ignore:unused_argument

# warning-ignore:unused_argument

# warning-ignore:unused_argument

func _on_leaderboard_request_completed(result, response_code, headers, body):#SCORE AND COUNTRY

var json = JSON.parse(body.get_string_from_utf8())

# Print data

print("boardresult: ",json.result)

# Formatting as a leaderboard

var leaderboardFormatted = ""

for n in json.result.items.size():

leaderboardFormatted += str(json.result.items[n].metadata)+str("-")

leaderboardFormatted += str(json.result.items[n].player.name)+str(" id - ")

leaderboardFormatted += str(json.result.items[n].player.id)+str(" - SC:")

leaderboardFormatted += str(json.result.items[n].score)+str(" ")

$hi.text = str("BEST SCORES ONLINE \n",leaderboardFormatted)

$send.visible = false

$name.visible = false

print(leaderboardFormatted)

# Clear node

leaderboard_http.queue_free()

# warning-ignore:shadowed_variable

# warning-ignore:unused_argument

func _upload_score(var score):

gl.name_pl = str($name.text)

# var data = { 

# "name":str("master"),

# "score": str(gl.hi),

# "rank":null,

# "metadata":str(gl.country," - ")

#  }

var data = { 

"player_name":str("master"),

"score": str(gl.hi),

"rank":null,

"metadata":str(gl.country," - ")

}

print("score send")

var headers = ["Content-Type: application/json", "x-session-token:"+session_token]

submit_score_http = HTTPRequest.new()

add_child(submit_score_http)

submit_score_http.connect("request_completed", self, "_on_upload_score_request_completed")

# Send request

submit_score_http.request("https://api.lootlocker.io/game/leaderboards/"+leaderboard_key+"/submit", headers, true, HTTPClient.METHOD_POST, to_json(data))

# Print what we're sending, for debugging purposes:

print("SEND: ",data)

_change_player_name()

# warning-ignore:unused_argument

# warning-ignore:unused_argument

func _on_upload_score_request_completed(result, response_code, headers, body) :

var json = JSON.parse(body.get_string_from_utf8())

# Print data

print(json.result)

# Clear node

submit_score_http.queue_free()

_get_leaderboards()

func _change_player_name():

print("Changing player name")

# use this variable for setting the name of the player

var player_n = gl.name_pl

# var data = { "name": str(gl.name_pl) }

var data = { "name": player_n }

var url =  "https://api.lootlocker.io/game/player/name"

var headers = ["Content-Type: application/json", "x-session-token:"+session_token]

# Create a request node for getting the highscore

set_name_http = HTTPRequest.new()

add_child(set_name_http)

set_name_http.connect("request_completed",self,"_on_player_set_name_request_completed")

# set_name_http.connect("request_completed",self,"_on_leaderboard_request_completed2")

# Send request

# set_name_http.request("https://htjw9peu.api.lootlocker.io/game/player_name", headers,true, HTTPClient.METHOD_PATCH, to_json(data))

set_name_http.request("https://api.lootlocker.io/game/player/name", headers,true, HTTPClient.METHOD_PATCH, to_json(data))

func _on_player_set_name_request_completed(result, response_code, headers, body):

# var json = JSON.new()

# json.parse(body.get_string_from_utf8())

#

# # Print data

# print("named ",json.get_data())

# set_name_http.queue_free()

var json = JSON.parse(body.get_string_from_utf8())

if response_code == 200:

print("Nombre cambiado exitosamente a: ", json.result.name)

else:

print("Error al cambiar el nombre: ", response_code, " - ", json.result)

set_name_http.queue_free()

func _get_only():

print("Getting leaderboards")

var url = "https://api.lootlocker.io/game/leaderboards/"+leaderboard_key+"/list?count=10"

var headers = ["Content-Type: application/json", "x-session-token:"+session_token]

# Create a request node for getting the highscore

leaderboard_http = HTTPRequest.new()

add_child(leaderboard_http)

leaderboard_http.connect("request_completed", self, "_check")

# Send request

leaderboard_http.request(url, headers, true, HTTPClient.METHOD_GET, "")


func _check(result, response_code, headers, body):

var json = JSON.parse(body.get_string_from_utf8())

# Print data

print("JS",json.result)

# Formatting as a leaderboard

# warning-ignore:unused_variable

var leaderboardFormatted = ""

for n in json.result.items.size():

leaderboardFormatted += str(json.result.items[n].rank)+str(". ")

leaderboardFormatted += str(json.result.items[n].metadata)+str("-")

leaderboardFormatted += str(json.result.items[n].player.id)+str(" - ")

leaderboardFormatted += str(json.result.items[n].score)+str("\n")

if json.result.items[n].score !=0 and n<9:

gl.online +=[json.result.items[n].score]

# Print the formatted leaderboard to the console

print(gl.online)

# print(leaderboardFormatted)

# Clear node

leaderboard_http.queue_free()

if gl.hi > gl.online.max():

self.visible = true

func _on_send_pressed():

_upload_score(gl.hi)

$hi.visible = true

func _on_leader_pressed():

_get_leaderboards()

$hi.visible = true


I hope it helps you because for Godot 3 the lootcreate tutorial is for Godot 4 and there are differences in how to read a json in the tutorial that will give an error in Godot 3. Greetings!

Submitted

Man, thank you so much for this! 

I did my scoreboard in silentwolf plugin. 🥹

Submitted

thankyou for look this post grettings!

Submitted
Deleted 58 days ago
Submitted
Deleted 58 days ago
Submitted

Nothing has happened in my game yet, I don't know what it is due to but it seems very frustrating, maybe it is due to different parameters when used in html

Submitted
Deleted 58 days ago