Changeable function call from dict

I am a relatively newbie with a decent amount of experience and I am trying to do a text adventure, I am building a combat system and I want to have an enemy that has different abilities. Instead of re-creating the fight for a different enemy every time, I try to use removable dictionaries for each enemy. My goal is to create a function call that changes depending on the enemy being in battle without hitting objects. I have an example below and would like to know if there is a way to do something like this.

wolf = {'ability': 'bite'}
bear = {'ability': 'claw'}
enemy = {}

def claw():
    print('stuff')

def bite():
    print('different stuff')

def use_ability():
    enemy = wolf
    enemy['ability']()

use_ability()

      

+3


source to share


3 answers


In python functions are first class objects. You can just use them as values ​​in the dictionary.

wolf = {'ability': bite}
bear = {'ability': claw}

      



However, be careful as there is no direct link in python. So make sure you define your functions before assigning them to the dictionary.

def claw():
    print('stuff')

def bite():
    print('different stuff')

wolf = {'ability': bite}
bear = {'ability': claw}

def use_ability():
    enemy = wolf
    enemy['ability']()

use_ability()

      

+5


source


You can do it:

def claw():
    print('stuff')

def bite():
    print('different stuff')

wolf = {'ability': bite}
bear = {'ability': claw}

def use_ability(enemy):
    enemy['ability']()

use_ability(wolf)
# different stuff

      



It really doesn't mean that you should do it this way.

Use object oriented programming. If you only want to use dicts and functions, you should probably write Javascript.

+2


source


I can't help myself, but make a small program explaining how this should be done in an object oriented language .

You should find some guides on how OOP languages ​​work because it will be very helpful when creating a game if you do it this way.

http://www.python-course.eu/object_oriented_programming.php

# This is the SUPERCLASS it holds functions and variables 
# that all classes related to this object use
class Enemy(object):
    # Here we initialise our Class with varibales I've given an example of how to do that
    def __init__(self, HP, MAXHP, ability):
        self.HP = HP
        self.MAXHP = MAXHP
        self.ability = ability

    # This function will be used by both Bear and Wolf!
    def use_ability(self):
        print(self.ability)

# This is our Wolf Object or Class
class Wolf(Enemy):
    # Here we init the class inheriting from (Enemy)
    def __init__(self, ability, HP, MAXHP):
        super().__init__(HP, MAXHP, ability)

    # Here we call the superfunction of this Object.
    def use_ability(self):
        super().use_ability()

# Same as Wolf
class Bear(Enemy):
    def __init__(self, ability, HP, MAXHP):
        super().__init__(HP, MAXHP, ability)

    def use_ability(self):
        super().use_ability()

# How to init Classes
wolf_abilities = 'bite'
w = Wolf(wolf_abilities, 10, 10)

bear_abilities = 'claw'
b = Bear(bear_abilities, 10, 10)

# How to use methods from Classes
b.use_ability() # This will print 'bite'
w.use_ability() # This will print 'claw'

      

+2


source







All Articles