|
| 1 | +# steamworks.getUserStorage |
| 2 | + |
| 3 | +> --------------------- ------------------------------------------------------------------------------------------ |
| 4 | +> **Type** [Function][api.type.Function] |
| 5 | +> **Return value** [Table][api.type.Table] or [nil][api.type.nil] |
| 6 | +> **Revision** [REVISION_LABEL](REVISION_URL) |
| 7 | +> **Keywords** steam, steamworks, getUserStorage, cloud storage, quota |
| 8 | +> **See also** [steamworks.*][plugin.steamworks], [steamworks.writeFile][plugin.steamworks.writeFile] |
| 9 | +> --------------------- ------------------------------------------------------------------------------------------ |
| 10 | +
|
| 11 | +## Overview |
| 12 | + |
| 13 | +This function retrieves information about the user's Steam Cloud storage quota. It returns a table containing total and available storage bytes, or `nil` if the operation failed. |
| 14 | + |
| 15 | +<div class="guide-notebox-imp"> |
| 16 | +<div class="notebox-title-imp">Important</div> |
| 17 | +This API is only available on 2025.3722+ |
| 18 | +</div> |
| 19 | + |
| 20 | +Steam Cloud storage has limits per application, and this function allows you to check how much storage space is available before writing large files. This is useful for managing save data and preventing storage-related errors. |
| 21 | + |
| 22 | +The function queries Steam's Remote Storage API to get the current quota information for the user's cloud storage. |
| 23 | + |
| 24 | +## Syntax |
| 25 | + |
| 26 | +```lua |
| 27 | +steamworks.getUserStorage() |
| 28 | +``` |
| 29 | + |
| 30 | +### Parameters |
| 31 | + |
| 32 | +None. |
| 33 | + |
| 34 | +### Return Value |
| 35 | + |
| 36 | +[Table][api.type.Table] or [nil][api.type.nil]. Returns a table with storage information if successful, or `nil` if the operation failed. |
| 37 | + |
| 38 | +The returned table contains the following fields: |
| 39 | +- **totalBytes** ([Number][api.type.Number]): Total cloud storage space allocated to the application in bytes |
| 40 | +- **availableBytes** ([Number][api.type.Number]): Remaining available storage space in bytes |
| 41 | + |
| 42 | +## Example |
| 43 | + |
| 44 | +```lua |
| 45 | +local steamworks = require( "plugin.steamworks" ) |
| 46 | + |
| 47 | +local function checkStorageSpace() |
| 48 | + local storageInfo = steamworks.getUserStorage() |
| 49 | + if storageInfo then |
| 50 | + local totalMB = storageInfo.totalBytes / (1024 * 1024) |
| 51 | + local availableMB = storageInfo.availableBytes / (1024 * 1024) |
| 52 | + local usedMB = totalMB - availableMB |
| 53 | + |
| 54 | + print("Steam Cloud Storage:") |
| 55 | + print("Total: " .. string.format("%.2f MB", totalMB)) |
| 56 | + print("Used: " .. string.format("%.2f MB", usedMB)) |
| 57 | + print("Available: " .. string.format("%.2f MB", availableMB)) |
| 58 | + |
| 59 | + -- Check if we have enough space for a save file |
| 60 | + local saveDataSize = 1024 -- Estimated save file size in bytes |
| 61 | + if storageInfo.availableBytes >= saveDataSize then |
| 62 | + print("Sufficient storage space available") |
| 63 | + return true |
| 64 | + else |
| 65 | + print("Warning: Low storage space!") |
| 66 | + return false |
| 67 | + end |
| 68 | + else |
| 69 | + print("Failed to retrieve storage information.") |
| 70 | + return false |
| 71 | + end |
| 72 | +end |
| 73 | + |
| 74 | +-- Before saving large data, check available space |
| 75 | +local function safeSavePlayerData(saveData) |
| 76 | + local storageInfo = steamworks.getUserStorage() |
| 77 | + if not storageInfo then |
| 78 | + print("Cannot check storage - proceeding anyway") |
| 79 | + else |
| 80 | + local jsonData = json.encode(saveData) |
| 81 | + local dataSize = string.len(jsonData) |
| 82 | + |
| 83 | + if storageInfo.availableBytes < dataSize then |
| 84 | + print("Error: Not enough cloud storage space!") |
| 85 | + print("Need: " .. dataSize .. " bytes") |
| 86 | + print("Available: " .. storageInfo.availableBytes .. " bytes") |
| 87 | + return false |
| 88 | + end |
| 89 | + end |
| 90 | + |
| 91 | + -- Proceed with save |
| 92 | + return steamworks.writeFile("playerData.json", json.encode(saveData)) |
| 93 | +end |
| 94 | +``` |
0 commit comments