#correct use of resources?

17 messages · Page 1 of 1 (latest)

ripe ether
#

Minor question/confusion on resources.. This might be related to Rust as such.
Assuming I have resource for triggering some explicit changes to happen here and there:

#[derive(Resource)]
struct DespawnTimer(Timer);

Is it correct to manage this resource timer using:

commands.insert_resource::<DespawnTimer>(DespawnTimer(Timer::from_seconds(...));
// or:
commands.remove_resource::<DespawnTimer>();

Primarily, it seems that double DespawnTimer struct specification when creating resource is somewhat redundant?

lyric sapphire
#

Primarily, it seems that double DespawnTimer struct specification when creating resource is somewhat redundant?
The generic type can be inferred here; just omit it

#

In the particular case of timers, I would probably just pause / reset it rather than inserting and removing it though

quick plinth
#

sidenote: i personally try to add as many resources as i can to the app directly

lyric sapphire
#

Yep, it's very rare that I'll use commands for it

ripe ether
#

my intent here is that if the "card is flipped" in system code, i want to do it tiny bit later, so i create timer resource with "Once" type, not sure what you mean by doing it in the "app" directly

#

similarly (first primitive app doing being "pairs" game) when e.g. user flips second card, and it is "same" as the first one, i want them both to despawn second or two later, so user has time to see both cards shown... thus "once" timer approach

quick plinth
#

usually you'd to app.init_resource::<ResourceType>() once

ripe ether
#

not sure if i chose bests ways for this behavior, my first bevy app...

#

oh, so i can keep Once timer, but keep it existing after it finishes, and just reset it when needed, instead of removing/inserting new one...

#

currently i've been doing command.remove_resouce on Timer when it finished, which is wasteful as i understand now

quick plinth
#

yeah you'd either do it once or as a component

#

if you need a timer per some entity, a component is better

ripe ether
#

i did global timer that then iterates entities to modify/despawn, not sure if adding timers on specific entities affecting only "self" would be cleaner, or is just matter of taste

ripe ether
#

is it possible to somehow automatically pause/not start Once Timer after/inside of init_resource?

lyric sapphire
ripe ether
#

oh, i do have impl Default due to requireement of .init_resource::<_>(), so i just need to look into available constructors cautiously, thanks for good point!