So I have a simple Character class:

class Character:

    def __init__(self, type, level, health, items=None):
        self._type = type
        self.level = level
        self.health = health
        self.items = items if items is not None else []

    def __getattr__(self, attr):
        return getattr(self._type, attr)

And a CharacterType:

class CharacterType:

    def __init__(self, name, max_level, max_health):
        self.name = name
        self.max_level = max_level
        self.max_health = health

    def new(self, *args, **kwargs):
        return Character(type=self, *args, **kwargs)

This allows me to easily spawn multiple instances of the same character type:

t_dragon = CharacterType('Dragon', 30, 2080)
t_mage = CharacterType('Mage', 100, 610)

dragon0 = t_dragon.new()
dragon1 = t_dragon.new(health=437)  # Damaged one
mage0 = t_mage.new(level=50)

But now I need to store these into a database table, and be able to somehow identify the character's type. I can't create a separate table for each type, since these types will be manageable by designers through JSON and thus new types can be added dynamically and old types be removed. So as it stands now, the above characters would populate the character table like so:

 id | level | health | type_identifier
 0  | 0     | 2080   | ????
 1  | 0     | 437    | ????
 2  | 50    | 610    | ????

I would highly prefer if there's a solution which doesn't require the designers to have to come up with an unique ID themselves, so while something like this would work, where the designer comes up with an unique ID:

    "name": "Dragon",
    "max_level": 30,
    "max_health": 2080,
    "unique_id": "#dragon_2017-04-12"

and keeps it even if they change the character's name, it's very bothersome to have to maintain something like this in the JSON itself.

How can I get a string/integer/value to identify the character types (Python objects) from each other, in a consistent manner between server restarts, without the designer having to manually come up with an unique ID?


1 Answer 1


I would suggest using GUID - it is widely used and pretty much every language/lib has an implementation, python included.

A common practice is to express it as hexadecimal string, possibly also truncate generated GUID to a shorter one (whether that is wise depends on particular case). You can also consider using GUID in conjunction with a "friendly" name, an alias that can be set manually.

An alternative would be to use hash, it is not trivial to create good hash function however - especially in context of game objects, where even for duplicates different hash might be needed.


You must log in to answer this question.

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