Displaying attack damage

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
TellerFarsight
Veteran
Posts: 230
Joined: Sun Jun 04, 2017 8:09 pm
Projects: Vora, Secrets Untold
Location: Toronto, ON
Contact:

Displaying attack damage

#1 Post by TellerFarsight »

Is there a way to access, in a screen, a variable that I've defined within a python object function? I've tried something like this and it tells me 'damage is not defined'.

Code: Select all

def attack(self, enemy):
            damage = randint(1,self.att) - enemy.def
            if damage >=0 and enemy.hp != "DEAD":
                enemy.hp = enemy.hp - damage
            if enemy.hp <=0:
                enemy.hp = "DEAD"
                
screen dam:
    text "[damage]"
Current Project: Vora
Also Check Out: Devil Survivor [Reverse-Engineered]

User avatar
RicharDann
Veteran
Posts: 286
Joined: Thu Aug 31, 2017 11:47 am
Contact:

Re: Displaying attack damage

#2 Post by RicharDann »

A question: You use enemy.def, does the game not crash because of this? def is a reserved keyword meant to define functions, no?

I had this very same problem not long ago, it's because damage is a local variable, that means, that it only exists within the function, and within the object from where it was called. So to show it as text you would have to find a way to export it out of the function, like storing it in another, global variable (wich is what I've been doing so far).

Code: Select all

default damage = 0 #A global variable to hold the damage number


#This method I imagine would be inside the battler's class.
def attack(self, enemy):
            global damage #This makes it so you can access the global variable
            damage = randint(1,self.att) - enemy.dfs #Had to change this to dfs, def wouldn't work
            if damage >=0 and enemy.hp != "DEAD":
                enemy.hp = enemy.hp - damage
            if enemy.hp <=0:
                enemy.hp = "DEAD"

screen dam():

    text "[damage]" #Since damage is global variable defined with default, the game can show it no problem.
The most important step is always the next one.

User avatar
TellerFarsight
Veteran
Posts: 230
Joined: Sun Jun 04, 2017 8:09 pm
Projects: Vora, Secrets Untold
Location: Toronto, ON
Contact:

Re: Displaying attack damage

#3 Post by TellerFarsight »

That works well, thanks! I'm very new to python and didn't really know how to use global/local; I assumed the local variables would work just like accessing the object attributes. My actual code had attack and defense as slightly more complicated math, so I decided to just simplify it here in the Lemma textbox to def, without thinking about it.
Current Project: Vora
Also Check Out: Devil Survivor [Reverse-Engineered]

Post Reply

Who is online

Users browsing this forum: No registered users