Skip to content

RandomTextureSpriteBehavior: Get path from AtlasTexture.atlas#2221

Merged
manuq merged 1 commit into
mainfrom
wjt/randomtexturespritebehavior-get-path-from-atlastexture-atlas
May 19, 2026
Merged

RandomTextureSpriteBehavior: Get path from AtlasTexture.atlas#2221
manuq merged 1 commit into
mainfrom
wjt/randomtexturespritebehavior-get-path-from-atlastexture-atlas

Conversation

@wjt
Copy link
Copy Markdown
Member

@wjt wjt commented May 19, 2026

RandomTextureSpriteBehavior: Get path from AtlasTexture.atlas

This script works by selecting one of an array of SpriteFrames resources
at random, then offsetting the sprite based on information encoded in
the filename. For example, the names of
townie-body_003.dx_-4.dy_-16.tres and its underlying texture atlas
townie-idle-body_003.dx_-4.dy_-16.png encode that the sprite should be
offset by Vector2(-4, -16). This allows different randomly-generated
townies to be different sizes.

We have some code that tries to find the path to the chosen SpriteFrames
resource, or failing that the path of the underlying texture. In practice,
new_sprite_frames.resource_path.get_file() is always "" (for unknown
reasons) so we always use the second path, looking at the path of the
first frame of the idle texture.

Previously this was:

filename = new_sprite_frames.get_frame_texture(&"idle", 0).resource_path

But the behaviour differs between running the game in the editor and in
release builds. In the editor, this gives
res://scenes/game_elements/characters/components/sprite_frames/townie-legs_002.dy_-6.tres::AtlasTexture_s8pun,
while in the exported game we get
res://scenes/game_elements/characters/npcs/townie.tscn::AtlasTexture_s8pun.

I can't quite explain this but my guess is that the SpriteFrames in
townie-legs_002.dy_-1.tres is getting inlined into the exported binary
townie.scn. But if we walk one step deeper, resolving the AtlasTexture
to its underlying Texture2D, whose resource_path is
res://scenes/game_elements/characters/npcs/components/townie-idle-legs_002.dy_-6.png
as hoped.

Also add a missing .get_file() to convert from the full path to the
filename in this code path.

Resolves #2216

This script works by selecting one of an array of SpriteFrames resources
at random, then offsetting the sprite based on information encoded in
the filename. For example, the names of
townie-body_003.dx_-4.dy_-16.tres and its underlying texture atlas
townie-idle-body_003.dx_-4.dy_-16.png encode that the sprite should be
offset by Vector2(-4, -16). This allows different randomly-generated
townies to be different sizes.

We have some code that tries to find the path to the chosen SpriteFrames
resource, or failing that the path of the underlying texture. In practice,
`new_sprite_frames.resource_path.get_file()` is always `""` (for unknown
reasons) so we always use the second path, looking at the path of the
first frame of the idle texture.

Previously this was:

    filename = new_sprite_frames.get_frame_texture(&"idle", 0).resource_path

But the behaviour differs between running the game in the editor and in
release builds. In the editor, this gives
`res://scenes/game_elements/characters/components/sprite_frames/townie-legs_002.dy_-6.tres::AtlasTexture_s8pun`,
while in the exported game we get
`res://scenes/game_elements/characters/npcs/townie.tscn::AtlasTexture_s8pun`.

I can't quite explain this but my guess is that the SpriteFrames in
`townie-legs_002.dy_-1.tres` is getting inlined into the exported binary
`townie.scn`. But if we walk one step deeper, resolving the AtlasTexture
to its underlying Texture2D, whose resource_path is
`res://scenes/game_elements/characters/npcs/components/townie-idle-legs_002.dy_-6.png`
as hoped.

Also add a missing .get_file() to convert from the full path to the
filename in this code path.

Resolves #2216
@wjt wjt requested a review from a team as a code owner May 19, 2026 11:00
@github-actions
Copy link
Copy Markdown

Play this branch at https://play.threadbare.game/branches/endlessm/wjt/randomtexturespritebehavior-get-path-from-atlastexture-atlas/.

(This launches the game from the start, not directly at the change(s) in this pull request.)

Copy link
Copy Markdown
Collaborator

@manuq manuq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh awesome, thanks for the fix! I was going to add a dictionary property to map the textures with Vector2 offsets.

@manuq manuq merged commit df6c124 into main May 19, 2026
7 checks passed
@manuq manuq deleted the wjt/randomtexturespritebehavior-get-path-from-atlastexture-atlas branch May 19, 2026 11:48
@wjt
Copy link
Copy Markdown
Member Author

wjt commented May 19, 2026

I was thinking an array of structs:

class_name RandomTextureSpriteBehavior

...

class BodyPart extends Resource:
    sprite_frames: SpriteFrames
    offset: Vector2

@export var sprite_frames: Array[BodyPart]

It might still be clearer to do that… but this does work.

Or! We could have:

class_name BodyPart extends SpriteFrames

@export var offset: Vector2

and make res://scenes/game_elements/characters/components/sprite_frames/townie-body_003.dx_-4.dy_-16.tres an instance of that not a bare SpriteFrames.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Townie: Parts offset is incorrect in exported builds

2 participants