The Godot Barn
Sign in
Sign in
Home
News & updates
Explore
Articles
Snippets
Shaders
Themes
Submit content
Sign in to submit content
Give or get help
Tutorials
Questions
Conversations
Coming soon!
GodotSteam Tutorials - Avatars
1
Description
"You may want to get the current player's avatar, or someone else's, and display it in your game. This tutorial will walk you through the basics of avatar retrieval and passing it to a sprite so you can use it.\r\n\r\n??? guide Relevant GodotSteam classes and functions\r\n* [Friends class](https:\/\/godotsteam.com\/classes\/friends)\r\n\t* [getPlayerAvatar](https:\/\/godotsteam.com\/classes\/friends#getplayeravatar)\r\n\t* [requestUserInformation](https:\/\/godotsteam.com\/classes\/friends\/#requestuserinformation)\r\n???\r\n\r\n## Request the Avatar{ .block }\r\n\r\nThe default Steamworks functions to get player avatars requires multiple steps. However, GodotSteam has a unique function which makes getting the avatar data incredibly easy. Just use the following:\r\n\r\n```gdscript\r\nSteam.getPlayerAvatar()\r\n```\r\n\r\nThis function has optional parameters: **size** and **steam_id**. By default, it will get medium-sized (64x64) avatar data for the current player. However, you may pass any of the following for the first argument to get different sizes:\r\n\r\n- Steam.AVATAR_SMALL (32x32)\r\n- Steam.AVATAR_MEDIUM (64x64)\r\n- Steam.AVATAR_LARGE (128x128 or larger)\r\n\r\nThe numerical counterparts to these enums (1, 2, or 3) will also work.\r\n\r\nFor the second argument, if you pass nothing then it will pull the avatar for the local player. You can pass along the Steam ID64 of any player to get their avatar data instead.\r\n\r\n!!! warning Note\r\nThis only works for users that the local user knows about. They will automatically know about their friends, people on leaderboards they've requested, or people in the same source as them (Steam group, chat room, lobby, or game server). If they don't know about them then you must call [requestUserInformation()](https:\/\/godotsteam.com\/classes\/friends\/#requestuserinformation) to cache the avatar locally.\r\n!!!\r\n\r\n## Retrieve and Create the Image{ .block }\r\n\r\nTo retrieve the avatar data buffer, you'll need to hook the signal for the callback:\r\n\r\n::: tabs\r\n@tab:active Godot 2.x, 3.x\r\n```gdscript\r\nSteam.connect(\"avatar_loaded\", self, \"_on_loaded_avatar\")\r\n```\r\n\r\n@tab Godot 4.x\r\n```gdscript\r\nSteam.avatar_loaded.connect(_on_loaded_avatar)\r\n```\r\n:::\r\n\r\nThis will return the user's Steam ID, the avatar's size, and the data buffer for rendering the image. If you have read the [Achievement Icons tutorial](https:\/\/godotsteam.com\/tutorials\/achievement_icons), this should all look pretty familiar. Our **\\_on_loaded_avatar()** function will look something like this:\r\n\r\n::: tabs\r\n@tab:active Godot 2.x, 3.x\r\n```gdscript\r\nfunc _on_loaded_avatar(user_id: int, avatar_size: int, avatar_buffer: PoolByteArray) -> void:\r\n\tprint(\"Avatar for user: %s\" % user_id)\r\n\tprint(\"Size: %s\" % avatar_size)\r\n\r\n\t# Create the image for loading\r\n\tavatar_image = Image.new()\r\n\tavatar_image.create_from_data(avatar_size, avatar_size, false, Image.FORMAT_RGBA8, avatar_buffer)\r\n\t\t\r\n\t# Optionally resize the image if it is too large\r\n\tif avatar_size > 128:\r\n\t\tavatar_image.resize(128, 128, Image.INTERPOLATE_LANCZOS)\r\n\r\n\t# Apply the image to a texture\r\n\tvar avatar_texture: ImageTexture = ImageTexture.new()\r\n\tavatar_texture.create_from_image(avatar_image)\r\n\r\n\t# Set the texture to a Sprite, TextureRect, etc.\r\n\t$Sprite.set_texture(avatar_texture)\r\n```\r\n\r\n@tab Godot 4.x\r\n```gdscript\r\nfunc _on_loaded_avatar(user_id: int, avatar_size: int, avatar_buffer: PackedByteArray) -> void:\r\n\tprint(\"Avatar for user: %s\" % user_id)\r\n\tprint(\"Size: %s\" % avatar_size)\r\n\r\n\t# Create the image and texture for loading\r\n\tvar avatar_image: Image = Image.create_from_data(avatar_size, avatar_size, false, Image.FORMAT_RGBA8, avatar_buffer)\r\n\r\n\t# Optionally resize the image if it is too large\r\n\tif avatar_size > 128:\r\n\t\tavatar_image.resize(128, 128, Image.INTERPOLATE_LANCZOS)\r\n\r\n\t# Apply the image to a texture\r\n\tvar avatar_texture: ImageTexture = ImageTexture.create_from_image(avatar_image)\r\n\r\n\t# Set the texture to a Sprite, TextureRect, etc.\r\n\t$Sprite.set_texture(avatar_texture)\r\n```\r\n:::\r\n\r\nNaturally that texture could be applied elsewhere, depending on where you place this function. That covers the basics of getting player avatars.\r\n\r\n## Additional Resources{ .block }\r\n\r\n### Video Tutorials\r\n\r\nPrefer video tutorials? Feast your eyes and ears!\r\n\r\n[youtube: Getting Your Avatar Picture and Processing It](https:\/\/www.youtube.com\/watch?v=VCwNxfYZ8Cw&t=731s){ data-author=\"FinePointCGI\" }\r\n\r\n[youtube: How Do We Load Our Friends Avatar](https:\/\/www.youtube.com\/watch?v=VCwNxfYZ8Cw&t=6301s){ data-author=\"FinePointCGI\" }\r\n\r\n[youtube: GodotSteam - How It Works - Player Avatars](https:\/\/makertube.net\/w\/2mFQ5ai4Z6ZarENMghPaxN){ data-author=\"Gramps \/ GodotSteam\" }\r\n\r\n### Example Project\r\n\r\n[Later this year you can see this tutorial in action with more in-depth information by checking out our upcoming free-to-play game Skillet on GitHub.](https:\/\/github.com\/GodotSteam\/Skillet) There you will be able to view of the code used which can serve as a starting point for you to branch out from."
Comments
Log in to post a comment
Licensed under the CC-BY license
See the full license details
Submitted by
Gramps
Open on Github
Code for this tutorial is also available on Github
Table of contents
Compatibility
Works in Godot
3.x / 4.x
Tags
GodotSteam
steamworks
SteamĀ®
Share this tutorial
Share on Bluesky
Share on X / Twitter
or share this direct link:
https://thegodotbarn.com/contributions/tutorial/234/godotsteam-tutorials-avatars
Please wait ...
Okay
Okay
No
Yes
Okay