13 Replies - 2344 Views - Last Post: 19 August 2012 - 05:04 AM Rate Topic: -----

#1 Akasen  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 13-August 12

Setting Max Health limit

Posted 13 August 2012 - 09:07 PM

So I have for a while been working on a text-based RPG as a way to learn Python and programming in general. I have been going at my study of the Python language for a good eight or so months now but I have never had particularly good help in figuring out what I am doing or how to get what I am doing to work. Much of my help in my Python endeavors have actually come from this site in general and so I decided that becoming a member and ask around as I continue this project of mine.

At the moment, I am having an issue with keeping a players health from going above the max set health. This issue occurred to me after I had gotten combat working and came to the conclusion of making equipment and items that a character could equip and use. And that is when I realized that there was nothing keeping the health from going over the max health.

#This is the character template that PC's and NPC's have.
#NPC's and the PC will have their own variables and attributes
#to further differentiate them.

class Character(object):
    def __init__(self, name, max_health, health, strength, stamina, toHit):
        self.name = name
        self.max_health = max_health
        self.health = health
        self.strength = strength
        self.stamina = stamina
        self.toHit = toHit

def max_health():
    
    if self.health => self.max_health:
        self.health = self.max_health

        #Health can never go above max health




My thought on a solution was to do an if statement that would check if the characters current health is above the max health that the health would then be set to the max health so it basically acted as a limit.

In my tests I would then set a variable to something like 'test = Character('John', 10, 10, 10, 10, 10)' and then increase the health by some number by doing 'test.health += 1' and then entering 'test.health' to see if my code worked.

That didn't work either. I will say that I originally had the if statement under the __init__ as well but then put it into it's own method to see if that worked. Neither have been working at all it seems.

I have also tried not setting any variable to self and have gotten no result either.

I am at my wits end as to how to get this working and would very much enjoy some assistance as I make my steps in learning how to program.

Is This A Good Question/Topic? 1
  • +

Replies To: Setting Max Health limit

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Setting Max Health limit

Posted 13 August 2012 - 09:51 PM

Hey boss, welcome to DIC. This isn't a really bad issue, I'd suggest making a function that would look something like this:
def recover_health(self, amount):
    if self.health + amount > self.max_health:
        self.health = self.max_health
    else:
        self.health += amount



Whenever you want to increase the NPC's current health, you would call on this function and it would ensure that his health never exceeds the max. There are other ways to solve this problem, but this quick and easy solution will get you past this trouble spot... you basically already had it in your max_health method!

EDIT:
if you want to block the situation of test.health += 5, you're going to need to create a method called
__setattr__(self, name, value)
More information can be found, here.

This post has been edited by atraub: 13 August 2012 - 10:17 PM

Was This Post Helpful? 1
  • +
  • -

#3 Akasen  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 13-August 12

Re: Setting Max Health limit

Posted 13 August 2012 - 11:07 PM

After much tinkering and understanding your post, I am now happy to say that I am on the right track in regards to having this issue sorted out. Thank you so much atraub for your input. I realized now that I kept thinking that I kept assuming that my Max_Health function would automatically act as soon as health was higher than the max health. I somehow think I might actually move the recover_health function and use it in health items instead. But time will tell.

So as of right now, I am looking into __setattr__. And I have never seen this before and have little understanding of how to make it work or even what it does.

What I have at the moment looks like this

class Character(object):
    def __init__(self, name, max_health, health, strength, stamina, toHit):
        self.name = name
        self.max_health = max_health
        self.health = health
        self.strength = strength
        self.stamina = stamina
        self.toHit = toHit

    def __setattr__(self, health, value):
    
def recover_health(self, amount):
    if self.health + amount > self.max_health:
        self.health = self.max_health
    else:
        self.health += amount


Was This Post Helpful? 0
  • +
  • -

#4 Tayacan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 275
  • Joined: 18-January 11

Re: Setting Max Health limit

Posted 14 August 2012 - 02:43 AM

Uhm... Two things...

First, you don't need to use both methods - you can delete the __setattr__ one, since it's not complete anyway, and just go with your recover_health method.

Second, the recover_health method needs to be indented in order to be part of the class.

class something:
    def this_is_part_of_the_class(self):
        # In this method, self refers to some
        # instance of the class 'something'.

def this_is_not_part_of_the_class(self, blah):
    # In this function, self is just another parameter,
    # because it's not part of a class.


Was This Post Helpful? 1
  • +
  • -

#5 Akasen  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 13-August 12

Re: Setting Max Health limit

Posted 14 August 2012 - 07:26 AM

Why would I do that, the function recover_health works fine as it is and where it is. To my understanding, it only seems to be called upon when implicitly called and can only work in such a way.

From what I have done, if I made

test = Character('John', 10, 10, 10, 10, 10)

The function recover_health when entered into the prompt as

recover_health(test, 5)

will give 5 health to 'test'. 'test' though is already max though and as such recover_health does not update the health above the max health
Was This Post Helpful? 0
  • +
  • -

#6 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Setting Max Health limit

Posted 14 August 2012 - 09:09 AM

Tayacan hit the nail on the head, you really do want to indent, here's what it accomplishes:

class Character(object):
    def __init__(self, name, max_health, health, strength, stamina, toHit):
        self.name = name
        self.max_health = max_health
        self.health = health
        self.strength = strength
        self.stamina = stamina
        self.toHit = toHit
    
    def recover_health(self, amount):
        if self.health + amount > self.max_health:
            self.health = self.max_health
        else:
            self.health += amount



now, when we use the interpreter, here's what you can do:
>>> test = Character('John', 10, 10, 10, 10, 10)
>>> test.recover_health(10)



It's considered a cleaner style, and later on down the line if you utilize inheritance, it will save you some headaches too. This style makes recover_health exists inside of the NPC class rather than external of it.
Was This Post Helpful? 0
  • +
  • -

#7 grimofdoom  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 14-August 12

Re: Setting Max Health limit

Posted 14 August 2012 - 05:01 PM

I know I am a newbie at python and all.But couldn't you just use:
if health>=max_health:
   health=max_health


To me it seams very simple for setting and "enforcing" the max_health.
Was This Post Helpful? 0
  • +
  • -

#8 Akasen  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 13-August 12

Re: Setting Max Health limit

Posted 14 August 2012 - 06:19 PM

Ah, my bad. I never knew that was how I would call on methods within a Class. That also fixes an issue I may have run into in the future had I not indented it.

I guess for now I will just stick to using this recover_health function for all things related to health recovery.
Was This Post Helpful? 0
  • +
  • -

#9 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Setting Max Health limit

Posted 14 August 2012 - 09:16 PM

View Postgrimofdoom, on 14 August 2012 - 08:01 PM, said:

I know I am a newbie at python and all.But couldn't you just use:
if health>=max_health:
   health=max_health


To me it seams very simple for setting and "enforcing" the max_health.


2 problems.
1.) That's nice and all, but you gotta know where to put it. When does that condition get hit?

2.) For simple applications, that approach is fine. However, between the time when the NPC's health is incremented and the time moment that condition gets hit, the NPC's health can be greater than max health. In a situation where there's threading and other pieces of the game might be relying on that data, this can create problems. That's why I chose to use the condition in my previous post, which never allows the health to exceed the maximum.


EDIT:

You could even do something like...
    def adjust_health(self, amount):
        if self.health + amount > self.max_health:
            self.health = self.max_health
        elif self.health + amount <= 0:
            #handle death here!
        else:
            self.health += amount



negative values can represent damage and positive ones can represent recovery. Just a passing thought. It may prove annoying to be forced to make damage negative values every time.

This post has been edited by atraub: 14 August 2012 - 09:17 PM

Was This Post Helpful? 0
  • +
  • -

#10 grimofdoom  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 14-August 12

Re: Setting Max Health limit

Posted 14 August 2012 - 10:16 PM

I did say I was a newb. And that's what (the only thing) I hate about python,that it is read line by line. Hopefully in D++ or whatever they are going to call it, they will at least add a function that runs simultaneously with the script without interfering with the main script.
Was This Post Helpful? 0
  • +
  • -

#11 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Setting Max Health limit

Posted 14 August 2012 - 10:30 PM

Most languages (and every one that I've ever worked with) executes code procedurally, 1 line at a time, unless you explicitly tell it otherwise, typically with threading. You'll hit that 1-line-at-a-time gripe with C/C++, Java, C#, Ruby, you name it. As for threading, it's a pain in the ass, so the general rule is don't use it unless you have a good reason to do so.

This post has been edited by atraub: 14 August 2012 - 10:36 PM

Was This Post Helpful? 2
  • +
  • -

#12 Akasen  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 13-August 12

Re: Setting Max Health limit

Posted 16 August 2012 - 08:28 AM

I kinda have this want to bring the topic of my little learning project into it's own thread so thus I can ask questions in regard to what I am working on rather than asking on specific bits. Could also be great material for someone else if they seek answers to a similar question.
Was This Post Helpful? 0
  • +
  • -

#13 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Setting Max Health limit

Posted 16 August 2012 - 11:11 AM

I would have no objections to this! There is technically a projects forum but considering that this project is centered around learning and mastering Python, I'd say that makes a strong case for putting it on the Python forum. You could also start a blog on Dream In Code revolving around your project :)

No matter what you decide, keep us posted!
Was This Post Helpful? 0
  • +
  • -

#14 Akasen  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 13-August 12

Re: Setting Max Health limit

Posted 19 August 2012 - 05:04 AM

So I did start a blog and posted two entries related to the project I am working on. I may still go ahead during this week and possibly make a thread related to my work to get input from the forums and use the blog to log information and progress.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1