amethyst / amethyst/specs

[Question] How to change values of Component's from outside of World ?

Offen
#727 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
feature-request
Vorherrschende Sprache
Rust
Sterne
2.6k
Forks
215
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

## Description
Hello. Im making a GUI prototype based on the ECS architecture using specs.
i would like to modfiy the values of components from outside the world through widgets.

## Motivation
Lets says i have Label widget, and i would like to modify its TextComponent's value when i click on a Button widget.

```rust
struct TextComponent {
text: String
}

struct Label {
id: Entity,
width: usize,
height: usize,
x: usize,
y: usize,
text: TextComponent
}

impl Label {
fn text(&self) -> &TextComponent {
&self.text
}

fn set_text(&mut self, new_text: String) {
// label has the new value, but world has the old value...
// how to notify the world that this entity's component changed ?
self.text = TextComponent {
text: new_text
}

}

impl build(world: &mut World) -> Label {
world.register::();
let text = TextComponent::default();
let id = world.create_entity()
.width(text);

Label {
id: id,
width: 100,
height: 75,
x:0,
y:0,
text: text
}
}
}
```

## Drawbacks
i would like to avoid using Rc> if its possible, it does not scale with lots of entities and has some performance costs
How do games solves this problem ? Im thinking some kinf of messaging system but what libraries are should i use ?
I tried the observer pattern, but its very challenging with rust ownership's system to hold a mutable references of World in each widget.

## Unresolved questions

---

Beitragsleitfaden

Beitragsleitfaden öffnen

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.