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.
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'.
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).
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.
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.