1. 添加LuaScriptRes

#pragma once
#include "runtime/core/meta/reflection/reflection.h"

namespace Piccolo
{
    REFLECTION_TYPE(LuaScriptRes)
    CLASS(LuaScriptRes, WhiteListFields)
    {
        REFLECTION_BODY(LuaScriptRes);

    public:
        void loadScriptContent();

    public:
        META(Enable)
        std::string m_script_url;

        std::string m_script_content;
    };
} // namespace Piccolo

#include "runtime/resource/res_type/components/lua_script.h"
#include "runtime/core/base/macro.h"
#include "runtime/function/global/global_context.h"
#include "runtime/resource/config_manager/config_manager.h"
#include "runtime/resource/asset_manager/asset_manager.h"

#include <fstream>

namespace Piccolo
{
    void LuaScriptRes::loadScriptContent()
    {
        // TODO : use file service
        auto file_dir = g_runtime_global_context.m_config_manager->getAssetFolder();
        file_dir      = file_dir / m_script_url;

        LOG_DEBUG("open lua script: " + file_dir.generic_string());

        std::ifstream fin;
        std::string   temp;
        m_script_content = "";
        fin.open(file_dir, std::ios::in);
        while (std::getline(fin, temp))
        {
            m_script_content += temp;
            m_script_content += "\n";
        }
        fin.close();

        LOG_DEBUG("script content:\n" + m_script_content);
    }
} // namespace Piccolo

2. 更改资源文件player.object.json

...
        {
            "$context": {
                "lua_script_res": {
                    "script_url": "scripts/player/set_jump_height.lua"
                }
            },
            "$typeName": "LuaComponent"
        },
...

set_jump_height.lua

if (get_bool(GameObject, "MotorComponent.m_is_moving")) then
    set_float(GameObject, "MotorComponent.m_motor_res.m_jump_height", 10)
else 
    set_float(GameObject, "MotorComponent.m_motor_res.m_jump_height", 4)
end
invoke(GameObject, "MotorComponent.getOffStuckState")

3. 修改LuaComponent

    void LuaComponent::postLoadResource(std::weak_ptr<GObject> parent_object)
    {
        m_parent_object = parent_object;
        m_lua_state.open_libraries(sol::lib::base);
        m_lua_state.set_function("set_float", &LuaComponent::set<float>);
        m_lua_state.set_function("get_float", &LuaComponent::get<float>);
        m_lua_state.set_function("set_bool", &LuaComponent::set<bool>);
        m_lua_state.set_function("get_bool", &LuaComponent::get<bool>);
        m_lua_state.set_function("invoke", &LuaComponent::invoke);
        m_lua_state["GameObject"] = m_parent_object;

        // load scripts
        m_lua_script_res.loadScriptContent();
    }
    
    void LuaComponent::tick(float delta_time)
    {
        if (!m_parent_object.lock())
            return;

        try
        {
            m_lua_state.script(m_lua_script_res.m_script_content);
        }
        catch (std::exception& e)
        {
            LOG_ERROR("lua script error: " + e.what());
        }
    }

通过上述修改,就可以使Piccolo引擎从文件中加载Lua脚本,可以更方便修改与调试。