Skip to content

Commit

Permalink
deploy: cfb3205
Browse files Browse the repository at this point in the history
  • Loading branch information
mrDIMAS committed Sep 12, 2024
1 parent 0c71cc0 commit 7d54d8c
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 3 deletions.
50 changes: 49 additions & 1 deletion code/snippets/src/scripting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ pub mod tasks;

use fyrox::graph::BaseSceneGraph;
use fyrox::graph::SceneGraph;
use fyrox::script::{ScriptMessageContext, ScriptMessagePayload};
use fyrox::plugin::Plugin;
use fyrox::script::{ScriptDeinitContext, ScriptMessageContext, ScriptMessagePayload};
use fyrox::{
core::{
log::Log, pool::Handle, reflect::prelude::*, type_traits::prelude::*, visitor::prelude::*,
Expand Down Expand Up @@ -125,3 +126,50 @@ impl ScriptTrait for LaserSight {
}
}
// ANCHOR_END: message_passing

// ANCHOR: access_plugin
#[derive(Default, Debug, Reflect, Visit)]
struct GamePlugin {
bots: Vec<Handle<Node>>,
}

impl Plugin for GamePlugin {
// ..
}

#[derive(Clone, Debug, Default, Visit, Reflect, ComponentProvider, TypeUuidProvider)]
#[type_uuid(id = "460cd09f-8768-4f38-8799-5e9c0c08b8fd")]
struct Bot {
// ..
}

impl ScriptTrait for Bot {
fn on_start(&mut self, ctx: &mut ScriptContext) {
// Get a reference to the plugin.
let plugin = ctx.plugins.get_mut::<GamePlugin>();
// Register self in the "global" list of bots.
plugin.bots.push(ctx.handle);
}

fn on_deinit(&mut self, ctx: &mut ScriptDeinitContext) {
let plugin = ctx.plugins.get_mut::<GamePlugin>();
// Unregister the bot from the list.
if let Some(index) = plugin
.bots
.iter()
.position(|handle| *handle == ctx.node_handle)
{
plugin.bots.remove(index);
}
}

fn on_update(&mut self, ctx: &mut ScriptContext) {
let plugin = ctx.plugins.get::<GamePlugin>();
for bot in plugin.bots.iter() {
if *bot != ctx.handle {
// Search for target.
}
}
}
}
// ANCHOR_END: access_plugin
57 changes: 57 additions & 0 deletions print.html
Original file line number Diff line number Diff line change
Expand Up @@ -2009,6 +2009,63 @@ <h2 id="accessing-other-scripts-data"><a class="header" href="#accessing-other-s
<span class="boring">}</span></code></pre></pre>
<p>This code searches for a node with <code>SomeName</code> and assigns its handle to the <code>second_node</code> variable in the script
for later use.</p>
<h2 id="accessing-plugins-from-scripts"><a class="header" href="#accessing-plugins-from-scripts">Accessing Plugins From Scripts</a></h2>
<p>Sometimes there's a need to access plugin data from scripts, there may be various reasons for that, for example
you may need to register a bot in the list of bots. This list could then be used for AI to search targets without
searching in the entire scene graph at every frame.</p>
<p>Accessing plugins from scripts is very easy, all you need to do is to call <code>get/get_mut</code> method from <code>ctx.plugins</code>:</p>
<pre><pre class="playground"><code class="language-rust no_run"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>#[derive(Default, Debug, Reflect, Visit)]
struct GamePlugin {
bots: Vec&lt;Handle&lt;Node&gt;&gt;,
}

impl Plugin for GamePlugin {
// ..
}

#[derive(Clone, Debug, Default, Visit, Reflect, ComponentProvider, TypeUuidProvider)]
#[type_uuid(id = "460cd09f-8768-4f38-8799-5e9c0c08b8fd")]
struct Bot {
// ..
}

impl ScriptTrait for Bot {
fn on_start(&amp;mut self, ctx: &amp;mut ScriptContext) {
// Get a reference to the plugin.
let plugin = ctx.plugins.get_mut::&lt;GamePlugin&gt;();
// Register self in the "global" list of bots.
plugin.bots.push(ctx.handle);
}

fn on_deinit(&amp;mut self, ctx: &amp;mut ScriptDeinitContext) {
let plugin = ctx.plugins.get_mut::&lt;GamePlugin&gt;();
// Unregister the bot from the list.
if let Some(index) = plugin
.bots
.iter()
.position(|handle| *handle == ctx.node_handle)
{
plugin.bots.remove(index);
}
}

fn on_update(&amp;mut self, ctx: &amp;mut ScriptContext) {
let plugin = ctx.plugins.get::&lt;GamePlugin&gt;();
for bot in plugin.bots.iter() {
if *bot != ctx.handle {
// Search for target.
}
}
}
}
<span class="boring">}</span></code></pre></pre>
<p>In this example the Bot script registers itself in a global list of bots on start, and unregisters on destruction.
<code>update</code> is then used to search for targets in that list.</p>
<p>In multiplayer games, plugin could store server/client instances and scripts could easily access them to send messages
across the network for other players. In general, you could use plugins as an arbitrary, global data storage for your
scripts.</p>
<div style="break-before: page; page-break-before: always;"></div><h1 id="tasks"><a class="header" href="#tasks">Tasks</a></h1>
<p>Fyrox supports task-based programming for both scripts and plugins. Task is a closure that does something in a separate
thread and then the result of it is returned back to the main thread. This is very useful technique, that allows you to
Expand Down
57 changes: 57 additions & 0 deletions scripting/script.html
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,63 @@ <h2 id="accessing-other-scripts-data"><a class="header" href="#accessing-other-s
<span class="boring">}</span></code></pre></pre>
<p>This code searches for a node with <code>SomeName</code> and assigns its handle to the <code>second_node</code> variable in the script
for later use.</p>
<h2 id="accessing-plugins-from-scripts"><a class="header" href="#accessing-plugins-from-scripts">Accessing Plugins From Scripts</a></h2>
<p>Sometimes there's a need to access plugin data from scripts, there may be various reasons for that, for example
you may need to register a bot in the list of bots. This list could then be used for AI to search targets without
searching in the entire scene graph at every frame.</p>
<p>Accessing plugins from scripts is very easy, all you need to do is to call <code>get/get_mut</code> method from <code>ctx.plugins</code>:</p>
<pre><pre class="playground"><code class="language-rust no_run"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>#[derive(Default, Debug, Reflect, Visit)]
struct GamePlugin {
bots: Vec&lt;Handle&lt;Node&gt;&gt;,
}

impl Plugin for GamePlugin {
// ..
}

#[derive(Clone, Debug, Default, Visit, Reflect, ComponentProvider, TypeUuidProvider)]
#[type_uuid(id = "460cd09f-8768-4f38-8799-5e9c0c08b8fd")]
struct Bot {
// ..
}

impl ScriptTrait for Bot {
fn on_start(&amp;mut self, ctx: &amp;mut ScriptContext) {
// Get a reference to the plugin.
let plugin = ctx.plugins.get_mut::&lt;GamePlugin&gt;();
// Register self in the "global" list of bots.
plugin.bots.push(ctx.handle);
}

fn on_deinit(&amp;mut self, ctx: &amp;mut ScriptDeinitContext) {
let plugin = ctx.plugins.get_mut::&lt;GamePlugin&gt;();
// Unregister the bot from the list.
if let Some(index) = plugin
.bots
.iter()
.position(|handle| *handle == ctx.node_handle)
{
plugin.bots.remove(index);
}
}

fn on_update(&amp;mut self, ctx: &amp;mut ScriptContext) {
let plugin = ctx.plugins.get::&lt;GamePlugin&gt;();
for bot in plugin.bots.iter() {
if *bot != ctx.handle {
// Search for target.
}
}
}
}
<span class="boring">}</span></code></pre></pre>
<p>In this example the Bot script registers itself in a global list of bots on start, and unregisters on destruction.
<code>update</code> is then used to search for targets in that list.</p>
<p>In multiplayer games, plugin could store server/client instances and scripts could easily access them to send messages
across the network for other players. In general, you could use plugins as an arbitrary, global data storage for your
scripts.</p>

</main>

Expand Down
2 changes: 1 addition & 1 deletion searchindex.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion searchindex.json

Large diffs are not rendered by default.

0 comments on commit 7d54d8c

Please sign in to comment.