Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9243333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:47:20+00:00 2026-06-18T08:47:20+00:00

Im teaching myself python and I’ve come upon a snag in a simple game

  • 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:47:21+00:00Added an answer on June 18, 2026 at 8:47 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am teaching myself some Python and I have come across a problem which
I'm teaching myself Rails, and as a test project I'm mocking up a simple
Teaching myself Perl with Project Euler. Anywho, print Hei if 1==1; works like a
I am self teaching myself some python OOP, and I have created a simple
I'm teaching myself Python 3.2 and I'm trying to make a program to match
I am teaching myself python. I was thinking of small programs, and came up
As part of teaching myself python I've written a script which allows a user
I'm teaching myself Python and I was translating some sample code into this class
I've been teaching myself python and cgi scripting, and I know that your basic
I'm a Python(3.1.2)/emacs(23.2) newbie teaching myself tkinter using the pythonware tutorial found here .

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.