Firstly, you don't need default parm = 0
... parm is a local variable within the function.
That's why his first attempt don't worked :
Code:
def iCorr():
t = Corruption + (whatever - Focus)
[...]
whatever
is an external value that need to be changed everytime.
Code:
def iCorr( whatever ):
t = Corruption + (whatever - Focus)
[...]
label blabla:
$ Corruption = iCorr( 1 )
would have worked.
(By local, I mean it only exists as part of that function... I think RenPy actually calls it something else.
It should also call it local. Python redefined some terms, but local/global kept the same meaning.
But I tend to think in terms of local and global variables. Local ones are almost temporary, global ones exist everywhere.
More precisely it's a question of context.
Imagine your code like a house. You have many context in it. The first context is "whole house", then you have the context "kitchen", the context "living room", and so on.
Global variables are in the "whole house" context, so can be used anywhere on the house. This while local variables are in the "kitchen", "living room", or whatever context, and can only be used in the right room.
The difference is important because there's a whole world outside of the house (Python modules, Ren'py's core modules, ...). A world that create new contexts and where you can also found variables.
But it's relatively out of context, so I'll not go further.
It probably doesn't matter, I think RenPy keeps them in separated variable spaces...
It's Python which do it. It create a "namespace" (not sure it's the term for Python) for the function, and define the parameter inside it as local (to the function) variable.
In short, parameters are local to the function that any variable defined inside the body of this function.
That said... I've no problem defining lots of function that all use the passed variable as "parm" or "parm1, parm2", etc.
It's a matter of understanding more than anything. The parameter should have a name that will talk to you. Which is why I don't like this much your "parm". Here, "value" would be more appropriated, or "step" like I used myself.
So, you have JavaScript and other prototype based languages that use things like 'a', 'b' as parameter, because they are seen as anonymous variables. The order of the parameter depend of the letter. And you have other language that have more explicit names because the parameter are seen as real variables inside the body of the function.
Basically speaking, you should be able to see the declaration of the function and know what value is assigned to what parameter :
def increase( target, step ):
First parameter is the name of the variable to increment, and the second is the value to add.
This versus
def increase( parm, parm1 ):
where you don't know if you should put the name first, then the value, or do the opposite. You'll need to look at the body of the function to see this.
Finally, the function iCorr as it stands right now doesn't actually change the value of Corruption. All it does is return a value calculated using Corruption, Focus and the value passed to the function.
Yeah. I haven't reread the thread, but I should have initially defined it to be used in a screen to display a computed value instead of the direct value.
And no, I've no clue why picking up the value of "Focus" within the function is fine, but altering the value of Corruption requires the store.Corruption.
Because Python don't know what you want to do.
When you wirte
if Focus == 1
, Python look at the local variable, don't found one named
Focus, so it look in the global variable, and use the one it found here.
But when you write
Corruption = 1
, is it an assignation to the global variable named
Corruption or the creation of a local variable named
Corruption ? There's no way to know, so Python fallback to what seem logical, and it create a local variable named
Corruption.
And finally, when you write
store.Corruption = 1
, you don't refer to a variable named
Corruption but to an attribute named
Corruption and part of the
store object. So, Python look at the local variable, and like it don't found an object named
store, it search in the global variables.
That's the trap with Ren'py. What we call "global" variables aren't in fact this global, they are attribute of the
store object. It works fine like this, until the moment we need to use them in functions.