Ekamu wrote:I'm guessing dynamic scoped variables are like global variables and lexically scoped variables are like local variables.
Not exactly, but that could be one way of looking at it.
Dynamic scope is possible in LUA, just like in Python but you'd have to take special measures to get that done. Both PLs are considered lexically scoped (along with most modern languages). That "Dynamic" bit is prolly there due to existence of "stack depth" when calling the labels.
It's not easy to explain in simpler terms but what you need to look out for is this:
Code: Select all
default var = 10
init python:
def func():
return var + 5
def func2():
var = 100
return func()
label start:
$ result = func()
"[result]"
$ result = func2()
"[result]"
return
In any conceivable case, without extraordinary measures taken and var remaining the same, func will always return the same 15 answer. And yet, in paramiterized labels:
Code: Select all
default var = 10
init python:
def func():
return var + 5
label start:
$ result = func()
"[result]"
call called
return
label called(var=5):
$ result = func()
"[result]"
return
inside of the label called, func returns 10 instead of 15 as the var is resolved at run-time and has a higher priority for a lookup than the global var with the same name. It should also be noted that the global var in store is not overwritten with this.
Basically you cannot really count on var being bound to something when you write the code as it's evaluation during the run-time may tweak and twist it's value depending on many factors. This can (and prolly will) lead to hard to find errors (which is prolly why I almost never use it).
Not
all code in those labels "magically" becomes dynamically scoped. For example, if it trully was so, one might expect func2 to return 105 instead of 10 when called in label "called" but that is not a case.
=====>>
Just for the record, if you don't understand something, Ren'Py makes it very easy to test stuff as any small project created to illustrate one issue can be ran and tested quite easily. If you don't understand why something is happening or get an unexpected result, that's when you go Q&A
I know that from experience, when you try stuff yourself, it will give you more experience and confidence than any given answer.