Skip to content

Commit

Permalink
fix more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
makspll committed Apr 4, 2024
1 parent 975115b commit b3f89ac
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 254 deletions.
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ name = "game_of_life_lua"
path = "examples/lua/game_of_life.rs"
required-features = [
"lua54",
"teal",
"lua_script_api",
"bevy/file_watcher",
"bevy/multi-threaded",
Expand Down Expand Up @@ -184,8 +183,8 @@ path = "examples/rhai/bevy_api.rs"
required-features = ["rhai", "rhai_script_api"]

[[example]]
name = "new_wrappers"
path = "examples/new_wrappers.rs"
name = "wrappers"
path = "examples/wrappers.rs"
required-features = ["lua54", "lua_script_api"]

[[example]]
Expand Down
22 changes: 12 additions & 10 deletions assets/scripts/game_of_life.tl → assets/scripts/game_of_life.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
math.randomseed(os.time())

global function init()
function init()
local LifeState = world:get_type_by_name("LifeState")
local life_state = world:get_component(entity,LifeState) as BevyAPI.LuaLifeState
local cells = life_state.cells as {integer}
local life_state = world:get_component(entity,LifeState)
local cells = life_state.cells

-- set some cells alive
for _=1,10000 do
Expand All @@ -12,21 +12,22 @@ global function init()
end
end

global function on_update()
function on_update()
local LifeState = world:get_type_by_name("LifeState")
local Settings = world:get_type_by_name("Settings")

local life_state = world:get_component(entity,LifeState) as BevyAPI.LuaLifeState
local cells = life_state.cells as {integer}
local life_state = world:get_component(entity,LifeState)
-- note currently this is a copy of the cells, as of now the macro does not automatically support Vec<T> proxies by reference
local cells = life_state.cells

-- note that here we do not make use of LuaProxyable and just go off pure reflection
local settings = world:get_resource(Settings) as {string:any}
local dimensions = settings.physical_grid_dimensions as {integer}
local settings = world:get_resource(Settings)
local dimensions = settings.physical_grid_dimensions


-- primitives are passed by value to lua, keep a hold of old state but turn 255's into 1's
local prev_state = {}
for k,v in pairs(life_state.cells as {integer:integer}) do
for k,v in pairs(cells) do
prev_state[k] = (not(v == 0)) and 1 or 0
end

Expand All @@ -52,5 +53,6 @@ global function on_update()
end
end


-- propagate the updates
life_state.cells = cells
end
5 changes: 4 additions & 1 deletion crates/bevy_mod_scripting_common/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use syn::{
use crate::utils::ident_to_type_path;

/// Flags which detail required functionality or additional derivation requirements
#[derive(Debug, FromMeta)]
#[derive(Debug, FromMeta, Default)]
pub struct ProxyFlags {
pub clone: Flag,
}
Expand Down Expand Up @@ -561,12 +561,15 @@ pub struct ProxyInput {
pub data: darling::ast::Data<Variant, Field>,

/// Flags signifying which additional trait implementation should be generated on the proxy type
#[darling(default)]
pub derive: ProxyFlags,

/// A list of multi-lang function definitions to be generated on the proxy type
#[darling(default)]
pub functions: TraitItemFnsWrapper,
}

#[derive(Default)]
pub struct TraitItemFnsWrapper(pub Vec<TraitItemFn>);

impl FromMeta for TraitItemFnsWrapper {
Expand Down
22 changes: 19 additions & 3 deletions crates/languages/bevy_mod_scripting_lua/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// generates path to the given script depending on build configuration.
/// (optimized builds don't have the teal compiler available)
///
/// Current configuration will provide ".tl" paths
/// Current configuration will provide "scripts/*.tl" paths
/// ```rust
/// use bevy_mod_scripting_lua::lua_path;
/// assert_eq!("scripts/my_script.tl",lua_path!("my_script"))
Expand All @@ -17,19 +17,35 @@ macro_rules! lua_path {
/// generates path to the given script depending on build configuration.
/// (optimized builds don't have the teal compiler available)
///
/// Current configuration will provide ".lua" paths
/// Current configuration will provide "scripts/build/*.lua" paths
/// ```rust
/// use bevy_mod_scripting::lua_path;
/// assert_eq!("scripts/build/my_script.lua",lua_path!("my_script"))
/// ```
#[cfg(all(feature = "teal", not(debug_assertions)))]
#[cfg(all(not(debug_assertions), feature = "teal"))]
#[macro_export]
macro_rules! lua_path {
($v:literal) => {
concat!("scripts/build/", $v, ".lua")
};
}

/// generates path to the given script depending on build configuration.
/// (optimized builds don't have the teal compiler available)
///
/// Current configuration will provide "/scripts/*.lua" paths
/// ```rust
/// use bevy_mod_scripting::lua_path;
/// assert_eq!("scripts/build/my_script.lua",lua_path!("my_script"))
/// ```
#[cfg(not(feature = "teal"))]
#[macro_export]
macro_rules! lua_path {
($v:literal) => {
concat!("scripts/", $v, ".lua")
};
}

#[cfg(feature = "lua51")]
#[doc(hidden)]
#[macro_export]
Expand Down
2 changes: 1 addition & 1 deletion examples/lua/documentation_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl APIProvider for LuaAPIProvider {
type DocTarget = LuaDocFragment;
type ScriptContext = Mutex<Lua>;

fn attach_api(&mut self, ctx: &mut Self::APITarget) -> Result<(), ScriptError> {
fn attach_api(&mut self, _ctx: &mut Self::APITarget) -> Result<(), ScriptError> {
Ok(())
}

Expand Down
33 changes: 5 additions & 28 deletions examples/lua/game_of_life.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![allow(deprecated)]
use std::{borrow::Cow, sync::Mutex, time::Duration};
use std::sync::Mutex;

use bevy::{
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
Expand All @@ -15,25 +15,12 @@ use bevy::{

use bevy_mod_scripting::prelude::*;

#[derive(Debug, Default, Reflect, Component)]
#[derive(Debug, Default, Clone, Reflect, Component, LuaProxy)]
#[reflect(Component, LuaProxyable)]
pub struct LifeState {
pub cells: Vec<u8>,
}

impl_script_newtype!(
#[languages(lua)]
LifeState : Debug
lua impl {
get "cells" => |lua,s: &LuaLifeState| {
Ok(LuaVec::<u8>::new_ref(s.reflect_ref(lua.get_world()?).index(Cow::Borrowed("cells"))))
};
set "cells" => |lua,s,o| {
Vec::<u8>::apply_lua(&mut s.reflect_ref(lua.get_world()?).index(Cow::Borrowed("cells")),lua,o)
};
}
);

#[derive(Default)]
pub struct LifeAPI;

Expand All @@ -47,13 +34,6 @@ impl APIProvider for LifeAPI {
Ok(())
}

fn get_doc_fragment(&self) -> Option<Self::DocTarget> {
// this will enable us type casting in teal
Some(LuaDocFragment::new("MyAPI", |tw| {
tw.process_type::<LuaLifeState>()
}))
}

fn register_with_app(&self, app: &mut App) {
// this will register the `LuaProxyable` typedata since we derived it
// this will resolve retrievals of this component to our custom lua object
Expand Down Expand Up @@ -104,7 +84,6 @@ pub fn setup(

image.sampler = ImageSampler::nearest();

// in release builds we want to fetch ".lua" files over ".tl" files
let script_path = bevy_mod_scripting_lua::lua_path!("game_of_life");

commands.spawn(Camera2dBundle::default());
Expand Down Expand Up @@ -212,8 +191,7 @@ pub fn send_init(mut events: PriorityEventWriter<LuaEvent<()>>) {
)
}

/// how often to step the simulation
const UPDATE_FREQUENCY: f32 = 1.0 / 20.0;
const UPDATE_FREQUENCY: f32 = 1.0 / 60.0;

fn main() -> std::io::Result<()> {
let mut app = App::new();
Expand All @@ -231,9 +209,8 @@ fn main() -> std::io::Result<()> {
.add_systems(FixedUpdate, send_on_update.after(update_rendered_state))
.add_systems(FixedUpdate, script_event_handler::<LuaScriptHost<()>, 0, 1>)
.add_script_host::<LuaScriptHost<()>>(PostUpdate)
.add_api_provider::<LuaScriptHost<()>>(Box::new(LuaBevyAPIProvider))
.add_api_provider::<LuaScriptHost<()>>(Box::new(LifeAPI))
.update_documentation::<LuaScriptHost<()>>();
.add_api_provider::<LuaScriptHost<()>>(Box::new(CoreBevyAPIProvider))
.add_api_provider::<LuaScriptHost<()>>(Box::new(LifeAPI));

app.run();

Expand Down
117 changes: 0 additions & 117 deletions examples/new_wrappers.rs

This file was deleted.

Loading

0 comments on commit b3f89ac

Please sign in to comment.