0

Ive created a function that randomly generates mobs for a game making. The function works and produces an unique monsters, BUT, I can't seem to use the returned values as the values for the Mob Class. Code is as follows

import random as r
import math

# Define base character class amd attributes
class Base:
    def __init__(self, name, lvl, hp, strength, defense, agility, wpn):
        self.name = name
        self.lvl = lvl
        self.hp = hp
        self.strength = strength
        self.defense = defense
        seld.agility = agility
        self.wpn = wpn

# Set Mob class
class Mob(Base):
    def __init__(self, *args):
        super().__init__(*args)
        
#Mob Names
m_names = ["Goblin", "Minotaur", "Imp", "Rat", "Chimera", "Bandit"]

h_lvl = 1
def CreateMob():
    stats ={}
    stats["name"] = r.choice(m_names)
    stats["lvl"] = r.randint(h_lvl, h_lvl+2)
    stats["hp"] = r.choice(r.sample(range(stats["lvl"]*8, stats["lvl"]*16), 4))
    stats["strength"] = r.randint(math.ceil(stats["lvl"]/3), math.ceil(stats["lvl"]/2))
    stats["defense"] = r.randint(math.ceil(stats["hp"]/25), math.ceil(stats["hp"]/20))
    stats["agility"] = r.randint(math.ceil(stats["lvl"]/4), math.ceil(stats["lvl"]/2))
    stats["wpn"] = round(r.uniform(1, ((stats["lvl"]*0.0625)+1 )), 2)
    
    
    return list(stats.values())   #Edited to list values, which is what I was working with
    
for i in range(1):
    print(Mob(CreateMob()))

The traceback after says I'm missing 6 positional arguments which means it's using 'name' but not the rest. Why?

  • You wanted `**kwargs` (i.e. for passing a dictionary around to populate named arguments) not `*args` (i.e. for passing a list around to populate positional arguments) in `Mob`. You also need to do `Mob(**CreateMob())` to explode the dictionary into being the arguments. – Kemp Dec 02 '21 at 17:01
  • Note that dictionaries aren't semantically ordered data structures (although they do retain insertion order in recent versions of Python) - if you want a list from `CreateMob`, why not _create a list_? Or use `**kwargs` instead of `*args`. Also the `Mob` class seems pointless in general. – jonrsharpe Dec 02 '21 at 17:01
  • `print(Mob(*CreateMob()))` Final line should be this, so you can unpack the list into positional args. It's thinking you're only passing `name` cause it's not unpacking. – foxyblue Dec 02 '21 at 17:04
  • What's the purpose of `Mob`? It doesn't add anything to `Base`. A function like `CreateMob`, though, is a perfect candidate for a class method like `Mob.create`, which generates the values and passes them to `Mob` itself. – chepner Dec 02 '21 at 17:08
  • It's funny because I hesitated on having the mob class in this version altogether. I was trying to use kwargs but in my other version it worked fine as args. Thanks everyone I'll give it a shot – Justice Brinston Dec 02 '21 at 17:09
  • I already had the base, mob, and player classes in another version but I was trying to make the CreateMob generator. Very valid point that it should be a class method. The mob class has its own attack method already and that's really the only reason for it. – Justice Brinston Dec 02 '21 at 17:13

0 Answers0