Best practices for unordinary Properties of Objects

Hello all,

Timer, float, integer, string, boolean are the property types of an object. If I wanted to use a matrix as a property instead, how would I initialize it as a property of the object? I suppose I could put it in the controller for an “always” sensor and find a way to execute it only once, but I was wondering if there was a better way to do it.

Maybe somehow run a script using the PythonController.use_priority flag? There doesn’t seem to be much documentation on that though…

Thanks,
ejang

You could set a Python controller to initialize by ticking the flag on it and connect an Always sensor with no pulse settings attached to it to force it to run just once. Or, you could just initialize the matrix or list that you need at the top of the script and use it below.

Thanks SolarLune! It worked just fine.

You can see properties in two ways:

External Properties:

  • declared at the GUI
  • usable at logic bricks
  • editable via GUI
  • editable via Python
  • supports boolean, integer, float, string and the special type: timer

Internal Properties:

  • declared via Python
  • editable via Python (not via GUI)
  • usable at logic bricks only if setup via Python (not via GUI)
  • supports any Python data type
  • does not support timer type (as it is not a python data type)

Be aware if you declare an internal property with a name of an already existing external property (but different type) you delete the internal property.

To your question:

  • external properties are declared at design time (before the game starts)
  • internal properties should be declared shortly before needed but have to be declared before used

example:


INITIAL_SCORE = 0

<b>def createAndUseProperty(cont):</b>
  myObject = cont.owner
  
  scoreValue = myObject.get("score") # read it

  if scoreValue is None: # not present
    scoreValue = INITIAL_SCORE
    myObject["score"] = scoreValue #declare the property

  myObject["Text"] = scoreValue # use the property

you can indeed execute a separate Python call to establish the initial values:


<b>def createProperty(cont):</b>
  myObject = cont.owner
  
  myObject["score"] = INITIAL_SCORE #declare the property

<b>def useProperty(cont)</b>:
  myObject = cont.owner
  
  scoreValue = myObject["score"] # read it -&gt; Error if not present

  myObject["Text"] = scoreValue # use the property


I recommend to use external properties if you want to configure initial values via GUI (which is very handy) and internal properties if the existence of the property is not important outside of the used code.