Advertisement
Guest User

Untitled

a guest
Oct 5th, 2023
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 7.41 KB | Gaming | 0 0
  1. extends Node
  2.  
  3. var appId = 0
  4.  
  5. const LVLS_PER_PAGE = 10.0
  6. const STEAM_LVLS_PER_PAGE = 50.0
  7.  
  8. var is_initialized :bool= false
  9.  
  10. var user_id : int
  11.  
  12. var current_item_id
  13. var current_file_id
  14. var current_ugc_query_handler_id:int=-1
  15. var current_loading_level_id:int=-1
  16.  
  17.  
  18. func _ready():
  19.     Steam.connect("steamworks_error", self, "_log_error", [], CONNECT_PERSIST)
  20.     Steam.connect("current_stats_received", self, "_steam_Stats_Ready", [], CONNECT_PERSIST)
  21.     Steam.connect("item_created", self, "_on_item_created", [], CONNECT_PERSIST)
  22.     Steam.connect("item_updated", self, "_on_item_updated", [], CONNECT_PERSIST)
  23.     Steam.connect("ugc_query_completed", self, "_on_ugc_query_completed", [], CONNECT_PERSIST)
  24.     Steam.connect("item_downloaded", self, "_on_item_downloaded", [], CONNECT_PERSIST)
  25.     Steam.steamInit()
  26.     Steam.requestCurrentStats()
  27.  
  28.  
  29. func _process(delta):
  30.     Steam.run_callbacks()
  31.  
  32.  
  33. func _log_error(err_signal:String, err_msg:String):
  34.     print_debug("Error with signal: %s" % err_signal)
  35.     print_debug(err_msg)
  36.  
  37.  
  38. func checkSteam() -> bool:
  39.     if !Steam.isSteamRunning():
  40.         print_debug("Steam is not running")
  41.         return false
  42.     if !Steam.isSubscribed():
  43.         print_debug("Not subscribed / Ownership is not confirmed")
  44.         return false
  45.     return true
  46.  
  47.  
  48. func _steam_Stats_Ready(game: int, result: int, user: int) -> void:
  49.     appId = game
  50.     is_initialized = true
  51.  
  52.  
  53. func unlock_achievement(achievent_name):
  54.     if !checkSteam():
  55.         return
  56.     Steam.setAchievement(achievent_name)
  57.     Steam.storeStats()
  58.  
  59.  
  60. func get_workshop_levels(page :int= 1, filters :Array= []):
  61.     if current_ugc_query_handler_id > 0:
  62.         # There is already a query in the works, so will just release its handler because we need the new one to work its way.
  63.         Steam.releaseQueryUGCRequest(current_ugc_query_handler_id)
  64.     # This is a quick way to convert YOUR pages into STEAM pages. Steam SDK does not allow you to specify the number of items per page, it always returns up to 50 results. Here, LVLS_PER_PAGE = 10 and STEAM_LVLS_PER_PAGE = 50.
  65.     var steam_page = ceil(page * LVLS_PER_PAGE / STEAM_LVLS_PER_PAGE)
  66.     # Creating a query with type 0 - ordered by upvotes for all time.
  67.     current_ugc_query_handler_id = Steam.createQueryAllUGCRequest(0, 0, appId, appId, steam_page)
  68.     # Add filters. In my case, I am exluding tags. filters is just an array of strings.
  69.     for filter in filters:
  70.         Steam.addExcludedTag(current_ugc_query_handler_id, filter)
  71.     # Reduce the number of server calls by relying on local cache (managed by SteamSDK)
  72.     Steam.setAllowCachedResponse(current_ugc_query_handler_id, 30)
  73.     # Finally, send the query
  74.     Steam.sendQueryUGCRequest(current_ugc_query_handler_id)
  75.  
  76.  
  77. func download_level(lvl_id):
  78.     if current_loading_level_id==lvl_id:
  79.         # This means that we are already downloading this item. Probably just a second click on a button.
  80.         return false
  81.     elif current_loading_level_id>0 and current_loading_level_id!=lvl_id:
  82.         # We are already downloading another level. I allow this situations to happen but you might decide otherwise.
  83.         pass
  84.     # This function return bool, true if download request was successfully sent
  85.     if Steam.downloadItem(lvl_id, false):
  86.         current_loading_level_id = lvl_id
  87.         # Here you can block user input to prevent send clicks a launch loading animation.
  88.  
  89.  
  90. func upload_level(lvl_id):
  91.     current_item_id = lvl_id
  92.     Steam.createItem(appId, 0)
  93.  
  94.  
  95. func upvote_level(file_id:int):
  96.     if !checkSteam():
  97.         return false
  98.     Steam.setUserItemVote(file_id, true)
  99.  
  100.  
  101. func downvote_level(file_id:int):
  102.     if !checkSteam():
  103.         return false
  104.     Steam.setUserItemVote(file_id, false)
  105.  
  106.  
  107. func open_item_page(file_id):
  108.     Steam.activateGameOverlayToWebPage("steam://url/CommunityFilePage/%s" % str(file_id))
  109.  
  110.  
  111. func _on_item_created(result: int, file_id: int, accept_tos: bool):
  112.     var handler_id = Steam.startItemUpdate(appId, file_id)
  113.     var lvl_id = current_item_id
  114.     var lvl_path = "ABSOLUTE\PATH\TO\ITEM\FOLDER"
  115.     # Access metadata file and read the level title and tags from it
  116.     var metadata:ConfigFile=ConfigFile.new()
  117.     metadata.load("PATH\TO\METADATA\FILE")
  118.     var lvl_title = metadata.get_value("main", "title", "")
  119.     var lvl_tags = []
  120.     # Saving file_id into the metadata file so I can update this item later if needed
  121.     metadata.set_value("steam", "file_id", file_id)
  122.     metadata.save("PATH\TO\METADATA\FILE")
  123.     for tag in ["LIST", "OF", "YOUR", "TAGS"]:
  124.         if metadata.get_value("main", tag, false):
  125.             lvl_tags.append(tag)
  126.     # Setting UGC item title - it will appear in the workshop
  127.     Steam.setItemTitle(handler_id, lvl_title)
  128.     # Setting the path to directory containing the item files
  129.     Steam.setItemContent(handler_id, lvl_path)
  130.     # Setting UGC item tags - they will be visible in the workshop
  131.     Steam.setItemTags(handler_id, lvl_tags)
  132.     # Attaching a preview file is an optional step. The preview file is just a .png image. For example, you can take a screenshot in Godot and save it as file.
  133.     Steam.setItemPreview(handler_id, "ABSOLUTE\PATH\TO\PREVIEW\FILE.PNG")
  134.     # Making item visible in the workshop
  135.     Steam.setItemVisibility(handler_id, 0)
  136.     # Adding workshop metadata that is not visible via web interface. For example, I store the version of the editor.
  137.     Steam.setItemMetadata(handler_id, "OPTIONAL METADATA STRING")
  138.     # Submit item update - Steam will now upload the data
  139.     Steam.submitItemUpdate(handler_id, "CHANGE NOTE")
  140.     current_item_id = null
  141.     current_file_id = file_id
  142.     # You will need this file_id if you wish to monitor the progress
  143.  
  144.  
  145. func _on_item_updated(result: int, accept_tos):
  146.     var item_page_template = "steam://url/CommunityFilePage/%s"
  147.     if accept_tos:
  148.         Steam.activateGameOverlayToWebPage(item_page_template % str(current_file_id))
  149.  
  150.  
  151. func _on_ugc_query_completed(handle:int, result:int, results_returned:int, total_matching:int, cached:bool):
  152.     # If the current handler id changed - it means that we requested a list of items again, so we can dismiss these results.
  153.     if handle != current_ugc_query_handler_id:
  154.         Steam.releaseQueryUGCRequest(handle)
  155.         return
  156.     if result != 1:
  157.         # The query failed. See steam result codes for possible reasons.
  158.         return
  159.     var list_of_results = []
  160.     for item_id in range(results_returned):
  161.         # Get information for each item and (optional) metadata
  162.         var item = Steam.getQueryUGCResult(handle, item_id)
  163.         var md = Steam.getQueryUGCMetadata(handle, item_id)
  164.         item["metadata"] = md
  165.         list_of_results.append(item)
  166.     # Release the query handle to free memory
  167.     Steam.releaseQueryUGCRequest(handle)
  168.     current_ugc_query_handler_id = 0
  169.     # Now we can show the list of results to the player
  170.  
  171.  
  172. func _on_item_downloaded(result, file_id, app_id):
  173.     if result != 1:
  174.         # See steam result codes for more details
  175.         print_debug("Download failed %d" % result)
  176.     if file_id != current_loading_level_id:
  177.         # We are expecting another file to download, so we will just skip this one. You can of course allow multiple parallel downloads in you game, if you wish.
  178.         return
  179.     # Getting the information about the downloaded item
  180.     var lvl_info = Steam.getItemInstallInfo(file_id)
  181.     # lvl_info["folder"] will contain a folder with item files
  182.     # In my case, these are level.tscn and level.data
  183.     # Reset the global variable to allow new downloads to happen
  184.     current_loading_level_id = -1
  185.     # You can stop tracking for all items just in case there is still an item in use
  186.     Steam.stopPlaytimeTrackingForAllItems()
  187.     # Start tracking playtime for the downloaded item
  188.     Steam.startPlaytimeTracking([file_id])
  189.     # Lauch the downloaded level
  190.     SomeFunctionToLauchTheLevel(lvl_info["folder"])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement