Skip to content

Add test or example or docs for sending a hash map from Lua to Bevy #324

@mgi388

Description

@mgi388

Given a Lua function like this:

-- Global state
local my_state = {
    vars = {},        
    a_string = nil, 
}

function get_state()
    local type = world.get_type_by_name("MyResource")
    local my_resource = world.get_resource(type)
    my_resource.a_string = my_state.a_string
    -- Show how to send a table to Bevy.
    -- my_resource.vars = my_state.vars
    -- my_resource.vars = { foo = "bar" }
end

And the resource in Bevy:

#[derive(Debug, Default, Reflect, Resource)]
#[reflect(Debug, Default, Resource)]
pub struct MyResource {
    a_string: Option<String>,
    vars: std::collections::HashMap<String, String>,
}

Calling get_state() fails with an error:

Error in function set on type: bevy_mod_scripting_core::bindings::reference::ReflectReference: Value mismatch, expected: std::collections::HashMap<alloc::string::String, alloc::string::String, std::collections::hash_map::RandomState>, got: {foo: bar}.

An example/doc/test here would be great to show how one should pass maps between Lua and Bevy.

Specifically, docs for when to use ScriptValue or not would be useful. I haven't managed to fully understand this even from reading https://makspll.github.io/bevy_mod_scripting/generated_docs/types/scriptvalue.html?highlight=scriptvalue#scriptvalue

Edit: The general thing I'm trying to support here is sending a table of key/value pairs from the script to Bevy where the keys are only known at runtime.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions