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.
Most of the time this works fine. However, when using it with variables belonging to an npc object to get the ratio of "good" interactions with the player vs "bad", it always returns 0.
I think it's a matter of scope, but I'm not sure, or how to let the ratio function access the npc object's variables (call them npc.goodInteraction and npc.badInteraction).
Is there a way to make this work, or will I need to create global NPCGoodInteraction and NPCBadInteraction variables for each character?
If you are passing the variables into the function or method, the variables will become aliases to the passed variable, meaning that should you alter the value you would only alter the alias and not the original.
Without seeing your npc objects or knowing why rat is calculated rather than returned I'd only be guessing at a best approach... Have you considered adding a @property method (or just a get_ratio(self) method) to the npc class and just letting it return a ratio?
Stupid question - are the npc variables integers or floating point numbers? Python 2 still implements integer division as a “floor” operation, so that 5/2=2, not 2.5. This was one of the significant changes in Python 3. Thus, if x and y are both integers, x/(x+y) will always return 0 unless y is zero, at which point it will return 1.