Skip to content

Change defaults with Local<> to be more ergonomic #17747

Open
@newclarityex

Description

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

I often use Local for systems that keep track of local ui input, timers, counters, etc. For data that only needs to be accessed in a single system, it's very convenient especially for prototyping to not have to manage resources or components. Using Local<> with a default value however is a lot more complex and makes Local<> much less appealing to use. The workaround described in the docs is to use a resource and convert your system into a capturing closure, which is more complex.

What solution would you like?

A possible solution would be a function on Local<> to set an intial value that is only in the first run of the system.

Example:

fn counter(mut count: Local<i32>) {
    count.set_default(10);
    *count += 1;
}

What alternative(s) have you considered?

An alternative workaround is to create a wrapping type, and impl default on that type, then Local. This is still simpler than the closure solution, and makes more sense, but I feel this can be simplified further.

Example:

struct CounterLocal(i32);

impl Default for CounterLocal {
    fn default() -> Self {
        CounterLocal(10)
    }
}

fn counter(mut count: Local<CounterLocal>) {
    count.0 += 1;
}

Additional context

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsC-DocsAn addition or correction to our documentationD-StraightforwardSimple bug fixes and API improvements, docs, test and examplesS-Ready-For-ImplementationThis issue is ready for an implementation PR. Go for it!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions