So far, I'm not really seeing why you would use
call
. Most games can be written using
jump
only - especially if you're already struggling with
call
. While
jump
doesn't have some of the flexibility of
call
, it's also simpler to implement for a new developer.
Anyway, let's talk you through it...
call
will jump to the named
label
and execute code until it encounters a
return
statement.
When it encounters a
return
, it will jump back to the next line directly below the
call
statement (the "next line" may be much further down the code, depending on indentation/code blocks).
Another way to explain this is that each time
call
is used, the calling line identifier is added to the end of "call stack". Each time
return
is used, the last entry on the call stack is removed and used to be the place where the game goes next.
It's also why games that don't use return
properly end up doing things the developer didn't expect.
Other than that, RenPy just processes each line in order - moving down one line at a time, from code block to matching code block (matching indentation or back to the previous level of indentation).
Walking through your example...
Python:
label start:
scene black with fade
menu:
"Kitchen":
call kitchenempty
"Livingroom":
call livingroomempty
label kitchenempty:
scene myscene1
"Nothing here."
return
label livingroonempty:
scene myscene2
"nothing here"
return
The game starts at
label start:
There's a
scene
and a
menu:
and the player gets to pick either "Kitchen" or "Livingroom".
If the player picks "Livingroom", the game calls
label kitchenempty:
and does the code within that block until it encounters
return
- at which point it returns back to the next executable line after the
call
.
Since there isn't another line at the same indentation level, the game passes control to the next line after the
menu:
- which happens to be the same
label kitchenempty:
and that code will be repeated until it hits the
return
again. With no entries left on the call stack, the game will return to the game's main menu.
The same thing will happen if the player picks "Livingroom", except it will execute "livingroomempty" and then "kitchenempty" before returning to the main menu.
If you want things to return to the menu, you need to tell your code do that.
One way would be...
Python:
label start:
scene black with fade
label choose_room:
menu:
"Kitchen":
call kitchenempty
"Livingroom":
call livingroomempty
jump choose_room
label kitchenempty:
# etc, etc.
Edit: I've chosen to use a separate
label:
, whereas
anne O'nymous has used the
menu:
itself as a label. Both are perfectly fine. I tend to use a separate
label
only because I find it simpler to explain. It also means that if I see
jump go_to_this_line
, I can search the code for
label go_to_this_line
without worrying if it's a
label:
or a
menu:
.
I've also chosen to put the
jump choose_room
on the next line below the
menu:
. Picking where to put it will depend on your game's needs. I've assume that the game will always return to the room choice menu. Anne's example would allow for each menu choice to behave differently if needed.
Though personally, I think this is easier when you're just starting out...
Python:
label choose_room:
menu:
"Kitchen":
jump kitchenempty
"Livingroom":
jump livingroomempty
label kitchenempty:
scene myscene1
"Nothing here."
jump choose_room
label livingroonempty:
scene myscene2
"nothing here"
jump choose_room
No
call
statements in sight.
Edit2: Oh, and a note of caution: When you build the game for distribution - the build process will normally add...
from {identifier}
...to each
call
statement. If you do end up using
call
... NEVER... EVER... edit or delete those
from
parts of the line.
Because RenPy allows for code to be added/updated/deleted from one version of the game to the next, those
from
additions are an extra level of protection that keeps RenPy straight about where to return back to when a player loads a saved game with lots of entries on the call stack. Don't feel the need to "tidy them up" or "renumber them because they're out of sequence" or anything like that. Just leave them the hell alone.