Hello all. I am trying to write a short little game, mainly to boost my knowledge of classes and objects, and of course I am starting to encounter problems.

I have several classes for different types of creatures in this game. Most of them take the form of:

class human:
    loc = location()
    sta = lifedata()
    isplayer = False

    nhealth = 20.0
    nhunger = 0.0
    nenergy = 10.0 - nhunger
    nspeed  = (((nenergy/10)*7.0) + 3.0)

I can create objects in the shell using these classes and they seem to work quite well, the problem is when I try to create objects in functions:

def creation():
    gameplayer = human();
    gameplayer.isplayer = True;

    goblin1 = goblin();
    troll1  = troll();
    human1  = human();
    bear1   = bear();

This is the result I get:

IDLE 2.6.5      
>>> ================================ RESTART ================================
>>> 
>>> creation()
>>> goblin1

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    goblin1
NameError: name 'goblin1' is not defined
>>> gameplayer

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    gameplayer
NameError: name 'gameplayer' is not defined
>>>

What am I doing wrong?

Notihing except trying to access local variables of function, which do not exist outside the scope of function. you must return them or for example append to list of creatures passed as parameter.

First, there are two types of variables in classes, attributes and instance variables. I think you want instance variables, so you can have more than one human for example, with separate values for each one.

class Human:
    def __init__(self):
        self.isplayer = False
        self.nhealth = 20.0

    def health_add_one(self):
        self.nhealth += 1

## 2 separate humans
instance_1 = Human()
instance_2 = Human()

instance_1.health_add_one()
print instance_1.nhealth
print instance_2.nhealth

Next, store each instance in a dictionary or list that you can pass around the program.

Edited 5 Years Ago by woooee: n/a

Comments
agree with you

Thanks for the responses. I am sorry, but I am a bit new at this: why would I use the def__init__(self)? Also I am trying to create a look which creates dynamic objects. Mainly:

while creaturecreation < 20:
        exec 'goblin%s = goblin()' % creaturecreation
        exec 'goblin%s.number = %s'% (creaturecreation, creaturecreation)
        exec 'return goblin%s' % creaturecreation

        exec 'troll%s = troll()' % creaturecreation
        exec 'trolls%s.number = %s'% (creaturecreation, creaturecreation)
        
        creaturecreation = creaturecreation + 1

Except I am having the same problem, even when I have this code outside of a function. It simply doesn't create the objects with the names. I want.

>>> 

Traceback (most recent call last):
  File "/Users/karlnotturno/Desktop/Goblins and Trolls/Newversion.py", line 99, in <module>
    exec 'trolls%s.number = %s'% (creaturecreation, creaturecreation)
  File "<string>", line 1, in <module>
NameError: name 'trolls0' is not defined
>>> gameplayer
<__main__.human instance at 0x25938a0>
>>> Newworld
<__main__.world instance at 0x2593850>
>>> goblin1

First, there are two types of variables in classes, attributes and instance variables. I think you want instance variables, so you can have more than one human for example, with separate values for each one.

class Human:
    def __init__(self):
        self.isplayer = False
        self.nhealth = 20.0

    def health_add_one(self):
        self.nhealth += 1

## 2 separate humans
instance_1 = Human()
instance_2 = Human()

instance_1.health_add_one()
print instance_1.nhealth
print instance_2.nhealth

Next, store each instance in a dictionary or list that you can pass around the program.

You need to think about the scope of variables. As a general rule, you don't want global variables (though global constants are much more acceptable) since the presence of global variables makes your program harder to understand ("where in the heck did that variable change value?")? As a result, most interactive games have a single container called the "game" or the "board" or "map" or whatever that holds information about the current state of the game: What objects exist, and their locations. The objects themselves know their own state.

In your main function, you create the "world" then enter the game loop: Pass it around to the various other functions which examine it, update it and return it to the main loop, which examines it, lets it report its state, and sends it off again.

This question has already been answered. Start a new discussion instead.