makspll / makspll/bevy_mod_scripting

Pattern for working with Enum variants

Open
#414 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
649
Forks
59
PR merge metrics
No merged PRs in 30d

Description

Is your feature request related to a problem? Please describe.
Handling of enums feels relatively complex especially when working with things such as key codes, having to construct a variant, or convert to variant name and then do a string comparison makes our script api relatively unfriendly to users when considering things like modding etc.

i.e. with the input being KeyboardInput event.

function on_input(event)
    local key = event.key_code:variant_name()

    if (key == "KeyA") then
        info("move left")
    end
end
local KeyA = construct(types.KeyCode, {
  variant = "KeyA"
})

function on_input(event)
    if (event.key_code == KeyA) then
        info("move left")
    end
end

Describe the solution you'd like
A simpler way to handle enum variants, or even a well defined set of globals for bevy bindings (and a way for the user to easily define their own)

function on_input(event)
    if (event.key_code == KeyCode.KeyA) then
        info("move left")
    end
end
function on_input(event)
    if (event.key_code == keys.KeyA) then
        info("move left")
    end
end

Describe alternatives you've considered
This could be defined in a lua set of globals relatively easily.

keys = {
  KeyA = construct(types.KeyCode, {
    variant = "KeyA"
  })
}

Or register a global manually.

    registry.register("keys", move |wg: WorldGuard| {
        let allocator = wg.allocator();
        let mut allocator = allocator.write();

        let mut key_cache = HashMap::<String, ScriptValue>::default();
        for (key, name) in &key_codes {
            let payload = ReflectReference::new_allocated(*key, &mut allocator);

            key_cache.insert(name.to_string(), ScriptValue::Reference(payload));
        }

        Ok(ScriptValue::Map(key_cache))
    });

However this seems like an important binding to upstream if possible.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No repository files or tests are named. Start by reviewing the enum construction and variant_name examples, then inspect the registry approach described in the issue. Done would require an agreed API for comparing or constructing enum variants, including a decision on built-in globals versus user-defined bindings.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.