-
-
Save JohannesLoot/20290272a91bb785459dd014b8845c8c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extends Node | |
| # Use this game API key if you want to test it with a functioning leaderboard | |
| # "987dbd0b9e5eb3749072acc47a210996eea9feb0" | |
| var game_API_key = "your-game-API-key-here" | |
| var development_mode = true | |
| var leaderboard_key = "leaderboardKey" | |
| var session_token = "" | |
| var score = 0 | |
| # 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() | |
| func _ready(): | |
| _authentication_request() | |
| func _process(_delta): | |
| if(Input.is_action_just_pressed("ui_up")): | |
| score += 1 | |
| print("CurrentScore:"+str(score)) | |
| if(Input.is_action_just_pressed("ui_down")): | |
| score -= 1 | |
| print("CurrentScore:"+str(score)) | |
| # Upload score when pressing enter | |
| if(Input.is_action_just_pressed("ui_accept")): | |
| _upload_score(score) | |
| # Get score when pressing spacebar | |
| if(Input.is_action_just_pressed("ui_select")): | |
| _get_leaderboards() | |
| func _authentication_request(): | |
| # Check if a player session has been saved | |
| var player_session_exists = false | |
| var file = File.new() | |
| file.open("user://LootLocker.data", File.READ) | |
| 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": true } | |
| # 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) | |
| func _on_authentication_request_completed(result, response_code, headers, body): | |
| var json = JSON.parse(body.get_string_from_utf8()) | |
| # Save player_identifier to file | |
| var file = File.new() | |
| file.open("user://LootLocker.data", File.WRITE) | |
| file.store_string(json.result.player_identifier) | |
| file.close() | |
| # Save session_token to memory | |
| session_token = json.result.session_token | |
| # Print server response | |
| print(json.result) | |
| # Clear node | |
| auth_http.queue_free() | |
| # Get leaderboards | |
| _get_leaderboards() | |
| 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, "") | |
| func _on_leaderboard_request_completed(result, response_code, headers, body): | |
| var json = JSON.parse(body.get_string_from_utf8()) | |
| # Print data | |
| print(json.result) | |
| # Formatting as a leaderboard | |
| var leaderboardFormatted = "" | |
| for n in json.result.items.size(): | |
| leaderboardFormatted += str(json.result.items[n].rank)+str(". ") | |
| leaderboardFormatted += str(json.result.items[n].player.id)+str(" - ") | |
| leaderboardFormatted += str(json.result.items[n].score)+str("\n") | |
| # Print the formatted leaderboard to the console | |
| print(leaderboardFormatted) | |
| # Clear node | |
| leaderboard_http.queue_free() | |
| func _upload_score(var score): | |
| var data = { "score": str(score) } | |
| 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(data) | |
| 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() |
@samtse: you got this error because most likely the request return no result at all, thus json.result is null.
Your screenshot unfortunately doesn't show the variables so it needs to be confirmed.
Remember when working with network: ALWAYS, always, check results, all the items from top to bottom, here for instance, json not null, then result not null AND a proper array, and so on...
The code you've used is not actually production ready as it is just a sample which need to be completed to work in ALL possible cases.
Thanks! I started off leaving the code unedited but will now make it my own
Thx again!
…
On Jun 30, 2024 at 9:25 PM, <Infini-Creation ***@***.***)> wrote:
@Infini-Creation commented on this gist.
@samtse (https://github.com/samtse): you got this error because most likely the request return no result at all, thus json.result is null.
Your screenshot unfortunately doesn't show the variables so it needs to be confirmed.
Remember when working with network: ALWAYS, always, check results, all the items from top to bottom, here for instance, json not null, then result not null AND a proper array, and so on...
The code you've used is not actually production ready as it is just a sample which need to be completed to work in ALL possible cases.
—
Reply to this email directly, view it on GitHub (https://gist.github.com/JohannesLoot/20290272a91bb785459dd014b8845c8c#gistcomment-5106752) or unsubscribe (https://github.com/notifications/unsubscribe-auth/AABLYL4MREVXZ73L6HU2CT3ZKBSVNBFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDUOJ2WLJDOMFWWLO3UNBZGKYLEL5YGC4TUNFRWS4DBNZ2F6YLDORUXM2LUPGBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVEYTCNRXGYYDKOJVU52HE2LHM5SXFJTDOJSWC5DF).
You are receiving this email because you were mentioned.
Triage notifications on the go with GitHub Mobile for iOS (https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675) or Android (https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub).
hi again.. I just had a closer look and json.result.size() = 2 so i tried treating itas an array which failed as result is notan array..
I can't find ou what it isfrom thedocseither!?
looking in here:
https://docs.godotengine.org/en/3.5/classes/class_httprequest.html#methods
there isn't even a connect method in the method list for httprequest despite it being in their example and in your code too??
do you know what json.result is...?
…On Jun 30 2024, at 9:28 PM, Jerome mayeux ***@***.***> wrote:
Thanks! I started off leaving the code unedited but will now make it my own
Thx again!
>
>
> On Jun 30, 2024 at 9:25 PM, <Infini-Creation ***@***.***)> wrote:
>
>
>
> @Infini-Creation commented on this gist.
>
>
>
> @samtse (https://github.com/samtse): you got this error because most likely the request return no result at all, thus
>
>
>
> json.result
>
>
>
> is
>
>
>
> null
>
>
>
> .
>
>
> Your screenshot unfortunately doesn't show the variables so it needs to be confirmed.
>
>
>
>
> Remember when working with network: ALWAYS, always, check results, all the items from top to bottom, here for instance,
>
>
>
> json
>
>
>
> not
>
>
>
> null
>
>
>
> , then
>
>
>
> result
>
>
>
> not
>
>
>
> null
>
>
>
> AND a proper array, and so on...
>
>
> The code you've used is not actually production ready as it is just a sample which need to be completed to work in ALL possible cases.
>
>
>
> —
>
> Reply to this email directly, view it on GitHub (https://gist.github.com/JohannesLoot/20290272a91bb785459dd014b8845c8c#gistcomment-5106752) or unsubscribe (https://github.com/notifications/unsubscribe-auth/AABLYL4MREVXZ73L6HU2CT3ZKBSVNBFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDUOJ2WLJDOMFWWLO3UNBZGKYLEL5YGC4TUNFRWS4DBNZ2F6YLDORUXM2LUPGBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVEYTCNRXGYYDKOJVU52HE2LHM5SXFJTDOJSWC5DF).
>
> You are receiving this email because you were mentioned.
>
>
> Triage notifications on the go with GitHub Mobile for iOS (https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675) or Android (https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub).
>
>
just looking more closely at your example ode and:
…On Jul 7 2024, at 5:10 PM, ***@***.*** wrote:
hi again.. I just had a closer look and json.result.size() = 2 so i tried treating itas an array which failed as result is notan array..
I can't find ou what it isfrom thedocseither!?
looking in here:
https://docs.godotengine.org/en/3.5/classes/class_httprequest.html#methods
there isn't even a connect method in the method list for httprequest despite it being in their example and in your code too??
do you know what json.result is...?
On Jun 30 2024, at 9:28 PM, Jerome mayeux ***@***.***> wrote:
>
> Thanks! I started off leaving the code unedited but will now make it my own
>
>
>
> Thx again!
>
>
>
>
>
>
>
> >
> >
> > On Jun 30, 2024 at 9:25 PM, <Infini-Creation ***@***.***)> wrote:
> >
> >
> >
> > @Infini-Creation commented on this gist.
> >
> >
> >
> > @samtse (https://github.com/samtse): you got this error because most likely the request return no result at all, thus
> >
> >
> >
> >
> >
> >
> > json.result
> >
> >
> >
> >
> >
> >
> > is
> >
> >
> >
> >
> >
> >
> > null
> >
> >
> >
> >
> >
> >
> > .
> >
> >
> > Your screenshot unfortunately doesn't show the variables so it needs to be confirmed.
> >
> >
> >
> >
> > Remember when working with network: ALWAYS, always, check results, all the items from top to bottom, here for instance,
> >
> >
> >
> >
> >
> >
> > json
> >
> >
> >
> >
> >
> >
> > not
> >
> >
> >
> >
> >
> >
> > null
> >
> >
> >
> >
> >
> >
> > , then
> >
> >
> >
> >
> >
> >
> > result
> >
> >
> >
> >
> >
> >
> > not
> >
> >
> >
> >
> >
> >
> > null
> >
> >
> >
> >
> >
> >
> > AND a proper array, and so on...
> >
> >
> > The code you've used is not actually production ready as it is just a sample which need to be completed to work in ALL possible cases.
> >
> >
> >
> > —
> >
> > Reply to this email directly, view it on GitHub (https://gist.github.com/JohannesLoot/20290272a91bb785459dd014b8845c8c#gistcomment-5106752) or unsubscribe (https://github.com/notifications/unsubscribe-auth/AABLYL4MREVXZ73L6HU2CT3ZKBSVNBFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDUOJ2WLJDOMFWWLO3UNBZGKYLEL5YGC4TUNFRWS4DBNZ2F6YLDORUXM2LUPGBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVEYTCNRXGYYDKOJVU52HE2LHM5SXFJTDOJSWC5DF).
> >
> > You are receiving this email because you were mentioned.
> >
> >
> > Triage notifications on the go with GitHub Mobile for iOS (https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675) or Android (https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub).
> >
> >
>
>
>
theresult. property contains this:
items:Null, pagination:{next_cursor:0, previous_cursor:Null, total:0}}
what does that mean has happened with the request ..?
…On Jul 7 2024, at 5:18 PM, ***@***.*** wrote:
just looking more closely at your example ode and:
On Jul 7 2024, at 5:10 PM, ***@***.*** wrote:
>
>
> hi again.. I just had a closer look and json.result.size() = 2 so i tried treating itas an array which failed as result is notan array..
>
> I can't find ou what it isfrom thedocseither!?
>
> looking in here:
>
> https://docs.godotengine.org/en/3.5/classes/class_httprequest.html#methods
>
>
> there isn't even a connect method in the method list for httprequest despite it being in their example and in your code too??
>
> do you know what json.result is...?
>
>
>
>
>
> On Jun 30 2024, at 9:28 PM, Jerome mayeux ***@***.***> wrote:
>
> >
> > Thanks! I started off leaving the code unedited but will now make it my own
> >
> >
> >
> > Thx again!
> >
> >
> >
> >
> >
> >
> >
> > >
> > >
> > > On Jun 30, 2024 at 9:25 PM, <Infini-Creation ***@***.***)> wrote:
> > >
> > >
> > >
> > > @Infini-Creation commented on this gist.
> > >
> > >
> > >
> > > @samtse (https://github.com/samtse): you got this error because most likely the request return no result at all, thus
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > > json.result
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > > is
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > > null
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > > .
> > >
> > >
> > > Your screenshot unfortunately doesn't show the variables so it needs to be confirmed.
> > >
> > >
> > >
> > >
> > > Remember when working with network: ALWAYS, always, check results, all the items from top to bottom, here for instance,
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > > json
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > > not
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > > null
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > > , then
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > > result
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > > not
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > > null
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > > AND a proper array, and so on...
> > >
> > >
> > > The code you've used is not actually production ready as it is just a sample which need to be completed to work in ALL possible cases.
> > >
> > >
> > >
> > > —
> > >
> > > Reply to this email directly, view it on GitHub (https://gist.github.com/JohannesLoot/20290272a91bb785459dd014b8845c8c#gistcomment-5106752) or unsubscribe (https://github.com/notifications/unsubscribe-auth/AABLYL4MREVXZ73L6HU2CT3ZKBSVNBFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDUOJ2WLJDOMFWWLO3UNBZGKYLEL5YGC4TUNFRWS4DBNZ2F6YLDORUXM2LUPGBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVEYTCNRXGYYDKOJVU52HE2LHM5SXFJTDOJSWC5DF).
> > >
> > > You are receiving this email because you were mentioned.
> > >
> > >
> > > Triage notifications on the go with GitHub Mobile for iOS (https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675) or Android (https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub).
> > >
> > >
> >
> >
> >
>
then i tried the test API key offered in you rcode var game_API_key="987dbd0b9e5eb3749072acc47a210996eea9feb0"
and got:
{message:leaderboard does not exist, request_id:f7a78a8b-244b-4875-843e-7dfbd30f2190, trace_id:27c769510afdd861f5c38f248ef0f73a}
are you able to help me get moving with lootlocker...?
…On Jul 7 2024, at 5:25 PM, ***@***.*** wrote:
theresult. property contains this:
items:Null, pagination:{next_cursor:0, previous_cursor:Null, total:0}}
what does that mean has happened with the request ..?
On Jul 7 2024, at 5:18 PM, ***@***.*** wrote:
>
>
> just looking more closely at your example ode and:
>
>
>
>
>
> On Jul 7 2024, at 5:10 PM, ***@***.*** wrote:
>
> >
> >
> > hi again.. I just had a closer look and json.result.size() = 2 so i tried treating itas an array which failed as result is notan array..
> >
> > I can't find ou what it isfrom thedocseither!?
> >
> > looking in here:
> >
> > https://docs.godotengine.org/en/3.5/classes/class_httprequest.html#methods
> >
> >
> > there isn't even a connect method in the method list for httprequest despite it being in their example and in your code too??
> >
> > do you know what json.result is...?
> >
> >
> >
> >
> >
> > On Jun 30 2024, at 9:28 PM, Jerome mayeux ***@***.***> wrote:
> >
> > >
> > > Thanks! I started off leaving the code unedited but will now make it my own
> > >
> > >
> > >
> > > Thx again!
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > > >
> > > >
> > > > On Jun 30, 2024 at 9:25 PM, <Infini-Creation ***@***.***)> wrote:
> > > >
> > > >
> > > >
> > > > @Infini-Creation commented on this gist.
> > > >
> > > >
> > > >
> > > > @samtse (https://github.com/samtse): you got this error because most likely the request return no result at all, thus
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > > json.result
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > > is
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > > null
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > > .
> > > >
> > > >
> > > > Your screenshot unfortunately doesn't show the variables so it needs to be confirmed.
> > > >
> > > >
> > > >
> > > >
> > > > Remember when working with network: ALWAYS, always, check results, all the items from top to bottom, here for instance,
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > > json
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > > not
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > > null
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > > , then
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > > result
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > > not
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > > null
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > > AND a proper array, and so on...
> > > >
> > > >
> > > > The code you've used is not actually production ready as it is just a sample which need to be completed to work in ALL possible cases.
> > > >
> > > >
> > > >
> > > > —
> > > >
> > > > Reply to this email directly, view it on GitHub (https://gist.github.com/JohannesLoot/20290272a91bb785459dd014b8845c8c#gistcomment-5106752) or unsubscribe (https://github.com/notifications/unsubscribe-auth/AABLYL4MREVXZ73L6HU2CT3ZKBSVNBFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDUOJ2WLJDOMFWWLO3UNBZGKYLEL5YGC4TUNFRWS4DBNZ2F6YLDORUXM2LUPGBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVEYTCNRXGYYDKOJVU52HE2LHM5SXFJTDOJSWC5DF).
> > > >
> > > > You are receiving this email because you were mentioned.
> > > >
> > > >
> > > > Triage notifications on the go with GitHub Mobile for iOS (https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675) or Android (https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub).
> > > >
> > > >
> > >
> > >
> > >
> >
>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

I just got it working on Godot Engine v4.1.3
Can't figure out how to PR my fork, so for now my working code is here:
https://gist.github.com/dudecon/48105f960a55eccd77ab0517d15d079f