Dynamic Label Function!

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Post Reply
Message
Author
User avatar
CoreBear
Newbie
Posts: 2
Joined: Sat Apr 17, 2021 10:28 am
Github: Core-Bear
Contact:

Dynamic Label Function!

#1 Post by CoreBear »

Alright, I've spent the past few months looking into Ren'Py and trying to learn how things are done- the forums, and especially this here cookbook has been incredibly helpful. SO! Here's something I've been working on for a little bit, just a small function that updates itself and loads the next chapter upon being called.

Code: Select all

init python:
    def nextCh():
        global num
        if num == None:
            num = 0
        lbl = "Ch" + "%d" % num
        num += 1
        if renpy.has_label(lbl) == True:
            renpy.call(lbl)
        else:
            global lbl
            renpy.jump("error")
How it works is simple! First you make num global, so that it can be reused as necessary and doesn't lock up. Then you make sure it actually has a value- since without a value it just breaks and can't be added to. You don't need to, but I store the result in a variable as a string. The "Ch" means chapter, and the next part: "%d" % num; just turns the variable num into a string! That means if num == 0, lbl == "Ch0"! After that, it adds 1 to num so that the next time you call the function, lbl == "Ch1", and then lbl == "Ch2", and so on. The next line just checks to make sure the label you want exists, because you don't want to be calling "Ch10" when you only have 9 Chapters, and then I use renpy.call(lbl) so I can call from a variable! Finally, if the label lbl doesn't exist, it saves the variable globally and then jumps to an error label! The error looks like this:

Code: Select all

label error:
    "It appears there was an error while trying to load the label [lbl]. Please contact CoreBear#9999 at the {a=https://discord.gg/mgF4B5sxez}discord{/a}."
    $ renpy.quit()
And this is how you use it;

Code: Select all

label start:
	$nextCh()
	$nextCh()
	$nextCh()

label Ch0:
	"This is the prologue, if you have one. Otherwise, just the start of your story!"
	
label Ch1:
	"We don't have a Ch2 label, so in this scenario it would call the error label.
Signature's are effort and I'm lazy :shock: Gonna work on this when I have time maybe
- My discord: https://discord.gg/mgF4B5sxez

User avatar
hell_oh_world
Miko-Class Veteran
Posts: 777
Joined: Fri Jul 12, 2019 5:21 am
Contact:

Re: Dynamic Label Function!

#2 Post by hell_oh_world »

I think this one is not very semantic. Somehow if your game gets larger you can easily lose yourself with this since there are no meaningful words to tell what label you're jumping in. Writing tens or more of this $nextCh() function is vague and can be quite repetitive. Of course, you can just use a loop, though that won't justify the importance of this either.
Add that in your example you can simply add a pass statement under the start label and have the same result.
Just my two cents...

User avatar
CoreBear
Newbie
Posts: 2
Joined: Sat Apr 17, 2021 10:28 am
Github: Core-Bear
Contact:

Re: Dynamic Label Function!

#3 Post by CoreBear »

True that, I just found it useful for a small project I was working on. It's simply meant for linear short stories, though I intend on changing it up slightly and having the chapter title be an argument so you can actually have different paths and stuff, but even then I'm well aware this is basically just the call/jump functions done in a linear fashion. Still, as it is here it's viable for purely linear stories with little to no deviations in story pathing; alternatively, if I can write a system to save where you're up to in a story and variables and stuff it could be useful for sort of anthology mini-novels of some sort.
Signature's are effort and I'm lazy :shock: Gonna work on this when I have time maybe
- My discord: https://discord.gg/mgF4B5sxez

togs
Newbie
Posts: 6
Joined: Wed Aug 18, 2021 6:12 pm
itch: togs
Contact:

Re: Dynamic Label Function!

#4 Post by togs »

I'm new to this, but I would have gone for a more descriptive name like "chapter"

Then, just to make it more readable:

lbl = "Ch" + str(chapter)

Post Reply

Who is online

Users browsing this forum: No registered users