@@ -33,23 +33,42 @@ class EntityDictionary(AutoUnload, dict):
3333 """Helper class used to store entity instances."""
3434
3535 def __init__ (self , factory = Entity , * args , ** kwargs ):
36- """Initialize the dictionary."""
36+ """Initializes the dictionary.
37+
38+ :param callable factory:
39+ Factory class or function used to create missing instances. Set to
40+ `None` to disable this feature.
41+
42+ Factory signature: index, *args, **kwargs
43+ :param tuple args:
44+ Arguments passed to the factory class or function.
45+ :param dict kwargs:
46+ Keyword arguments passed to the factory class or function.
47+ """
3748 # Store the given entity class...
3849 self ._factory = factory
3950
4051 # Store given arguments/keywords
4152 self ._args = args
4253 self ._kwargs = kwargs
4354
44- # Register our OnEntityDeleted listener...
55+ # Register our networked entity deletion listener...
4556 on_networked_entity_deleted_listener_manager .register_listener (
4657 self ._on_networked_entity_deleted )
4758
4859 # Initialize the dictionary...
4960 super ().__init__ ()
5061
5162 def __missing__ (self , index ):
52- """Add and return the entity instance for the given index."""
63+ """Called when an instance is requested but missing.
64+
65+ :param int index:
66+ The index of the entity instance requested.
67+
68+ :raises KeyError:
69+ If the auto-construction of missing instances is disabled or the
70+ factory class or function fails to return an instance.
71+ """
5372 # Get the factory
5473 factory = self ._factory
5574
@@ -61,7 +80,7 @@ def __missing__(self, index):
6180 try :
6281 instance = factory (index , * self ._args , ** self ._kwargs )
6382 except Exception as e :
64- raise KeyError (str ( e ))
83+ raise KeyError (e ). with_traceback ( e . __traceback__ ) from None
6584
6685 # Only cache entities that are not marked for deletion.
6786 # This is required, because if someone request an entity instance
@@ -73,13 +92,17 @@ def __missing__(self, index):
7392 return instance
7493
7594 def __delitem__ (self , index ):
76- """Remove the given index from the dictionary."""
95+ """Removes the given index from the dictionary.
96+
97+ :param int index:
98+ The index of the entity instance being removed.
99+ """
77100 # Remove the given index from the dictionary...
78101 with suppress (KeyError ):
79102 super ().__delitem__ (index )
80103
81104 def from_inthandle (self , inthandle ):
82- """Get an entity instance from an inthandle.
105+ """Returns an entity instance from an inthandle.
83106
84107 :param int inthandle:
85108 The inthandle.
@@ -88,7 +111,11 @@ def from_inthandle(self, inthandle):
88111 return self [index_from_inthandle (inthandle )]
89112
90113 def on_automatically_removed (self , index ):
91- """Called when an index is automatically removed."""
114+ """Called when an index is automatically removed.
115+
116+ :param int index:
117+ The index of the entity instance being removed.
118+ """
92119
93120 def _on_networked_entity_deleted (self , entity ):
94121 """Internal networked entity deletion callback.
0 commit comments