diff --git a/doc/en/scripting/ecs.md b/doc/en/scripting/ecs.md index f0a33a97..20d03e5d 100644 --- a/doc/en/scripting/ecs.md +++ b/doc/en/scripting/ecs.md @@ -26,6 +26,8 @@ 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 +-- Retrieves a component by name. Throws an exception if it does not exist +entity:require_component(name: str) -> component -- Enables/disables the component entity:set_enabled(name: str, enable: bool) diff --git a/doc/ru/scripting/ecs.md b/doc/ru/scripting/ecs.md index 89af90fa..54923c9a 100644 --- a/doc/ru/scripting/ecs.md +++ b/doc/ru/scripting/ecs.md @@ -26,6 +26,8 @@ entity:get_uid() -> int entity:get_component(name: str) -> компонент или nil -- Проверяет наличие компонента по имени entity:has_component(name: str) -> bool +-- Запрашивает компонент по имени. Бросает исключение при отсутствии +entity:require_component(name: str) -> компонент -- Включает/выключает компонент по имени entity:set_enabled(name: str, enable: bool) diff --git a/res/modules/internal/stdcomp.lua b/res/modules/internal/stdcomp.lua index 767bfa35..7b66070d 100644 --- a/res/modules/internal/stdcomp.lua +++ b/res/modules/internal/stdcomp.lua @@ -63,6 +63,13 @@ local Entity = {__index={ get_skeleton=function(self) return entities.get_skeleton(self.eid) end, set_skeleton=function(self, s) return entities.set_skeleton(self.eid, s) end, get_component=function(self, name) return self.components[name] end, + require_component=function(self, name) + local component = self.components[name] + if not component then + error(("entity has no required component '%s'"):format(name)) + end + return component + end, has_component=function(self, name) return self.components[name] ~= nil end, get_uid=function(self) return self.eid end, def_index=function(self) return entities.get_def(self.eid) end,