Can you explain why "for" does not work without python block?
RenPy itself doesn't have a
for
command/statement for normal script coding (although it is supported within the
screen
language).
To do anything like that, requires that you ask python to do it for you. Hence the
$
or alternatively
python:
code blocks.
It should be noted that
default
is only "create this variable for later use". All
default
statements are processed by RenPy before the game actually starts. When it's running, they are ignored.
The other thing is that as far as I can see, you aren't actually creating variables that python can loop around.
For example, you have
lt1
thru
lt10
. But those are 10 separate variables that are completely independent of each other. You as a human can see the connection, but programming languages can only follow their internal rules.
If you check my .sig you'll see a link to a guide I wrote for lists. I'm imagining something that might look more like:
Python:
default lt_list = [ "Name1", "Name2", "Name3", "Name4", "Name5", "Name6", "Name7", "Name8", "Name9", "Name10" ]
default mt_list = []
label start:
scene black with fade
"*** START ***"
label new_day:
$ mt_list = lt_list[:]
$ renpy.random.shuffle(mt_list)
python:
for i in range(10):
renpy.say("Nobody", "Person #" + str(i) + ", is " + mt_list[i] + " today.")
"*** THE END ***"
return
Some further explanations...
$ mt_list = lt_list[:]
is effectively "copy lt_list to mt_list".
Without the
[:]
, python only creates a
reference called
mt_list
which points at
lt_list
.
Think of it like creating an alias rather than creating a new object/variable.
The brackets are to specify "from" and "to" when copying a list. If you omit the "from" number, if defaults at the first item. If you omit the "to" number, it defaults to the last item. So
[:]
can be thought of as "from first to last". In this case, it's handy way to force python to create a new variable rather than just a reference.
The problem of randomly shuffling a reference is that BOTH variables/lists are linked, so both would get shuffle (at least from your point of view).
renpy.random.shuffle
randomly shuffles (and overwrites) an existing list. It's why I'm copying lt_list to mt_list before doing anything else with it. Though it really depends on whether you need separate randomized and unrandomized lists.
$
is effectively "do python command".
python:
is also "do python command", except it processes all the lines below that are part of the code block (the indenting).
Generally you'd use
$
for a single line or a couple of lines (with multiple lines starting with
$
).
When you have more lines, perhaps 4 or 5+, you can consider using
python:
instead.
I used both here for only 2 lines just to show the difference.
Alternatively, the
for
loop could have been written as a single line as:
$ for i in range(10): renpy.say("Nobody", "Person #" + str(i) + ", is " + mt_list[i] + " today.")
... or the whole lot could have been a single
python:
block.
Python lists start at #0, which is common to most programming languages these days. So the first entry in this code would be
lt_list[0]
and the last one would be
lt_list[9]
. Still 10 entries, just counting from 0 to 9 instead of 1 to 10.
renpy.say
is just a way of printing stuff for testing, as if a character called "Nobody" is speaking dialogue. It's a bad way of doing it in a real game, because commands run by python are ignored by RenPy's rollback system. I'm imagining the list of names being part a screen somewhere, which is much better supported by RenPy.
I will add that doing stuff directly in python and processing lists and stuff like that is fairly "intermediate" level RenPy knowledge. It feels like you might be trying to bite off more than you can chew at this early stage. Which shouldn't stop you trying. I merely offer caution.