Trait Action

Source
pub trait Action:
    Downcast
    + Send
    + Sync
    + 'static {
    // Required methods
    fn is_finished(&self, agent: Entity, world: &World) -> bool;
    fn on_start(&mut self, agent: Entity, world: &mut World) -> bool;
    fn on_stop(
        &mut self,
        agent: Option<Entity>,
        world: &mut World,
        reason: StopReason,
    );

    // Provided methods
    fn on_add(&mut self, agent: Entity, world: &mut World) { ... }
    fn on_remove(&mut self, agent: Option<Entity>, world: &mut World) { ... }
    fn on_drop(
        self: Box<Self>,
        agent: Option<Entity>,
        world: &mut World,
        reason: DropReason,
    ) { ... }
    fn type_name(&self) -> &'static str { ... }
}
Expand description

The trait that all actions must implement.

It contains various methods that together defines the lifecycle of an action. From this, you can create any action that can last as long as you like, and do as much as you like.

In general, the act of starting the action should happen inside on_start. This is especially true for despawning an agent. The other methods should be used for initializing and cleaning up. See below for more information.

§⚠️ Warning

Since you are given a mutable World, you can in practice do anything. Depending on what you do, the logic for advancing the action queue might not work properly.

There are a few things you should keep in mind:

  • If you want to despawn an agent as an action, this should be done in on_start.
  • The execute and next methods should not be used, as that will immediately advance the action queue while inside any of the trait methods. Instead, you should return true in on_start.
  • When adding new actions, you should set the start property to false. Otherwise, you will effectively call execute which, again, should not be used. At worst, you will cause a stack overflow if the action adds itself.
    fn on_start(&mut self, agent: Entity, world: &mut World) -> bool {
        world
            .actions(agent)
            .start(false) // Do not start next action
            .add((action_a, action_b, action_c));

        // Immediately advance the action queue
        true
    }

Required Methods§

Source

fn is_finished(&self, agent: Entity, world: &World) -> bool

Determines if an action is finished or not. Advances the action queue when returning true.

By default, this method is called every frame in the Last schedule.

Source

fn on_start(&mut self, agent: Entity, world: &mut World) -> bool

The method that is called when an action is started.

Typically here you would insert components to agent or a new entity to make systems run. If you wish to despawn agent as an action, this is where you should do it.

Returning true here marks the action as already finished, and will immediately advance the action queue.

Source

fn on_stop( &mut self, agent: Option<Entity>, world: &mut World, reason: StopReason, )

The method that is called when an action is stopped.

Typically here you would clean up any stuff from on_start, depending on the reason.

When paused, the action will be put back into the action queue again to the front. This means that it will start again when the next action is executed.

Provided Methods§

Source

fn on_add(&mut self, agent: Entity, world: &mut World)

The method that is called when an action is added to the queue.

You can think of this as the constructor, as it will only be called once.

Examples found in repository?
examples/repeat.rs (line 52)
51    fn on_add(&mut self, agent: Entity, world: &mut World) {
52        self.action.on_add(agent, world);
53    }
More examples
Hide additional examples
examples/parallel.rs (line 50)
47    fn on_add(&mut self, agent: Entity, world: &mut World) {
48        self.actions
49            .iter_mut()
50            .for_each(|action| action.on_add(agent, world));
51    }
examples/sequence.rs (line 52)
49    fn on_add(&mut self, agent: Entity, world: &mut World) {
50        self.actions
51            .iter_mut()
52            .for_each(|action| action.on_add(agent, world));
53    }
Source

fn on_remove(&mut self, agent: Option<Entity>, world: &mut World)

The method that is called when an action is removed from the queue.

You can think of this as the destructor, as it will only be called once.

Examples found in repository?
examples/repeat.rs (line 64)
63    fn on_remove(&mut self, agent: Option<Entity>, world: &mut World) {
64        self.action.on_remove(agent, world);
65    }
More examples
Hide additional examples
examples/sequence.rs (line 65)
64    fn on_remove(&mut self, agent: Option<Entity>, world: &mut World) {
65        self.actions[self.index].on_remove(agent, world);
66    }
67
68    fn on_drop(mut self: Box<Self>, agent: Option<Entity>, world: &mut World, reason: DropReason) {
69        self.index += 1;
70
71        if self.index >= N {
72            return;
73        }
74
75        if self.canceled || reason != DropReason::Done {
76            for i in self.index..N {
77                self.actions[i].on_remove(agent, world);
78            }
79            return;
80        }
81
82        let Some(agent) = agent else { return };
83
84        world
85            .actions(agent)
86            .start(false)
87            .order(AddOrder::Front)
88            .add(self as BoxedAction);
89    }
examples/parallel.rs (line 68)
65    fn on_remove(&mut self, agent: Option<Entity>, world: &mut World) {
66        self.actions
67            .iter_mut()
68            .for_each(|action| action.on_remove(agent, world));
69    }
Source

fn on_drop( self: Box<Self>, agent: Option<Entity>, world: &mut World, reason: DropReason, )

The last method that is called with full ownership.

This is useful for actions that might want to alter the behavior of the action queue. For example, a RepeatAction could keep readding itself to the action queue based on some counter.

Source

fn type_name(&self) -> &'static str

Returns the type name of an action.

Implementations§

Source§

impl dyn Action

Source

pub fn is<__T: Action>(&self) -> bool

Returns true if the trait object wraps an object of type __T.

Source

pub fn downcast<__T: Action>(self: Box<Self>) -> Result<Box<__T>, Box<Self>>

Returns a boxed object from a boxed trait object if the underlying object is of type __T. Returns the original boxed trait if it isn’t.

Source

pub fn downcast_rc<__T: Action>(self: Rc<Self>) -> Result<Rc<__T>, Rc<Self>>

Returns an Rc-ed object from an Rc-ed trait object if the underlying object is of type __T. Returns the original Rc-ed trait if it isn’t.

Source

pub fn downcast_ref<__T: Action>(&self) -> Option<&__T>

Returns a reference to the object within the trait object if it is of type __T, or None if it isn’t.

Source

pub fn downcast_mut<__T: Action>(&mut self) -> Option<&mut __T>

Returns a mutable reference to the object within the trait object if it is of type __T, or None if it isn’t.

Implementors§

Source§

impl<OnStart> Action for OnStart
where OnStart: FnMut(Entity, &mut World) -> bool + Send + Sync + 'static,