The most common mistake with
jump
and
call
is what Anne described... using
call
in situations where (like now) you decide that you want to exit the code you've "called into" without using
return
. The reality is that those
call
statements should probably have been
jump
statements. Or at the very least, there should have been some sort of "hub"
label
that was always returned to and the decision to jump elsewhere should be made after the script logic has returned from the called block (like Anne's most recent reply).
Or put another way, only ever use
call
when you're 100% sure you'll be using
return
to come back.
But you are where you are... and this is RenPy... so we can afford to throw out some of the commercial programming rules.
If you jump to Chapter4... that's fine
*.
* Okay, it's not fine - but it's not terrible either.
What will happen is that you'll leave a few labels on the call stack, but as long as you never use a
return
beyond this point without a corresponding
call
- it will behave (due to it's "Last on, First off" nature)... up to a point. If you keep repeating this sort of coding, the amount of labels left on the stack will keep increasing. That's bad in purely technical terms (since you're not managing your code correctly), but RenPy won't care. A couple of hundred labels on a stack is perfectly normal in python, as long as things are coded correctly. Relatively speaking, it's a tiny amount of memory.
The "up to a point" is when the program ends.
RenPy scripts tend to end with a final
return
statement (it effectively returns to the main menu).
Except your script will still have labels on the stack and the script will "return" there instead. Suddenly your player will be wondering why at the end of the game, they're back in the middle of some content they finished ages ago (because you called it and didn't return out of it).
Since this is RenPy... there's a way around it. Instead of ending the game with
return
, instead use
$ MainMenu(confirm=false)
, which will effectively go "sod, it... back to the main menu... I don't care what else is happening". It's a kludge, but it's a kludge that will ignore the remaining items on the stack.
Alternatively, you could use Anne's other suggestion of a loop using
You must be registered to see the links
and
You must be registered to see the links
.
You must be registered to see the links
will remove the newest entry on the call stack. One at a time.
You must be registered to see the links
will tell you how many items are currently on the stack.
If you loop around the "pop" until the "stack_depth" is zero, the stack will be empty and then you can jump to Chapter4. Again, a bit of kludge - but it solves the problem you've built for yourself.