diff --git a/doc/en/scripting/builtins/librules.md b/doc/en/scripting/builtins/librules.md index c4b1c377..acdf11d8 100644 --- a/doc/en/scripting/builtins/librules.md +++ b/doc/en/scripting/builtins/librules.md @@ -18,7 +18,7 @@ Creates a rule. If a handler is specified, returns the id for deletion. > Rules that have not been created can be used, but resetting via rules.reset will result in setting the value to nil. ```lua - rules.listen( +rules.listen( -- rule name name: str, -- value change handler function diff --git a/doc/en/scripting/ecs.md b/doc/en/scripting/ecs.md index 8763d981..90a64828 100644 --- a/doc/en/scripting/ecs.md +++ b/doc/en/scripting/ecs.md @@ -26,6 +26,9 @@ entity:get_uid() -> int entity:get_component(name: str) -> component or nil -- Checks for the presence of a component by name entity:has_component(name: str) -> bool + +-- Enables/disables the component +entity:set_enabled(name: str, enable: bool) ``` ## Built-in components diff --git a/doc/ru/scripting/ecs.md b/doc/ru/scripting/ecs.md index 3e8e00e5..db44a1ac 100644 --- a/doc/ru/scripting/ecs.md +++ b/doc/ru/scripting/ecs.md @@ -26,6 +26,9 @@ entity:get_uid() -> int entity:get_component(name: str) -> компонент или nil -- Проверяет наличие компонента по имени entity:has_component(name: str) -> bool + +-- Включает/выключает компонент по имени +entity:set_enabled(name: str, enable: bool) ``` ## Встроенные компоненты diff --git a/res/modules/internal/stdcomp.lua b/res/modules/internal/stdcomp.lua index bdb2468f..a62989a9 100644 --- a/res/modules/internal/stdcomp.lua +++ b/res/modules/internal/stdcomp.lua @@ -68,6 +68,22 @@ local Entity = {__index={ def_index=function(self) return entities.get_def(self.eid) end, def_name=function(self) return entities.def_name(entities.get_def(self.eid)) end, get_player=function(self) return entities.get_player(self.eid) end, + set_enabled=function(self, name, flag) + local comp = self.components[name] + if comp then + if flag then + if comp.__disabled and comp.on_enable then + comp.on_enable() + end + comp.__disabled = nil + else + if not comp.__disabled and comp.on_disable then + comp.on_disable() + end + comp.__disabled = true + end + end + end, }} local entities = {} @@ -99,7 +115,7 @@ return { end for _, component in pairs(entity.components) do local callback = component.on_update - if callback then + if not component.__disabled and callback then local result, err = pcall(callback, tps) if err then debug.error(err) @@ -113,7 +129,7 @@ return { for _,entity in pairs(entities) do for _, component in pairs(entity.components) do local callback = component.on_render - if callback then + if not component.__disabled and callback then local result, err = pcall(callback, delta) if err then debug.error(err) diff --git a/src/data/dv.cpp b/src/data/dv.cpp index 5709fd84..cccf348e 100644 --- a/src/data/dv.cpp +++ b/src/data/dv.cpp @@ -104,6 +104,9 @@ namespace dv { } boolean_t value::asBoolean() const { + if (type == value_type::none) { + return false; + } check_type(type, value_type::boolean); return val.boolean; } diff --git a/src/logic/scripting/scripting.cpp b/src/logic/scripting/scripting.cpp index d58879d3..d9b15490 100644 --- a/src/logic/scripting/scripting.cpp +++ b/src/logic/scripting/scripting.cpp @@ -614,6 +614,10 @@ static void process_entity_callback( ) { auto L = lua::get_main_state(); lua::pushenv(L, *env); + if (lua::hasfield(L, "__disabled")) { + lua::pop(L); + return; + } if (lua::getfield(L, name)) { if (args) { lua::call_nothrow(L, args(L), 0);