diff --git a/evennia/commands/default/building.py b/evennia/commands/default/building.py index 40f1b60371..0692bca1ac 100644 --- a/evennia/commands/default/building.py +++ b/evennia/commands/default/building.py @@ -1858,12 +1858,21 @@ class CmdSetAttribute(ObjManipCommand): nested = False for key, nested_keys in self.split_nested_attr(attr): nested = True - if obj.attributes.has(key): - val = obj.attributes.get(key) - val = self.do_nested_lookup(val, *nested_keys) - if val is not self.not_found: - return f"\nAttribute {obj.name}/|w{attr}|n [category:{category}] = {val}" - error = f"\nAttribute {obj.name}/|w{attr} [category:{category}] does not exist." + if obj.attributes.has(key, category): + if nested_keys: + val = obj.attributes.get(key, category=category) + deep = self.do_nested_lookup(val, *nested_keys[:-1]) + if deep is not self.not_found: + try: + val = deep[nested_keys[-1]] + except (IndexError, KeyError, TypeError): + continue + return f"\nAttribute {obj.name}/|w{attr}|n [category:{category}] = {val}" + else: + val = obj.attributes.get(key, category=category) + if val: + return f"\nAttribute {obj.name}/|w{attr}|n [category:{category}] = {val}" + error = f"\nAttribute {obj.name}/|w{attr}|n [category:{category}] does not exist." if nested: error += " (Nested lookups attempted)" return error diff --git a/evennia/commands/default/tests.py b/evennia/commands/default/tests.py index 8ab5fa50bb..cda2c8f95c 100644 --- a/evennia/commands/default/tests.py +++ b/evennia/commands/default/tests.py @@ -1927,6 +1927,25 @@ class TestBuilding(BaseEvenniaCommandTest): building.CmdSpawn(), "/examine NO_EXISTS", "No prototype named 'NO_EXISTS' was found." ) + def test_setattr_view_with_category(self): + """ + Test checking attributes with a category, including nested attributes. + """ + self.obj1.attributes.add("test", "value", category="cat") + self.call( + building.CmdSetAttribute(), + "Obj/test:cat", + "Attribute Obj/test [category:cat] = value", + ) + + # nested dict + self.obj1.attributes.add("testdict", {"key": "value"}, category="cat") + self.call( + building.CmdSetAttribute(), + "Obj/testdict['key']:cat", + "Attribute Obj/testdict['key'] [category:cat] = value", + ) + import evennia.commands.default.comms as cmd_comms # noqa from evennia.comms.comms import DefaultChannel # noqa