bevyengine / bevyengine/bevy

`ReflectPath` on `TypeInfo`

Open
#11,677 0 comments 2 reactions 0 assignees View on GitHub
A-Reflection C-Feature
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 16h
Merged PRs (30d)
171

Description

## What problem does this solve or what need does it fill?

In my library, [bevy_dev_console](https://github.com/doonv/bevy_dev_console), the user can modify a resource's data using commands, for example: `MyStruct.number1 = 5`.

You can also assign to enums with this: `MyEnum = MyVariant`, the way I assign enums is via the `Enum::apply` method. But the `apply` method does not do any checking for if the variant being assigned actually exists. So I need to check if the enum variant actually exists. I do this by getting the `Resource`'s `TypeRegistration`, getting its `TypeInfo`, and then checking if the `TypeInfo` is an enum, and then unwrapping the `TypeInfo::Enum`, from there I can use the `EnumInfo::variant` method to check if the enum variant exists.

The problem with that is nested enums, in a case like `MyStruct.enum = MyVariant`, the `Resource`'s `TypeInfo` is not an enum, but a struct, the same sturct as `MyStruct`. I already use `ReflectPath` to get the value of `MyStruct.enum`, but that is just a `&mut dyn Enum`, so only the current variant. I need to do something similar to `ReflectPath` on `TypeInfo` so the value and the `TypeInfo` match.

## What solution would you like?

I need to perform something similar to `ReflectPath` on `TypeInfo`

## What alternative(s) have you considered?

I can use `<&mut dyn Enum>::get_represented_type_info()`, but I'm not sure how reliable that method is as it returns an `Option`, and apparently it's somewhat slow.

I can also use `TypeRegistry::get_type_info(type_id)`, which is apparently faster (and maybe more reliable?).

## Additional Information

[`bevy_dev_console`'s enum assignment code](https://github.com/doonv/bevy_dev_console/blob/1c694a6c247eed9248f18b83543f7e5538ad39a4/src/builtin_parser/runner.rs#L202)

Contributor guide

Open the contributing guide

Research direction

Start with the enum-assignment code in bevy_dev_console's src/builtin_parser/runner.rs around line 202, then read the existing ReflectPath and TypeInfo APIs. Determine how a nested path should resolve to matching type information, including enum variants; done means the requested API supports reliable nested enum lookup.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
game-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.