Git Product home page Git Product logo

Comments (2)

makspll avatar makspll commented on September 1, 2024

I see, I think you're likely missing #[reflect(Reflect, Default)], here is an example which seems to work:

use bevy::app::AppExit;

use bevy::prelude::*;
use bevy_mod_scripting::prelude::*;

use bevy_script_api::lua::{bevy::LuaBevyAPIProvider, RegisterForeignLuaType};

#[derive(Component, Default, Reflect)]
#[reflect(Component, Default)]
pub struct DefComponent;

fn main() -> std::io::Result<()> {
    let mut app = App::new();

    app.add_plugins(DefaultPlugins)
        .add_plugins(ScriptingPlugin)
        .register_type::<DefComponent>()
        // note the implementation for Option is there, but we must register `LuaProxyable` for it
        .add_script_host::<LuaScriptHost<()>>(PostUpdate)
        .add_api_provider::<LuaScriptHost<()>>(Box::new(LuaBevyAPIProvider))
        .add_systems(Startup, |world: &mut World| {
            let entity = world.spawn(()).id();

            // run script
            world.resource_scope(|world, mut host: Mut<LuaScriptHost<()>>| {
                host.run_one_shot(
                    r#"
                        function once()
                            local my_component_type = world:get_type_by_name("DefComponent")
                            print("before: ", world:get_component(entity, my_component_type))
                            world:add_default_component(entity, my_component_type)
                            print("after: ", world:get_component(entity, my_component_type))
                        end
                        "#
                    .as_bytes(),
                    "script.lua",
                    entity,
                    world,
                    LuaEvent {
                        hook_name: "once".to_owned(),
                        args: (),
                        recipients: Recipients::All,
                    },
                )
                .expect("Something went wrong in the script!");
            });

            world.send_event(AppExit)
        });

    app.run();

    Ok(())
}

with the output being:

before:         nil
after:  bevy_api_lua::DefComponent

from bevy_mod_scripting.

LaserWitch avatar LaserWitch commented on September 1, 2024

You were right on the money, adding the reflect line to my struct got everything working right.

from bevy_mod_scripting.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.