In blender, you can store strings, integers, floats, timers, and boolean operators in an object by creating a property in its logic. However, you cannot store lists and dictionaries. I am creating a class, and I can't store a dictionary or a list as you cannot create this in an object's properties. The only method I can seem to think of is the following:

  • Have a module with the properties you want to save. For this example, I will refer to this module as class_properties.py.
  • When creating the class, refer to class_properties.py for properties.
  • In the module that has the class, use import imp and imp.reload(class_properties) in order to always reload the module and get the newest values.

Sure, this may work, but it's messy and nonsensical. With all my properties in my class, I can refer to the game object's properties. For example, I can do the following:

class Player(object):

    def __init__(self, game_object):
        self.game_object = game_object

        self.health = self.game_object["health"]

But if I have a dictionary, I cannot do the following:

class Player(object):

    def __init__(self, game_object):
        self.game_object = game_object

        self.health = self.game_object["health"]
        self.dictionary = dictionary{}

Why can't I do this? Well, the reason I used self.health = self.game_object["health"] is so I can update the player's game object's property health and have the class always have the updated value. If I use self.dictionary = dictionary{}, I would not be able to change dictionary according to the current scenario as self.dictionary would always be dictionary{} without anything in it as it always gets reset, for the __init__ function will always be called.

In other words, I need to have a dictionary in a game object's properties. I can store, for example, integers, in a game object's properties, so integers will always be stored. However, I cannot store a dictionary, so the class will never have the correct dictionary as it always gets reset in the __init__ function.

How can I store a dictionary & a list in a game object the same way I would store, for example, an integer? Thanks.


1 Answer 1


You can store anything in a game property from python. You simply assign it:

obj['inventory'] = {'sword':3, 'armour':5}

It doesn't have to be present in the logic brick window.

Also, you can (and probably should) store the instance of your class rather than regenerate it each frame. You can do this in a number of ways including:

  • Using module mode and a global variable
  • Saving it in a game property
  • Saving it to bge.logic.globalDict

That way, __init__() isn't called every frame

The other thing you can do is you can mutate the game object. The API includes this example:

import bge

class CustomGameObject(bge.types.KX_GameObject):
    RATE = 0.05

    def __init__(self, old_owner):
        # "old_owner" can just be ignored. At this point, "self" is
        # already the object in the scene, and "old_owner" has been
        # destroyed.

        # New attributes can be defined - but we could also use a game
        # property, like "self['rate']".
        self.rate = CustomGameObject.RATE

    def update(self):
        self.worldPosition.z += self.rate

        # switch direction
        if self.worldPosition.z > 1.0:
            self.rate = -CustomGameObject.RATE
        elif self.worldPosition.z < 0.0:
            self.rate = CustomGameObject.RATE

# Called first
def mutate(cont):
    old_object = cont.owner
    mutated_object = CustomGameObject(cont.owner)

    # After calling the constructor above, references to the old object
    # should not be used.
    assert(old_object is not mutated_object)
    assert(mutated_object is cont.owner)

# Called later - note we are now working with the mutated object.
def update(cont):

Which gives the game object the function 'update'


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .