Files
phy/bevy_app/trait.Plugin.html
Orion Kindel 0ce894e6b0 doc
2025-03-18 10:30:23 -05:00

76 lines
21 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="A collection of Bevy app logic and configuration."><title>Plugin in bevy_app - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../static.files/rustdoc-42caa33d.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="bevy_app" data-themes="" data-resource-suffix="" data-rustdoc-version="1.84.0 (9fc6b4312 2025-01-07)" data-channel="1.84.0" data-search-js="search-92e6798f.js" data-settings-js="settings-0f613d39.js" ><script src="../static.files/storage-59e33391.js"></script><script defer src="sidebar-items.js"></script><script defer src="../static.files/main-5f194d8c.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-893ab5e7.css"></noscript><link rel="icon" href="https://bevyengine.org/assets/icon.png"></head><body class="rustdoc trait"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle" title="show sidebar"></button><a class="logo-container" href="../bevy_app/index.html"><img src="https://bevyengine.org/assets/icon.png" alt=""></a></nav><nav class="sidebar"><div class="sidebar-crate"><a class="logo-container" href="../bevy_app/index.html"><img src="https://bevyengine.org/assets/icon.png" alt="logo"></a><h2><a href="../bevy_app/index.html">bevy_<wbr>app</a><span class="version">0.15.1</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">Plugin</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#lifecycle-of-a-plugin" title="Lifecycle of a plugin">Lifecycle of a plugin</a></li><li><a href="#defining-a-plugin" title="Defining a plugin.">Defining a plugin.</a></li></ul><h3><a href="#required-methods">Required Methods</a></h3><ul class="block"><li><a href="#tymethod.build" title="build">build</a></li></ul><h3><a href="#provided-methods">Provided Methods</a></h3><ul class="block"><li><a href="#method.cleanup" title="cleanup">cleanup</a></li><li><a href="#method.finish" title="finish">finish</a></li><li><a href="#method.is_unique" title="is_unique">is_unique</a></li><li><a href="#method.name" title="name">name</a></li><li><a href="#method.ready" title="ready">ready</a></li></ul><h3><a href="#implementations">Methods</a></h3><ul class="block method"><li><a href="#method.downcast" title="downcast">downcast</a></li><li><a href="#method.downcast_mut" title="downcast_mut">downcast_mut</a></li><li><a href="#method.downcast_rc" title="downcast_rc">downcast_rc</a></li><li><a href="#method.downcast_ref" title="downcast_ref">downcast_ref</a></li><li><a href="#method.is" title="is">is</a></li></ul><h3><a href="#implementors">Implementors</a></h3></section><div id="rustdoc-modnav"><h2 class="in-crate"><a href="index.html">In crate bevy_<wbr>app</a></h2></div></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><span class="rustdoc-breadcrumbs"><a href="index.html">bevy_app</a></span><h1>Trait <span class="trait">Plugin</span><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../src/bevy_app/plugin.rs.html#58-93">Source</a> </span></div><pre class="rust item-decl"><code>pub trait Plugin:
<a class="trait" href="../downcast_rs/trait.Downcast.html" title="trait downcast_rs::Downcast">Downcast</a>
+ <a class="trait" href="https://doc.rust-lang.org/1.84.0/core/any/trait.Any.html" title="trait core::any::Any">Any</a>
+ <a class="trait" href="https://doc.rust-lang.org/1.84.0/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a>
+ <a class="trait" href="https://doc.rust-lang.org/1.84.0/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> {
// Required method
fn <a href="#tymethod.build" class="fn">build</a>(&amp;self, app: &amp;mut <a class="struct" href="struct.App.html" title="struct bevy_app::App">App</a>);
// Provided methods
fn <a href="#method.ready" class="fn">ready</a>(&amp;self, _app: &amp;<a class="struct" href="struct.App.html" title="struct bevy_app::App">App</a>) -&gt; <a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.bool.html">bool</a> { ... }
<span class="item-spacer"></span> fn <a href="#method.finish" class="fn">finish</a>(&amp;self, _app: &amp;mut <a class="struct" href="struct.App.html" title="struct bevy_app::App">App</a>) { ... }
<span class="item-spacer"></span> fn <a href="#method.cleanup" class="fn">cleanup</a>(&amp;self, _app: &amp;mut <a class="struct" href="struct.App.html" title="struct bevy_app::App">App</a>) { ... }
<span class="item-spacer"></span> fn <a href="#method.name" class="fn">name</a>(&amp;self) -&gt; &amp;<a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.str.html">str</a> { ... }
<span class="item-spacer"></span> fn <a href="#method.is_unique" class="fn">is_unique</a>(&amp;self) -&gt; <a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.bool.html">bool</a> { ... }
}</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>A collection of Bevy app logic and configuration.</p>
<p>Plugins configure an <a href="struct.App.html" title="struct bevy_app::App"><code>App</code></a>. When an <a href="struct.App.html" title="struct bevy_app::App"><code>App</code></a> registers a plugin,
the plugins <a href="trait.Plugin.html#tymethod.build" title="method bevy_app::Plugin::build"><code>Plugin::build</code></a> function is run. By default, a plugin
can only be added once to an <a href="struct.App.html" title="struct bevy_app::App"><code>App</code></a>.</p>
<p>If the plugin may need to be added twice or more, the function <a href="trait.Plugin.html#method.is_unique"><code>is_unique()</code></a>
should be overridden to return <code>false</code>. Plugins are considered duplicate if they have the same
<a href="trait.Plugin.html#method.name"><code>name()</code></a>. The default <code>name()</code> implementation returns the type name, which means
generic plugins with different type parameters will not be considered duplicates.</p>
<h3 id="lifecycle-of-a-plugin"><a class="doc-anchor" href="#lifecycle-of-a-plugin">§</a>Lifecycle of a plugin</h3>
<p>When adding a plugin to an <a href="struct.App.html" title="struct bevy_app::App"><code>App</code></a>:</p>
<ul>
<li>the app calls <a href="trait.Plugin.html#tymethod.build" title="method bevy_app::Plugin::build"><code>Plugin::build</code></a> immediately, and register the plugin</li>
<li>once the app started, it will wait for all registered <a href="trait.Plugin.html#method.ready" title="method bevy_app::Plugin::ready"><code>Plugin::ready</code></a> to return <code>true</code></li>
<li>it will then call all registered <a href="trait.Plugin.html#method.finish" title="method bevy_app::Plugin::finish"><code>Plugin::finish</code></a></li>
<li>and call all registered <a href="trait.Plugin.html#method.cleanup" title="method bevy_app::Plugin::cleanup"><code>Plugin::cleanup</code></a></li>
</ul>
<h3 id="defining-a-plugin"><a class="doc-anchor" href="#defining-a-plugin">§</a>Defining a plugin.</h3>
<p>Most plugins are simply functions that add configuration to an <a href="struct.App.html" title="struct bevy_app::App"><code>App</code></a>.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code>App::new().add_plugins(my_plugin).run();
<span class="comment">// This function implements `Plugin`, along with every other `fn(&amp;mut App)`.
</span><span class="kw">pub fn </span>my_plugin(app: <span class="kw-2">&amp;mut </span>App) {
app.add_systems(Update, hello_world);
}</code></pre></div>
<p>For more advanced use cases, the <code>Plugin</code> trait can be implemented manually for a type.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">pub struct </span>AccessibilityPlugin {
<span class="kw">pub </span>flicker_damping: bool,
<span class="comment">// ...
</span>}
<span class="kw">impl </span>Plugin <span class="kw">for </span>AccessibilityPlugin {
<span class="kw">fn </span>build(<span class="kw-2">&amp;</span><span class="self">self</span>, app: <span class="kw-2">&amp;mut </span>App) {
<span class="kw">if </span><span class="self">self</span>.flicker_damping {
app.add_systems(PostUpdate, damp_flickering);
}
}
}</code></pre></div>
</div></details><h2 id="required-methods" class="section-header">Required Methods<a href="#required-methods" class="anchor">§</a></h2><div class="methods"><details class="toggle method-toggle" open><summary><section id="tymethod.build" class="method"><a class="src rightside" href="../src/bevy_app/plugin.rs.html#60">Source</a><h4 class="code-header">fn <a href="#tymethod.build" class="fn">build</a>(&amp;self, app: &amp;mut <a class="struct" href="struct.App.html" title="struct bevy_app::App">App</a>)</h4></section></summary><div class="docblock"><p>Configures the <a href="struct.App.html" title="struct bevy_app::App"><code>App</code></a> to which this plugin is added.</p>
</div></details></div><h2 id="provided-methods" class="section-header">Provided Methods<a href="#provided-methods" class="anchor">§</a></h2><div class="methods"><details class="toggle method-toggle" open><summary><section id="method.ready" class="method"><a class="src rightside" href="../src/bevy_app/plugin.rs.html#65-67">Source</a><h4 class="code-header">fn <a href="#method.ready" class="fn">ready</a>(&amp;self, _app: &amp;<a class="struct" href="struct.App.html" title="struct bevy_app::App">App</a>) -&gt; <a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.bool.html">bool</a></h4></section></summary><div class="docblock"><p>Has the plugin finished its setup? This can be useful for plugins that need something
asynchronous to happen before they can finish their setup, like the initialization of a renderer.
Once the plugin is ready, <a href="trait.Plugin.html#method.finish" title="method bevy_app::Plugin::finish"><code>finish</code></a> should be called.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.finish" class="method"><a class="src rightside" href="../src/bevy_app/plugin.rs.html#71-73">Source</a><h4 class="code-header">fn <a href="#method.finish" class="fn">finish</a>(&amp;self, _app: &amp;mut <a class="struct" href="struct.App.html" title="struct bevy_app::App">App</a>)</h4></section></summary><div class="docblock"><p>Finish adding this plugin to the <a href="struct.App.html" title="struct bevy_app::App"><code>App</code></a>, once all plugins registered are ready. This can
be useful for plugins that depends on another plugin asynchronous setup, like the renderer.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.cleanup" class="method"><a class="src rightside" href="../src/bevy_app/plugin.rs.html#78-80">Source</a><h4 class="code-header">fn <a href="#method.cleanup" class="fn">cleanup</a>(&amp;self, _app: &amp;mut <a class="struct" href="struct.App.html" title="struct bevy_app::App">App</a>)</h4></section></summary><div class="docblock"><p>Runs after all plugins are built and finished, but before the app schedule is executed.
This can be useful if you have some resource that other plugins need during their build step,
but after build you want to remove it and send it to another thread.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.name" class="method"><a class="src rightside" href="../src/bevy_app/plugin.rs.html#84-86">Source</a><h4 class="code-header">fn <a href="#method.name" class="fn">name</a>(&amp;self) -&gt; &amp;<a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.str.html">str</a></h4></section></summary><div class="docblock"><p>Configures a name for the <a href="trait.Plugin.html" title="trait bevy_app::Plugin"><code>Plugin</code></a> which is primarily used for checking plugin
uniqueness and debugging.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.is_unique" class="method"><a class="src rightside" href="../src/bevy_app/plugin.rs.html#90-92">Source</a><h4 class="code-header">fn <a href="#method.is_unique" class="fn">is_unique</a>(&amp;self) -&gt; <a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.bool.html">bool</a></h4></section></summary><div class="docblock"><p>If the plugin can be meaningfully instantiated several times in an <a href="struct.App.html" title="struct bevy_app::App"><code>App</code></a>,
override this method to return <code>false</code>.</p>
</div></details></div><h2 id="implementations" class="section-header">Implementations<a href="#implementations" class="anchor">§</a></h2><div id="implementations-list"><details class="toggle implementors-toggle" open><summary><section id="impl-dyn+Plugin" class="impl"><a class="src rightside" href="../src/bevy_app/plugin.rs.html#95">Source</a><a href="#impl-dyn+Plugin" class="anchor">§</a><h3 class="code-header">impl dyn <a class="trait" href="trait.Plugin.html" title="trait bevy_app::Plugin">Plugin</a></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.is" class="method"><a class="src rightside" href="../src/bevy_app/plugin.rs.html#95">Source</a><h4 class="code-header">pub fn <a href="#method.is" class="fn">is</a>&lt;__T: <a class="trait" href="trait.Plugin.html" title="trait bevy_app::Plugin">Plugin</a>&gt;(&amp;self) -&gt; <a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.bool.html">bool</a></h4></section></summary><div class="docblock"><p>Returns true if the trait object wraps an object of type <code>__T</code>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.downcast" class="method"><a class="src rightside" href="../src/bevy_app/plugin.rs.html#95">Source</a><h4 class="code-header">pub fn <a href="#method.downcast" class="fn">downcast</a>&lt;__T: <a class="trait" href="trait.Plugin.html" title="trait bevy_app::Plugin">Plugin</a>&gt;(self: <a class="struct" href="https://doc.rust-lang.org/1.84.0/alloc/boxed/struct.Box.html" title="struct alloc::boxed::Box">Box</a>&lt;Self&gt;) -&gt; <a class="enum" href="https://doc.rust-lang.org/1.84.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;<a class="struct" href="https://doc.rust-lang.org/1.84.0/alloc/boxed/struct.Box.html" title="struct alloc::boxed::Box">Box</a>&lt;__T&gt;, <a class="struct" href="https://doc.rust-lang.org/1.84.0/alloc/boxed/struct.Box.html" title="struct alloc::boxed::Box">Box</a>&lt;Self&gt;&gt;</h4></section></summary><div class="docblock"><p>Returns a boxed object from a boxed trait object if the underlying object is of type
<code>__T</code>. Returns the original boxed trait if it isnt.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.downcast_rc" class="method"><a class="src rightside" href="../src/bevy_app/plugin.rs.html#95">Source</a><h4 class="code-header">pub fn <a href="#method.downcast_rc" class="fn">downcast_rc</a>&lt;__T: <a class="trait" href="trait.Plugin.html" title="trait bevy_app::Plugin">Plugin</a>&gt;(self: <a class="struct" href="https://doc.rust-lang.org/1.84.0/alloc/rc/struct.Rc.html" title="struct alloc::rc::Rc">Rc</a>&lt;Self&gt;) -&gt; <a class="enum" href="https://doc.rust-lang.org/1.84.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;<a class="struct" href="https://doc.rust-lang.org/1.84.0/alloc/rc/struct.Rc.html" title="struct alloc::rc::Rc">Rc</a>&lt;__T&gt;, <a class="struct" href="https://doc.rust-lang.org/1.84.0/alloc/rc/struct.Rc.html" title="struct alloc::rc::Rc">Rc</a>&lt;Self&gt;&gt;</h4></section></summary><div class="docblock"><p>Returns an <code>Rc</code>-ed object from an <code>Rc</code>-ed trait object if the underlying object is of
type <code>__T</code>. Returns the original <code>Rc</code>-ed trait if it isnt.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.downcast_ref" class="method"><a class="src rightside" href="../src/bevy_app/plugin.rs.html#95">Source</a><h4 class="code-header">pub fn <a href="#method.downcast_ref" class="fn">downcast_ref</a>&lt;__T: <a class="trait" href="trait.Plugin.html" title="trait bevy_app::Plugin">Plugin</a>&gt;(&amp;self) -&gt; <a class="enum" href="https://doc.rust-lang.org/1.84.0/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.reference.html">&amp;__T</a>&gt;</h4></section></summary><div class="docblock"><p>Returns a reference to the object within the trait object if it is of type <code>__T</code>, or
<code>None</code> if it isnt.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.downcast_mut" class="method"><a class="src rightside" href="../src/bevy_app/plugin.rs.html#95">Source</a><h4 class="code-header">pub fn <a href="#method.downcast_mut" class="fn">downcast_mut</a>&lt;__T: <a class="trait" href="trait.Plugin.html" title="trait bevy_app::Plugin">Plugin</a>&gt;(&amp;mut self) -&gt; <a class="enum" href="https://doc.rust-lang.org/1.84.0/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.reference.html">&amp;mut __T</a>&gt;</h4></section></summary><div class="docblock"><p>Returns a mutable reference to the object within the trait object if it is of type
<code>__T</code>, or <code>None</code> if it isnt.</p>
</div></details></div></details></div><h2 id="implementors" class="section-header">Implementors<a href="#implementors" class="anchor">§</a></h2><div id="implementors-list"><section id="impl-Plugin-for-MainSchedulePlugin" class="impl"><a class="src rightside" href="../src/bevy_app/main_schedule.rs.html#288-321">Source</a><a href="#impl-Plugin-for-MainSchedulePlugin" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="trait.Plugin.html" title="trait bevy_app::Plugin">Plugin</a> for <a class="struct" href="struct.MainSchedulePlugin.html" title="struct bevy_app::MainSchedulePlugin">MainSchedulePlugin</a></h3></section><section id="impl-Plugin-for-PanicHandlerPlugin" class="impl"><a class="src rightside" href="../src/bevy_app/panic_handler.rs.html#40-51">Source</a><a href="#impl-Plugin-for-PanicHandlerPlugin" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="trait.Plugin.html" title="trait bevy_app::Plugin">Plugin</a> for <a class="struct" href="struct.PanicHandlerPlugin.html" title="struct bevy_app::PanicHandlerPlugin">PanicHandlerPlugin</a></h3></section><section id="impl-Plugin-for-ScheduleRunnerPlugin" class="impl"><a class="src rightside" href="../src/bevy_app/schedule_runner.rs.html#72-174">Source</a><a href="#impl-Plugin-for-ScheduleRunnerPlugin" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="trait.Plugin.html" title="trait bevy_app::Plugin">Plugin</a> for <a class="struct" href="struct.ScheduleRunnerPlugin.html" title="struct bevy_app::ScheduleRunnerPlugin">ScheduleRunnerPlugin</a></h3></section><section id="impl-Plugin-for-TerminalCtrlCHandlerPlugin" class="impl"><a class="src rightside" href="../src/bevy_app/terminal_ctrl_c_handler.rs.html#58-73">Source</a><a href="#impl-Plugin-for-TerminalCtrlCHandlerPlugin" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="trait.Plugin.html" title="trait bevy_app::Plugin">Plugin</a> for <a class="struct" href="struct.TerminalCtrlCHandlerPlugin.html" title="struct bevy_app::TerminalCtrlCHandlerPlugin">TerminalCtrlCHandlerPlugin</a></h3></section><section id="impl-Plugin-for-T" class="impl"><a class="src rightside" href="../src/bevy_app/plugin.rs.html#97-101">Source</a><a href="#impl-Plugin-for-T" class="anchor">§</a><h3 class="code-header">impl&lt;T: <a class="trait" href="https://doc.rust-lang.org/1.84.0/core/ops/function/trait.Fn.html" title="trait core::ops::function::Fn">Fn</a>(&amp;mut <a class="struct" href="struct.App.html" title="struct bevy_app::App">App</a>) + <a class="trait" href="https://doc.rust-lang.org/1.84.0/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + <a class="trait" href="https://doc.rust-lang.org/1.84.0/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> + 'static&gt; <a class="trait" href="trait.Plugin.html" title="trait bevy_app::Plugin">Plugin</a> for T</h3></section></div><script src="../trait.impl/bevy_app/plugin/trait.Plugin.js" async></script></section></div></main></body></html>