Customizing/Changing Rollback function in Renpy

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
h333
Newbie
Posts: 2
Joined: Wed Jun 09, 2021 5:58 pm
Contact:

Customizing/Changing Rollback function in Renpy

#1 Post by h333 »

I am having trouble figuring out how to change and manipulate the Rollback function. When I launch the game, and press start I arrive at the first page. Then when I click on the screen again and go to the second page. However, when I press the back option at the bottom of the page I return to the main menu. I wanted to know how to make this so that when I click the back option at the bottom I will be taken to the previous page. For example, say I have advanced to page 3 of my VN and would like to go back to page 2, and then from page 2 I would want to go back to page 1. Similar to how one would read a real book. The following code is how I am defining a "page". For each time in the while loop I use renpy.show() to show the image and I use narrator() to show the text. I then update the loop so that it may advance to the next "page". Any help would be greatly appreciated. Thanks.



default story = "mystory.txt"
default pg_image_number = "1"
image pg_image = "images/[story]_pg[pg_image_number].png"

label start:


python:
fname = "mystory.txt"
story_pages = read_file(fname)
length_of_story = len(story_pages)
pg_text_number = 1


while int(pg_image_number) <= length_of_story:

renpy.show ('pg_image', at_list=[Transform(child = 'pg_image', function = None, xsize = xsize_image , ysize = ysize_image,
fit = fit_style), Position(xpos = 0.5, ypos = 0.0, anchor = (0.0,0.0))], what=None, zorder=0, tag=None, behind=[])

narrator(story_pages[pg_text_number-1])

pg_text_number += 1
number = int(pg_image_number) + 1
pg_image_number = str(number)

philat
Eileen-Class Veteran
Posts: 1909
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Customizing/Changing Rollback function in Renpy

#2 Post by philat »

Rollback is based on statements (i.e., it will rollback to the preceding statement) but IIRC everything in a python block is treated as one statement. Possibly also things happening in a while loop, although I haven't tested that and can't be assed to do so right now. So, despite it being slightly messier code, if you use a separate statement for the narrator (and, if necessary, change the while loop to an if/jump combo) I think the rollback should work. Untested though.

Code: Select all

label start:

    python:
    fname = "mystory.txt"
    story_pages = read_file(fname)
    length_of_story = len(story_pages)
    pg_text_number = 1


label whileloop:
    if int(pg_image_number) <= length_of_story:

        $ renpy.show ('pg_image', at_list=[Transform(child = 'pg_image', function = None, xsize = xsize_image , ysize = ysize_image,
        fit = fit_style), Position(xpos = 0.5, ypos = 0.0, anchor = (0.0,0.0))], what=None, zorder=0, tag=None, behind=[])

        $ narrator(story_pages[pg_text_number-1])

        python: 
            pg_text_number += 1
            number = int(pg_image_number) + 1
            pg_image_number = str(number)
        jump whileloop


User avatar
zmook
Veteran
Posts: 421
Joined: Wed Aug 26, 2020 6:44 pm
Contact:

Re: Customizing/Changing Rollback function in Renpy

#3 Post by zmook »

I have written a comic viewer and managed pretty much this exact problem. Thing is, if you have explicit "pages" as a concept, rollback is just not how you want to manage "go to previous". It would work fine if the only way to read your novel was by starting at page 1, but sooner or later you're going to want to implement a shortcut to let someone jump to the start of Chapter Two. And you want the back button from chapter 2 page 1 to go to the last page of chapter 1, which rollback won't do for you.

Here's what I did:

Code: Select all

default current_page_n = (1,1)      # tuple with 1-based (chapter number, page number)
default regressing = False

init python:
    def get_chapter(pn):
        try:
            ei = eids.index(pn[0])
            rv = pages[ei]
        except:
            return None
        return rv
    
    
    def get_page(pn):
        try:
            rv = get_chapter(pn)[pn[1]-1]
        except:
            return None
        return rv
    
    
    def get_next_page_n():
        """returns the tuple ch/page numbers of the next page, if it exists, 
        else None"""
        global current_page_n, pages
        
        nxp = (current_page_n[0], current_page_n[1]+1)
        if get_page(nxp):
            return nxp
        else:
            try:
                ei = eids.index(nxp[0])
                nxp = (eids[ei+1], 1)          # so try the next chapter
                return nxp
            except:
                return None
    
    
    def get_prev_page_n():
        """returns the tuple ch/page numbers of the previous page, if it exists,
         else None"""
        global current_page_n, pages
        
        if 1 < current_page_n[1]:
            pvp = (current_page_n[0], current_page_n[1]-1)      # decrement the page number
            return pvp if get_page(pvp) else None
        else:
            # go to last page of prev chapter
            try:
                ei = eids.index(current_page_n[0])     # current chapter 0-index
                if ei <= 0:
                    return
                ei -= 1
                pvp = (eids[ei], len(pages[ei]))          # so try the prev chapter
                return pvp
            except:
                return None
    
    
    def advance_page():
        """increments current_page_n, iff the next page exists. The screen auto-updates."""
        global current_page_n
        nxp = get_next_page_n()
        if nxp:
            current_page_n = nxp
        return


    def regress_page():
        '''decrements current_page_n, iff the prev page exists. The screen auto-updates.'''
        global current_page_n, regressing
        pvp = get_prev_page_n()
        if pvp:
            current_page_n = pvp
        regressing = True
        return


label start:
    scene black
    show screen comic_page(_layer="master")
    show screen metadata_overlay()
    show screen controls()
    
    while True:
        if current_page_n[1] == 1:
            $ renpy.choice_for_skipping()   # triggers autosave
        pause
        $ zoom = False
        # this is here so that any Return() action, such as hitting space or enter,
        # will advance the comic.`regressing` is set as a flag when the player has
        # specifically called for moving to the previous page
        if not regressing:
            $ advance_page()  
        $ regressing = False

    return

Code: Select all

screen controls():
    zorder 12
    
    if current_page_n != (1,1):     # not first page
        key 'K_LEFT' action [Function(regress_page), Return()]
        key 'K_UP' action [Function(regress_page), Return()]
        key 'K_BACKSPACE' action [Function(regress_page), Return()]
        textbutton "<":
            style "movearrow"
            align (0.0,0.5)
            action [Function(regress_page), Return()]
    
    if get_next_page_n():   # not last past
        key 'K_RIGHT' action Return()
        key 'K_DOWN' action Return()
        key 'K_SPACE' action Return()
        key 'K_RETURN' action Return()
        textbutton ">":
            style "movearrow"
            align (1.0,0.5)
            action Return()
    
    text "issue [current_page_n[0]], p [current_page_n[1]]":
        align (1.0,1.0)
        offset (-12,-12)
colin r
➔ if you're an artist and need a bit of help coding your game, feel free to send me a PM

Post Reply

Who is online

Users browsing this forum: No registered users