2 added 7 characters in body
source | link

I'm creating an RPG game where a player has a set of skills, each of which he can level up to improve its effect. Here are two example skills:

  • health: increase player's maximum health
  • regeneration: slowly regenerate player's lost health back

Skills have many similar attributes, like name, description, maximum_level, and some event callbacks which tell what the skill should do, so I figured I should maybe create a class and these would be instances of it:

class Skill:
    def __init__(self, name, description, max_level, level=0):
        self.name = name
        self.description = description
        self.max_level = max_level
        self.level = level
        self.event_callbacks = {}

    def register_callback(self, event_name):
        def decorator(f):
             self.event_callbacks[event_name] = f
             return f
        return decorator

health_skill = Skill('health', 'increase player\'s maximum health', 8)

@health_skill.register_callback('player_spawn')
def on_player_spawn(skill, player):
    player.maximum_health += skill.level * 5

regeneration_skill = Skill('regeneration', 'regenerate player\'s lost health back', 8)

@regeneration_skill.register_callback('time_tick_second')
def on_second_tick(skill, player):
    if player.health < player.maximum_health:
        player.health = max(player.health + skill.level, player.maximum_health)

player.skills.extend([health_skill, regeneration_skill])

And this system works fine, but here's the problem: my game can have multiple players in it, and each of them needs its own instance of each skill. Now I need to somehow figure out a good structure where I can 1. create skills easily 2. create multiple instances of each skill easily

  1. create skills easily
  2. create multiple instances of each skill easily

Any ideas of a good structure? Metaclasses? Subclassing? How do games usually do this?

Edit: This similar structure is used in many popular games like League of Legends, Dota 2, Hearthstone, etc... Another good example of this issue is heroes/champions in such games: each hero is similar and they obviously have a common base, yet each hero needs a lot of custom instance attributes like current health, mana, level, etc.

I'm creating an RPG game where a player has a set of skills, each of which he can level up to improve its effect. Here are two example skills:

  • health: increase player's maximum health
  • regeneration: slowly regenerate player's lost health back

Skills have many similar attributes, like name, description, maximum_level, and some event callbacks which tell what the skill should do, so I figured I should maybe create a class and these would be instances of it:

class Skill:
    def __init__(self, name, description, max_level, level=0):
        self.name = name
        self.description = description
        self.max_level = max_level
        self.level = level
        self.event_callbacks = {}

    def register_callback(self, event_name):
        def decorator(f):
             self.event_callbacks[event_name] = f
             return f
        return decorator

health_skill = Skill('health', 'increase player\'s maximum health', 8)

@health_skill.register_callback('player_spawn')
def on_player_spawn(skill, player):
    player.maximum_health += skill.level * 5

regeneration_skill = Skill('regeneration', 'regenerate player\'s lost health back', 8)

@regeneration_skill.register_callback('time_tick_second')
def on_second_tick(skill, player):
    if player.health < player.maximum_health:
        player.health = max(player.health + skill.level, player.maximum_health)

player.skills.extend([health_skill, regeneration_skill])

And this system works fine, but here's the problem: my game can have multiple players in it, and each of them needs its own instance of each skill. Now I need to somehow figure out a good structure where I can 1. create skills easily 2. create multiple instances of each skill easily

Any ideas of a good structure? Metaclasses? Subclassing? How do games usually do this?

Edit: This similar structure is used in many popular games like League of Legends, Dota 2, Hearthstone, etc... Another good example of this issue is heroes/champions in such games: each hero is similar and they obviously have a common base, yet each hero needs a lot of custom instance attributes like current health, mana, level, etc.

I'm creating an RPG game where a player has a set of skills, each of which he can level up to improve its effect. Here are two example skills:

  • health: increase player's maximum health
  • regeneration: slowly regenerate player's lost health back

Skills have many similar attributes, like name, description, maximum_level, and some event callbacks which tell what the skill should do, so I figured I should maybe create a class and these would be instances of it:

class Skill:
    def __init__(self, name, description, max_level, level=0):
        self.name = name
        self.description = description
        self.max_level = max_level
        self.level = level
        self.event_callbacks = {}

    def register_callback(self, event_name):
        def decorator(f):
             self.event_callbacks[event_name] = f
             return f
        return decorator

health_skill = Skill('health', 'increase player\'s maximum health', 8)

@health_skill.register_callback('player_spawn')
def on_player_spawn(skill, player):
    player.maximum_health += skill.level * 5

regeneration_skill = Skill('regeneration', 'regenerate player\'s lost health back', 8)

@regeneration_skill.register_callback('time_tick_second')
def on_second_tick(skill, player):
    if player.health < player.maximum_health:
        player.health = max(player.health + skill.level, player.maximum_health)

player.skills.extend([health_skill, regeneration_skill])

And this system works fine, but here's the problem: my game can have multiple players in it, and each of them needs its own instance of each skill. Now I need to somehow figure out a good structure where I can

  1. create skills easily
  2. create multiple instances of each skill easily

Any ideas of a good structure? Metaclasses? Subclassing? How do games usually do this?

Edit: This similar structure is used in many popular games like League of Legends, Dota 2, Hearthstone, etc... Another good example of this issue is heroes/champions in such games: each hero is similar and they obviously have a common base, yet each hero needs a lot of custom instance attributes like current health, mana, level, etc.

    Bumped by Community user
1
source | link

Creating instances of an ability when there are multiple different type of abilities

I'm creating an RPG game where a player has a set of skills, each of which he can level up to improve its effect. Here are two example skills:

  • health: increase player's maximum health
  • regeneration: slowly regenerate player's lost health back

Skills have many similar attributes, like name, description, maximum_level, and some event callbacks which tell what the skill should do, so I figured I should maybe create a class and these would be instances of it:

class Skill:
    def __init__(self, name, description, max_level, level=0):
        self.name = name
        self.description = description
        self.max_level = max_level
        self.level = level
        self.event_callbacks = {}

    def register_callback(self, event_name):
        def decorator(f):
             self.event_callbacks[event_name] = f
             return f
        return decorator

health_skill = Skill('health', 'increase player\'s maximum health', 8)

@health_skill.register_callback('player_spawn')
def on_player_spawn(skill, player):
    player.maximum_health += skill.level * 5

regeneration_skill = Skill('regeneration', 'regenerate player\'s lost health back', 8)

@regeneration_skill.register_callback('time_tick_second')
def on_second_tick(skill, player):
    if player.health < player.maximum_health:
        player.health = max(player.health + skill.level, player.maximum_health)

player.skills.extend([health_skill, regeneration_skill])

And this system works fine, but here's the problem: my game can have multiple players in it, and each of them needs its own instance of each skill. Now I need to somehow figure out a good structure where I can 1. create skills easily 2. create multiple instances of each skill easily

Any ideas of a good structure? Metaclasses? Subclassing? How do games usually do this?

Edit: This similar structure is used in many popular games like League of Legends, Dota 2, Hearthstone, etc... Another good example of this issue is heroes/champions in such games: each hero is similar and they obviously have a common base, yet each hero needs a lot of custom instance attributes like current health, mana, level, etc.