Passing a label name to another label as a parameter? [solved]

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
trailsiderice
Regular
Posts: 69
Joined: Fri Sep 29, 2023 4:02 pm
Contact:

Passing a label name to another label as a parameter? [solved]

#1 Post by trailsiderice »

So, at the start of each chapter in my game, I want to call a label that does a few things like stop the music and show a chapter title card, and to avoid having to copy and paste this same label multiple different times, I'd like to be able to call a single label and just swap out certain things like the chapter title and such as well as the label it jumps to when it's done. Problem is, I can't figure out how to pass the name of the next label as a parameter.

here's what I've got:

Code: Select all

label chapterintro(chapter,startlabel):
    stop music 
    show screen chapter_titlecard(chapter) with fade
    jump startlabel
I tried calling the label like this:

Code: Select all

call chapterintro("CHAPTER 01",chapter01_intro)
but I got an error telling me that chapter01_intro isn't defined.
so I tried calling the label like this:

Code: Select all

call chapterintro("CHAPTER 01","chapter01_intro")
but then I got an error telling me it couldn't find the label "startlabel".
I also tried doing both after changing the "jump" in chapterintro to "call", but no dice there either.

is this possible? how do you do this?
if it's not possible, then what's even the point of being able to pass parameters to a label?
Last edited by trailsiderice on Thu Nov 09, 2023 8:25 pm, edited 1 time in total.

trailsiderice
Regular
Posts: 69
Joined: Fri Sep 29, 2023 4:02 pm
Contact:

Re: Passing a label name to another label as a parameter?

#2 Post by trailsiderice »

ok well i figured it out, sort of, but i'm having problems with it that I can't figure out how to fix.

i did it like this instead:

Code: Select all

$ renpy.call(startlabel)
but now i have a new problem.

I start chapter one with call chapterintro("CHAPTER 01","chapter01_intro")
then at the start of the next chapter, i call it again for chapter 2, and then again for chapter 3, and so on.
The problem arises when I reach the end of the chapters, and there's no more left. Expected behavior would be that the game ends and exits to main menu. But that's not what happens. Instead, it calls the chapter intro again... for each of the previous chapters in reverse order, essentially playing the whole game again backwards chapter by chapter.

How do I stop it from doing this?

trailsiderice
Regular
Posts: 69
Joined: Fri Sep 29, 2023 4:02 pm
Contact:

Re: Passing a label name to another label as a parameter?

#3 Post by trailsiderice »

i feel like the potential solution might be somewhere in this section of the documentation:

Code: Select all

If the optional from clause is present, it has the effect of including a label statement with the given name as the statement immediately following the call statement. An explicit label helps to ensure that saved games with return stacks can return to the proper place when loaded on a changed script.
but i'm having trouble parsing this. i have no idea what this is supposed to mean, it's word salad to me, and there aren't any examples in the documentation. (this is a frequent problem i have with the documentation.. it can be quite oddly worded sometimes and provides very few clarifying examples)

can someone translate?

trailsiderice
Regular
Posts: 69
Joined: Fri Sep 29, 2023 4:02 pm
Contact:

Re: Passing a label name to another label as a parameter?

#4 Post by trailsiderice »

OK, i think i've finally figured it out, for real this time.

that from statement section of the documentation still reads like word salad to me, but i don't think it's actually relevant in this case.
I was able to solve it by simply putting the statement "return" after the call chapterintro("CHAPTER 01","chapter01_intro") statement. I think the problem is that it was putting the entire first label of the chapter into the call stack, since there was no statement between the call and the first label in the chapter.

I would however still like an explanation of the from clause bit... that warning in the documentation about saves being broken or something if there's no from clause has me a little worried, i'd prefer not to have this setup break people's saves

Another_Tom
Regular
Posts: 61
Joined: Mon Apr 24, 2023 9:06 am
Completed: Harold And The Witches
Projects: Steven On The Run
itch: dantom
Location: Germany
Contact:

Re: Passing a label name to another label as a parameter?

#5 Post by Another_Tom »

trailsiderice wrote: Wed Nov 08, 2023 2:07 am here's what I've got:

Code: Select all

label chapterintro(chapter,startlabel):
    stop music 
    show screen chapter_titlecard(chapter) with fade
    jump startlabel
Would you like to use jump or call? "expression" should work in both cases:

Code: Select all

label chapterintro(chapter,startlabel):
    stop music 
    show screen chapter_titlecard(chapter) with fade
    jump expression startlabel

Code: Select all

label chapterintro(chapter,startlabel):
    stop music 
    show screen chapter_titlecard(chapter) with fade
    call expresson startlabel
    ## After a return from the startlabel, the code is continued at this point.
    # so do a jump here or whatever you want to do.

User avatar
Alex
Lemma-Class Veteran
Posts: 3095
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Passing a label name to another label as a parameter?

#6 Post by Alex »

trailsiderice wrote: Wed Nov 08, 2023 2:07 am So, at the start of each chapter in my game, I want to call a label that does a few things like stop the music and show a chapter title card, and to avoid having to copy and paste this same label multiple different times, I'd like to be able to call a single label and just swap out certain things like the chapter title and such as well as the label it jumps to when it's done. ...
The idea of call statement is to be able to run the same label several times from different places and easily return back. So, try to redesign your code a bit.
Make a label that you'll need and end this label with return statement.
Then, call this label wherever you need it - game will be returning to the line right after call, so you can put jump there or just continue the game flow.

Code: Select all

label some_label_to_call:
    "do stuff"
    return

label start:
    "..."
    call some_label_to_call
    "... ..."
    call some_label_to_call
    jump the_other_label
    "we won't see this line..."

label the_other_label:
    "the end"

trailsiderice
Regular
Posts: 69
Joined: Fri Sep 29, 2023 4:02 pm
Contact:

Re: Passing a label name to another label as a parameter?

#7 Post by trailsiderice »

Another_Tom wrote: Wed Nov 08, 2023 11:08 am
trailsiderice wrote: Wed Nov 08, 2023 2:07 am here's what I've got:

Code: Select all

label chapterintro(chapter,startlabel):
    stop music 
    show screen chapter_titlecard(chapter) with fade
    jump startlabel
Would you like to use jump or call? "expression" should work in both cases:

Code: Select all

label chapterintro(chapter,startlabel):
    stop music 
    show screen chapter_titlecard(chapter) with fade
    jump expression startlabel

Code: Select all

label chapterintro(chapter,startlabel):
    stop music 
    show screen chapter_titlecard(chapter) with fade
    call expresson startlabel
    ## After a return from the startlabel, the code is continued at this point.
    # so do a jump here or whatever you want to do.
I was mostly trying to use whichever one would actually work the way I wanted it to, I was trying both of them.
I didn't know about using expression though, that's good to know, thanks!

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Sirifys-Al, Zapor