0

Im teaching myself python and I've come upon a snag in a simple game project I'm working on.

I would like to keep the players stats in a different module from the rooms that are being run by the game engine. Problem is when I try to set a Playerattribute from a different module, it doesn't save the new attribute and instantiates the original attribute.

here is the Playerclass in the entities module

class Player(object):

    def __init__(self):

        self.name = ' '
        self.hp = 0
        self.current_hp = 0
        self.strength = 0
        self.dexterity = 0
        self.constitution = 0

And here is how im trying to manipulate and test the attributes in the rooms module

class CharacterCreation(Scene):
    def enter(self):
        character = entities.Player()
        character.hp = 10
        print character.hp
        return 'barracks'

class Barracks(Scene):
    def enter(self):
        character = entities.Player()
        print character.hp
        return 'shop'

When I test this with the rest of my code, here is what I get.

-------------------------------------------------------------------------------


10
-------------------------------------------------------------------------------


0
-------------------------------------------------------------------------------

So what am I missing here? I thought I could set that attribute using =but it seems I'm mistaken? the first time I did it, it worked, but then how do i get python to set the new value of hp to 10?

2 Answers 2

2

You're creating a new Player object in each scene, changing its attributes, and then throwing it away.

You should be explicitly passing one single player into each scene:

def enter(self, player):
   ... do something with player ...
Sign up to request clarification or add additional context in comments.

3 Comments

wouldn't I have to call it as self, rooms.Player() as opposed to player? And would I have to impliment that into the code for each room?
No. You seem to have some strange misunderstandings about object-orientated programming. There's nothing specific about Python here: you should read an introduction to OOP.
Yeah Im new to programming in general and could use a better understanding of OOP, I will try to find some good introductions, thanks!
1

It looks like you're creating a new Player instance on every enter method...

If you're going to have only one player in the game, you could have it as a global variable (usually not very good idea) or even better, as a singleton class: http://blog.amir.rachum.com/post/21850841339/implementing-the-singleton-pattern-in-python

I made some tweakings to the code. It adds the PlayerPool class (which is more like a cache, actually). It may give you some ideas :)

#!/usr/bin/env python
#http://stackoverflow.com/questions/14629710/python-setting-attributes-from-module-to-module/14629838#14629838

class Player(object):
    def __init__(self):
        self.name = ' '
        self.hp = 0
        self.current_hp = 0
        self.strength = 0
        self.dexterity = 0
        self.constitution = 0

class PlayerPool(object):
    _players = dict()

    @classmethod
    def getPlayerByName(cls, name):
        if not name in cls._players:
            newPlayer = Player()
            newPlayer.name = name
            cls._players[newPlayer.name] = newPlayer
        return cls._players[name]


class Scene(object):
    pass

class CharacterCreation(Scene):
    def enter(self):
        character = PlayerPool.getPlayerByName("foobar-hero")
        character.hp = 10
        print "%s has %s points of hp" % (character.name, character.hp)
        return 'barracks'

class Barracks(Scene):
    def enter(self):
        character = PlayerPool.getPlayerByName("foobar-hero")
        print "%s has %s points of hp" % (character.name, character.hp)
        return 'shop'

if __name__ == "__main__":
    step1 = CharacterCreation()
    if step1.enter() == "barracks":
        step2 = Barracks()
        step2.enter()

That outputs:

borrajax@borrajax-comp:~/Tests/Python/Stack Overflow$ python ./players.py 
foobar-hero has 10 points of hp
foobar-hero has 10 points of hp

Welcome to python. I'm sure you'll find it has really cool features... such as the ability to return functions, or pass functions as parameters, inspect the classes defined in any module... Looks like things you could find useful.

9 Comments

So every time I call Player, Im calling a completely new version of it? crazy, I'm having a little trouble understanding your link though, if i follow those steps with my code, does that mean it will only make a single instance of player? also, what would be so bad if I made it a global variable but contained in its own module? This also worries me though when i start to implement monsters.
Yep, you're creating a new one... Don't try to copy the code in the singleton pattern... Just investigate it... About global variables... well: c2.com/cgi/wiki?GlobalVariablesAreBad :)
This works, but I fear you're trying to teach the OP to run before he can walk.
Yeah I think I'm going to study this more and just use Globals for now, from what I'm reading since I'm keeping the globals contained to one module I should be good for the small scope of my project, thanks again guys!
The variable will remain "the same" (to say so) as long as you're running your program inside the same Python interpreter (which for what we're talking about here, will be your code's whole execution). Once you stop your python interpreter, the variable will be destroyed. If you restart the program (call it again), it will start from 0 (...unless you save your player in a file and load it when your program starts... but that's something else... )
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.