Si vous avez des idées d'amélioration, écrivez-moi à telegram / Issues / pull, je lirai tout
Mon télégramme @Alexey_Pe
Le jeu dans lequel j'ai utilisé cet addon : Match 3 puzzle, le jeu propose saving/loading
, rewarded/fullscreen advertising
, purchases
, call for feedback
Démo dans le dossier addons/YandexGamesSDK/Demo
Contient du code simple avec des commentaires
Cet addon est utilisé par de nombreux développeurs, j'ai ajouté ceux que ça ne dérange pas (écrivez-moi pour vous ajouter)
№ | Promoteur | Joueurs |
---|---|---|
1 | Jeux RA | 1 500 000 + |
2 | Magikelle Studio Yandex | 1000+ |
3 | Studio Légatum | 1000+ |
initGame()
, getPlayer(false)
, getPayments()
, getLeaderboards()
< script src = "https://yandex.ru/games/sdk/v2" > < / script >
< script >
function get_url ( js , size ) {
return js ( size ) ;
}
< / script>
Important
Lorsque le jeu est prêt à être montré au joueur, appelez YandexGames.ready()
- par exemple, après avoir chargé la sauvegarde getData()
, vous pouvez appeler cette fonction.
func on_initGame ():
# Here you can download the player's data, and then confirm that the game is ready to be shown
YandexGames . ready () # inform Yandex that the player has uploaded
func _ready ():
YandexGames . connect ( "on_initGame" , self , "on_initGame" ) # Connect to the sdk initialization signal
if YandexGames . is_initGame : on_initGame () # If for some reason you connected after initialization
# Interstitial Ads, signal on_showFullscreenAdv(wasShown:bool, ad_name:String)
# ad_name - this argument is not passed to yandex. I added it for signal and match
if YandexGames . current_fullscreen_ad_name == "" :
YandexGames . showFullscreenAdv ( "ad_name" )
YandexGames . connect ( "on_showFullscreenAdv" , self , "on_showFullscreenAdv" )
# example use signal
func on_showFullscreenAdv ( success : bool , ad_name : String ):
match ad_name :
"advertising after loading the game" :
# your some code when the player closed this ad
if success : pass
else : pass
# Rewarded Ads, signal on_showRewardedVideo(success:bool, current_rewarded_ad_name:String)
# ad_name - this argument is not passed to yandex. I added it for signal and match
if YandexGames . current_rewarded_ad_name == "" :
YandexGames . showRewardedVideo ( ad_name )
YandexGames . connect ( "on_showRewardedVideo" , self , "on_showRewardedVideo" )
# example use signal
func on_showRewardedVideo ( success : bool , ad_name : String ):
match ad_name :
"open chest for advertising" :
# your some code when the player closed this ad
if success : pass
else : pass
# setData(data:Dictionary) to save data on yandex, no signal
YandexGames . setData ( data )
# getData() and getData_yield()->Dictionary, signal on_getData(data:Dictionary)
var load_save : Dictionary = yield ( YandexGames . getData_yield (), "completed" )
# choose the use case that you like best
getData ()
YandexGames . connect ( "on_getData" , self , "on_getData" )
# Create DataManager.gd signleton
var can_load_data : bool = false
func _ready ():
YandexGames . connect ( "on_getPlayer" , self , "on_getPlayer" )
func on_getPlayer ( player ):
can_load_data = true
loadData ()
# Example call: a player has entered the game and does not have data, you need to get the data
# loadData is a yield function, when you call it you want to write it like this: yield(DataManager.loadData_yield(self), "completed")
# Why do you need to write yield: this means that you stop executing the code until the Yandex server sends you the data
func loadData_yield ( from : Object ):
if YandexGames . js_ysdk_player == null : print ( "DataManager.gd loadData(from: %s ) js_ysdk_player == null" % [ from ]); return
if can_load_data : print ( "DataManager.gd loadData(from: %s )" % [ from ]); return
else : print ( "DataManager.gd loadData(from: %s ) !can_load_data" % [ from ])
var load_save : Dictionary = yield ( YandexGames . getData_yield (), "completed" )
if load_save . empty (): pass # player logged in for the first time / there were no saves
# here you use the downloaded data from load_save
pass
# Example call: a player upgraded an item by spending currency, you want to save it
func saveData ( from : Object ):
print ( "DataManager.gd saveData(from: %s )" % [ from ])
if YandexGames . js_ysdk_player == null : print ( "DataManager.gd saveData(from: %s ) js_ysdk_player == null" % [ from ]); return
# setData - overwrites a variable on the yandex server
# Create a dictionary in which you write down all the data that you need to save
# The dictionary will be converted to json, check that your data does not contain classes, you need to convert classes into data that can be stored in json
var data : Dictionary = { "gold" : 100 , "inventory" : "your_value" , "etc" : 123 }
YandexGames . setData ( data )
pass
# example 1
var flags = yield ( YandexGames . getFlags_yield (), "completed" )
if flags is Dictionary : pass # you code here
# example 2
var flags = yield ( YandexGames . getFlags_yield ([
{ "name" : "var_1" , "value" : "20" },
{ "name" : "var_2" , "value" : "33" },
]), "completed" )
if flags is Dictionary : pass # you code here
# getFlags_yield() uses the on_getFlags signal
YandexGames . connect ( "on_getFlags" , self , "on_getFlags" )
func on_getFlags ( response ):
if response is Dictionary : pass # flags
else : pass # js error (the error will be printed in the browser console)
on_purchase_then()
uniquement lorsque le joueur ferme la fenêtre, gardez cela à l'esprit. Liens vers la vidéo 1 et la vidéo 2 (bugs)on_purchase_then()
- vous devez utiliser getPurchases()
lors du démarrage du jeu. # In-game purchase, signals on_purchase_then(), on_purchase_catch()
YandexGames . purchase ( "id" )
YandexGames . connect ( "on_purchase_then" , self , "on_purchase_then" )
YandexGames . connect ( "on_purchase_catch" , self , "on_purchase_catch" )
# example use signals
func on_purchase_then ( purchase : YandexGames . Purchase ):
match purchase . product_id :
"id which you specified in the console" :
# your code
# purchase.consume() - delete a purchase from the getPurchases array
# In short, when a player buys, he must close the window to call on_purchase_then
# Moderators make a purchase without closing the window and refresh the page, which is why on_purchase_then is not called
purchase . consume ()
func on_purchase_then ( product_id : String ):
match product_id :
"1" : pass
# Getting the purchases array [YandexGames.Purchase], signals on_getPurchases()
# The array contains only those Purchases for which consume() was not called
YandexGames . getPurchases ()
YandexGames . connect ( "on_getPurchases" , self , "on_getPurchases" )
# example use signals
# purchases = Array [YandexGames.Purchase] or null
func on_getPurchases ( purchases ):
if purchases == null : pass # your variant
for purchase in purchases :
match purchase . product_id :
"id which you specified in the console" :
# your code
# check the comment on_purchase_then()
purchase . consume ()
# YandexGames.gd
class Purchase :
# product_id from Yandex Console (You specify it yourself)
var product_id : String
var _js_purchase : JavaScriptObject
func _init ( __js_purchase : JavaScriptObject ):
product_id = __js_purchase . productID
_js_purchase = __js_purchase
# consume() for to remove from array YandexGames.getPurchases() (on the Yandex side)
func consume ():
YandexGames . js_ysdk_payments . consumePurchase ( _js_purchase . purchaseToken )
Pour un classement, vous devez avoir un classement (ou plusieurs) créé dans votre brouillon.
Vous pouvez également sélectionner le classement principal dans les paramètres (« par défaut » (bool) dans la réponse)
Lien vers la documentation du classement
# setLeaderboardScore(Leaderboard_name:String, score:int)
YandexGames . setLeaderboardScore ( "Leaderboard_name" , 1000 )
# getLeaderboardDescription(leaderboardName:String), signal on_getLeaderboardDescription(description:Dictionary)
# description:{appID:String, default:bool, invert_sort_order:bool, decimal_offset:int, type:String, name:String, title:{en:String, ru:String ..}}
YandexGames . getLeaderboardDescription ( "Leaderboard_name" )
# getLeaderboardPlayerEntry(leaderboardName:String)
# signals on_getLeaderboardPlayerEntry_then(response:Dictionary), on_getLeaderboardPlayerEntry_catch(err_code:String)
# response { score:int, extraData:String, rank:int, getAvatarSrc:JavaScriptObject, getAvatarSrcSet:JavaScriptObject, lang:String, publicName:String, uniqueID:String, scopePermissions_avatar:String, scopePermissions_public_name:String, formattedScore:String}
# response, getAvatarSrc and getAvatarSrcSet - is JavaScriptObject(Function maybe?) return String. I didn't understand how to use it
YandexGames . getLeaderboardPlayerEntry ( "Leaderboard_name" )
YandexGames . connect ( "on_getLeaderboardPlayerEntry_then" , self , "on_getLeaderboardPlayerEntry_then" )
YandexGames . connect ( "on_getLeaderboardPlayerEntry_catch" , self , "on_getLeaderboardPlayerEntry_catch" )
# getLeaderboardEntries(leaderboardName:String), signal on_getLeaderboardEntries(response:Dictionary)
# response { leaderboard:Dictionary, userRank:int, entries:Array}
# leaderboard - getLeaderboardDescription response
# entries - top 20 and 10-20(up/down) players around the player who requested. entries element - getLeaderboardPlayerEntry response
YandexGames . getLeaderboardEntries ( "Leaderboard_name" )
YandexGames . connect ( "on_getLeaderboardEntries" , self , "on_getLeaderboardEntries" )
# example use
func on_getLeaderboardEntries ( response : Dictionary ):
match response [ "leaderboard" ][ "name" ]:
"best_score" : pass
"best_speed" : pass
"aaa" : pass
# canReview() and on_canReview_yield()->bool, signal on_canReview(can_review:bool)
var can_review = yield ( YandexGames . on_canReview_yield (), "completed" )
# choose the use case that you like best
YandexGames . canReview ()
YandexGames . connect ( "on_canReview" , self , "on_canReview" )
# requestReview() automatically calls canReview(), signal on_requestReview(requestReview:bool)
YandexGames . requestReview ()
YandexGames . connect ( "on_requestReview" , self , "on_requestReview" )
# canShowPrompt() and canShowPrompt_yield()->bool, signal on_canShowPrompt(canShowPrompt:bool)
var can_show_prompt = yield ( YandexGames . canShowPrompt_yield (), "completed" )
# choose the use case that you like best
YandexGames . canShowPrompt ()
YandexGames . connect ( "on_canShowPrompt" , self , "on_canShowPrompt" )
# showPrompt() automatically calls canShowPrompt(), signal on_showPrompt(accepted:bool)
YandexGames . on_showPrompt ()
YandexGames . connect ( "on_showPrompt" , self , "on_showPrompt" )
# getLang() -> String (ru/en/tr/...)
YandexGames . getLang ()
# Notifies Yandex that the game is ready
YandexGames . ready ()
# Device Info
match YandexGames . js_ysdk . deviceInfo [ "_type" ]:
"desktop" : pass
"mobile" : pass
"tablet" : pass
"tv" : pass