bevyengine / bevyengine/bevy

Unable to specify `texture_atlas` in `bsn!` macro

Open
#24,889 3 comments 0 reactions 0 assignees View on GitHub
A-Scenes C-Bug D-Macros D-Modest S-Needs-Investigation X-Uncontroversial
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 22h
Merged PRs (30d)
161

Description

## Bevy version and features
Bevy 0.19

## What you did

I was trying to create a scene with that would describe an animated explosion and I needed `Sprite` with specified image and texture_atlas for that.

Here is the code:
```Rust
pub fn explosion(position: Vec3, explosion_txt: Res) -> impl Scene {
bsn!(
Explosion::default()
Transform::from_translation(position)
Sprite {
image: "explosions.png"
// PROBLEMATIC PART
texture_atlas: { Some(TextureAtlas {
layout: { explosion_txt.texture_atlas_layout.clone() },
index: { 0 },
})},
// END
custom_size: { Some(Vec2::splat(EXPLOSION_RADIUS * 2.)) },
}
template_value(RigidBody::Static)
CollisionEventsEnabled
)
}

```

## What went wrong

And I got an error, which I am unable to resolve. I asked about solution on Bevy's Discord and got instructed to post an issue here. The error goes as follows:
```
error[E0277]: the trait bound `OptionTemplate: From>` is not satisfied
--> src/summon_lib/explosion.rs:70:3
|
70 | / bsn!(
71 | | Explosion::default()
72 | | Transform::from_translation(position)
73 | | Sprite {
... |
77 | | texture_atlas: { Some(TextureAtlas {
| | ________________________-
78 | || layout: { explosion_txt.texture_atlas_layout.clone() },
79 | || index: { 0 },
80 | || })},
| ||________- this tail expression is of type `Option`
... |
85 | | CollisionEventsEnabled
86 | | )
| |____^ the trait `From>` is not implemented for `OptionTemplate`
|
help: the following other types implement trait `From`
--> /home/hunk/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.19.0/src/template.rs:530:1
|
530 | impl From> for OptionTemplate {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `OptionTemplate` implements `From>`
...
539 | impl From for OptionTemplate {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `OptionTemplate` implements `From`
= note: required for `Option` to implement `Into>`
= note: this error originates in the macro `bsn` (in Nightly builds, run with -Z macro-backtrace for more info)
```

## Additional information

At first i thought it might be a problem with the fact that texture_atlas is an `Option`, but after testing I found out that code with `Option` even with custom struct works perfectly fine.
Code below compiles successfully:
```
#[derive(Clone, Default)]
struct Abba {
a: u32,
b: u32
}

#[derive(Component, Clone, Default)]
struct Test {
foo: Option,
bar: Option
}
pub fn test() -> impl Scene {
bsn!(
Test {
foo: { Some(32) },
bar: {Some(Abba { a: 32, b: 32} )}
}
)
}
```
So I suspect it is probably something with `TextureAtlas` itself.

I'll happily provide more info if needed!

Contributor guide

Open the contributing guide

Research direction

Start with the bsn! macro expansion and the OptionTemplate implementations in bevy_ecs/src/template.rs around line 530, then reproduce the error using the Sprite texture_atlas example from the issue. Compare it with the working custom Option example and verify that specifying an optional TextureAtlas in bsn! compiles successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
game-dev
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.