A python hiearchy Oriented Object quesiton.

Hi there :wink:

Currently facing a problem which is hard to explain but I’ll try :
I’m having five files :

  • main.py
  • Hero.py
  • BadGuy.py
  • FireBall.py
  • Level.py

Main is importing Level and Hero.
Level is importing BadGuy
Both Hero and BadGuy are importing FireBall

Well, here my comprenhension problem start.

Problem 1:
BadGuy and Hero are, off course, supposed to fight each other using FireBall but there is no direct link between BadGuy and Hero.
How am I supposed to do that link to remove life points to each other ?
(This is an OO question but I’m sure this is an easy one for you guys who are accustom to these :wink: )

When BadGuy is launching a FireBall on Hero and missing him. I red on the forum that the FireBalls need to be deleted to avoid not needed calculation on far objects.
So, I included in the task manager a condition which delete the FireBall if it’s too far from its sender using:

del self

but I noticed that not all FireBalls were deleted… Specially the ones generated before the previous was deleted!
I’m quiet sure that this problem is related to the first one but not finding a solution.

Help is (as usual) welcome :slight_smile:
Thanks in advance.

How your thing should work is that your main class should load most other objects.

import Hero.py
import BadGuy.py
import FireBall.py
import Level.py

Then when fire ball is created have it be created in main. Then when its time to delete the fire ball remove it. Don’t ever use “del self” that does not do what you think it does. Don’t forget to remove the created NodePaths of the objects.

What I like to do is to attach a .node to each object and each object to a “level.”

Thanks for the answer treeform,

Oups!! That will be many corrections to do and my whole structure to redesign.
Here’s the basic structure I’m thinking to switch to :

class Hero():
     def attack(self, BadGuy):
        if Monster.stillAlive:
            Monster.life -= 10

class BadGuy():
    def attack(self, Hero):
        if Hero.stillAlive:
            Hero.life -=  10

I’m hesitating to pass Hero to BadGuy as argument and the other way arround to keep my structure working. I’m affraid that somewhere a loop will be created that I’ll not see directly!
Isn’t there an obvious way to keep following my first design or the one in the code above ?

Didn’t know that… Okay, I’ll use myObject.removeNode() starting now to delete my objects.

I’m badly following that idea! What is the benefit of attaching a .node to BadGuy, Hero and FireBall ?

Your classes look nearly the same. Why don’t you abstract them a bit? Example:

class Avatar(object):
    def __init__(self, name, model):
        self.name = name
        self.model = Actor(model)
        self._health = 100
        self.health = property(self.getHealth, self.setHealth)
    def attack(self, target):
        if isinstance(target, Avatar):
            target.health -= 10
    def self.die(self):
    def getHealth(self):
        return self._health
    def setHealth(self, amount):
        self._health = amount
        if self._health > 100:
            self._health = 100
        elif self._health <=0:


import Avatar
hero = Avatar("mighty guy", "hero")
villain = Avatar("evil guy", "dr_evil")


Or use the power of inheritance.

That was not my code, just a basic exemple I quickly typed :slight_smile:
I’m blocked because I’m not able to do the link between my objects.

In my Hero class, I’m having a function named heroFire :

  def heroFire(self):
        if self.state["isFire"]==0:
            heroPos= self.hero.getPos()
            self.fireBall= Magic.FireBall(heroPos, heroPos+5)

The last line is calling an other file “Magic.py” containing the class FireBall taking two aguments : the initial position and the target position.
No problem for the initial position as I’m in my Hero class but the target position is suppose to be the BadGuy’s position which is inclueded in an other file BadGuy.py

And that’s my problem, I would like to have the position of the BadGuy within the Hero class and I don’t know how to do that!

give the target as argument and extend your method to fire at that target.

see my snippet


if isinstance(target, Avatar):

This is checking if target is an instance of Avatar, right ? But it’s suppose to be an instance of BadGuy.

Well, this is what I’m not able to do. Hero is loaded at the very begining and as he’s moving in the world BadGuys are being loaded. So, I can’t instance any BadGuy to my Avatar!

Sorry to insist but I really can’t see how to do it.
Thanks for your patience.