Browse Source

added support for callable default value generators in ParamObj.ParamSet to avoid sharing of modified default values between instances

Darren Ranalli 19 years ago
parent
commit
c155ffa619
1 changed files with 20 additions and 3 deletions
  1. 20 3
      direct/src/showbase/PythonUtil.py

+ 20 - 3
direct/src/showbase/PythonUtil.py

@@ -1131,7 +1131,21 @@ class ParamObj:
     class ParamSet:
     class ParamSet:
         Params = {
         Params = {
             # base class does not define any parameters, but they would
             # base class does not define any parameters, but they would
-            # appear here as 'name': value,
+            # appear here as 'name': defaultValue,
+            #
+            # WARNING: default values of mutable types that do not copy by
+            # value (dicts, lists etc.) will be shared by all class instances
+            # if default value is callable, it will be called to get actual
+            # default value
+            #
+            # for example:
+            #
+            # class MapArea(ParamObj):
+            #     class ParamSet(ParamObj.ParamSet):
+            #         Params = {
+            #             'spawnIndices': Functor(list, [1,5,22]),
+            #         }
+            #
             }
             }
 
 
         def __init__(self, *args, **kwArgs):
         def __init__(self, *args, **kwArgs):
@@ -1166,7 +1180,10 @@ class ParamObj:
         getParams = classmethod(getParams)
         getParams = classmethod(getParams)
         def getDefaultValue(cls, param):
         def getDefaultValue(cls, param):
             cls._compileDefaultParams()
             cls._compileDefaultParams()
-            return cls._Params[param]
+            dv = cls._Params[param]
+            if callable(dv):
+                dv = dv()
+            return dv
         getDefaultValue = classmethod(getDefaultValue)
         getDefaultValue = classmethod(getDefaultValue)
         def _compileDefaultParams(cls):
         def _compileDefaultParams(cls):
             if cls.__dict__.has_key('_Params'):
             if cls.__dict__.has_key('_Params'):
@@ -1380,7 +1397,7 @@ class POD:
         # appear here as 'name': defaultValue,
         # appear here as 'name': defaultValue,
         #
         #
         # WARNING: default values of mutable types that do not copy by
         # WARNING: default values of mutable types that do not copy by
-        # value ( dicts, lists etc.) will be shared by all class instances
+        # value (dicts, lists etc.) will be shared by all class instances
         # if default value is callable, it will be called to get actual
         # if default value is callable, it will be called to get actual
         # default value
         # default value
         #
         #