attributes vs tags - best use?

360 views
Skip to first unread message

Chuggur

unread,
Mar 16, 2019, 11:31:35 AM3/16/19
to Evennia
Hey so, I've never actually used tags before but was reading up on them and I see that the biggest advantage is that they're much quicker to find (as a group of objects). I'm thinking of moving some things over to tags, but I realized that in all honestly I primarily check specific objects. For example, my game would not broadcast a weather line to all outdoors rooms. Instead, player-characters periodically check their current room for weather updates. Since there are fewer player-characters than rooms, it made sense to write it that way. That said, there MAY be times I want to grab all outdoor rooms - say, if a meteor streaks across the sky as part of some event, I may want to broadcast a message to all players in all outdoor rooms. Right now, the way I would do that is by grabbing all players online, then iterating through and removing the ones currently in indoors rooms.

Anyway, I have two related questions about tags - first and foremost, can/how do you check if an individual object has a tag (ie if a specific location has the outdoors tag)? And second, if the primary use of a flag like outdoors is by-object instead of by-group, is it generally better to use tags or stick to attributes?

Griatch Art

unread,
Mar 17, 2019, 5:09:43 AM3/17/19
to eve...@googlegroups.com
Hi,

The main general difference is that Attributes can hold more complex data than Tags (which is just a name and an optional category). Also Tags are shared between objects whereas Attributes always are unique to a given object.

The example with outdoor rooms is a good one. You can of course do it both ways. If all the meteor does is to be shown visually to a player, then getting all players in outdoor rooms is a good way to not waste echoing to empty rooms. If you want to perform some sort of action (maybe new monsters appear in all outdoor rooms as the meteor passes?) you obviously want to get all relevant outdoor rooms.

Let's assume the outdoor rooms are tagged with a tag with key "outdoors" and category "room_type". I recommend giving a category since that helps to avoid clashes. The default None-category is also a category in itself, so if you specify a category you must give the category whenever you search for the tag in the future, or it will not be found!

To get all outdoor rooms:

outdoor_rooms = Room.objects.get_by_tag("outdoors", category="room_type")

(note that the return is a queryset so you can keep querying with it). To get all outdoor rooms with a player in them:

outdoor_rooms_with_players = outdoor_rooms.filter(locations_set__db_typeclass_path="typeclasses.characters.Character")

(instead of a specific typeclass-path, you can also use a list of valid character typeclasses and subclasses in a similar way we discussed in a previous post). To check if an object myroom has a given tag you can simply use the .tags handler, which sits on every typeclassed object. It tracks all tags on that object and allows you to add, remove and, yes, check if the tag is there.

is_outdoors = myroom.tags.get("outdoors", category="room_type")

Hope that helps!
.
Griatch

Chuggur

unread,
Apr 21, 2019, 11:57:53 AM4/21/19
to Evennia
Thanks Griatch! Is it quicker, processing wise, to fetch objects by tag or by querying with an attribute?

gri...@gmail.com

unread,
May 1, 2019, 9:48:27 AM5/1/19
to Evennia
Tags should be slightly faster due to they being simpler than Attributes (plain string-lookup, no need to pickle anything). An Attribute can of course hold a lot of extra data in itself compared to what a Tag can, but if you only want to use the Tag/Attribute in order to locate an object or group of objects, a Tag should be more efficient.
.
Griatch
Reply all
Reply to author
Forward
0 new messages