Page 1 of 1

Adding choices selected to History without showing to user

Posted: Thu Sep 15, 2016 11:13 am
by wyverngem
In the new History is there a way to show what choice the player made in the history without showing it to them on screen?
I want to be able to change the color of the choice selected font in the history screen.

Re: Adding choices selected to History without showing to us

Posted: Mon Sep 19, 2016 4:37 pm
by wyverngem
So I was reading the documentation and came across this: https://www.renpy.org/doc/html/history. ... storyEntry Still haven't figured how to add the information manually though. I know it has something to do with these:

class HistoryEntry

_history_list = [ ]

I'm all right with manually adding the choices.

Re: Adding choices selected to History without showing to us

Posted: Wed Sep 21, 2016 9:38 pm
by philat
Well, you can just manually add a history entry, I suppose. The old readback code (https://www.renpy.org/wiki/renpy/doc/co ... xt_History ) also hijacked the menu handler to record choices automatically, but I don't think the new history does that.

Simple (if somewhat laborious solution): Look through character.py to to see how a new HistoryEntry is generated by regular dialogue. Adapt as necessary for your choices and invoke that manually in your script.

More complex solution: Hijack the menu handler a la old history code.

Wait and see solution: Bug PyTom about incorporating this in the new history out of the box. ;)

Re: Adding choices selected to History without showing to us

Posted: Thu Sep 22, 2016 11:39 am
by wyverngem
I can append the _history_list variable, but I don't know how to do it properly to add the new entry in the History Screen. If I create a variable called "userchoice" I don't know what to set it to. When I try HistoryEntry() from the documentation it tells me HistoryEntry is not defined.

Code: Select all

define e = Character('Eileen')

label start:
    "Hello, world." #History enters this as usual.
    
    "[_history_list]" #Displays [<History None u'Hello, world.'>]
    menu:
        "1 line added.":
            $ userchoice = HistoryEntry(kind=adv, who=None, what="1 line added.") #Breaks here. Doesn't work. HistoryEntry not defined.
            $ _history_list.append(userchoice)
            "I write here."
        "2 line added.":
            $ userchoice = "2 line added."
            $ _history_list.append(userchoice) #Appends variable, but not correctly. "who" not defined and "what" not defined when you open history screen. 
            "I read here."
    "[_history_list]"
    
    "game here."

    return

Re: Adding choices selected to History without showing to us

Posted: Fri Sep 23, 2016 2:16 am
by philat
Never really bothered to go too deep under the hood of renpy, but I imagine the issue is that renpy script doesn't have access to character.py directly and therefore doesn't see the HistoryEntry definition. The definition is super bare bones anyway, though, so just throw that into a init python somewhere in your script.

Re: Adding choices selected to History without showing to us

Posted: Wed Sep 28, 2016 9:05 pm
by wyverngem
I was able to dig into the code a little more and figured out how to add history lines. I will not post it here since I do not know how to troubleshoot it if it breaks the game.

Re: Adding choices selected to History without showing to us

Posted: Thu Apr 26, 2018 6:58 am
by kalesco
wyverngem wrote: Wed Sep 28, 2016 9:05 pm I was able to dig into the code a little more and figured out how to add history lines. I will not post it here since I do not know how to troubleshoot it if it breaks the game.
Would you please care to share your code anyway? I'm trying to add choices to the history but had no luck until now. I use the "new" history system. The old readback code had the choices but I can't figure out how to use this properly :-(

Re: Adding choices selected to History without showing to user

Posted: Sat Jun 29, 2019 2:15 am
by SleepKirby
Old thread, but since there was no code shared for adding history entries for selected choices, here's mine:

Code: Select all

init python:

    def menu(items, **add_input):
        """Overwrites the default menu handler, thus allowing us to log the
        choice made by the player.
        The default menu handler is set to renpy.display_menu(), as seen in
        renpy/defaultstore.py.
        Implementation of this is based on delta's readback module."""
        rv = renpy.display_menu(items, **add_input)
        for item_text, choice_obj in items:
            if rv == choice_obj.value:
                log_menu_choice(item_text)
        return rv

    def log_menu_choice(item_text):
        """Log a choice-menu choice, which is passed in as item_text.
        Implementation based on add_history() in renpy/character.py."""
        h = renpy.character.HistoryEntry()
        h.who = ""
        h.what = ">> " + item_text
        h.what_args = []

        if renpy.game.context().rollback:
            h.rollback_identifier = renpy.game.log.current.identifier
        else:
            h.rollback_identifier = None

        _history_list.append(h)

        while len(_history_list) > renpy.config.history_length:
            _history_list.pop(0)
I hijacked the menu handler like delta's old readback module, since I couldn't think of a better way to do it. It would be cleaner if Ren'Py provided an 'after menu choice' callback where we could put the logic, but there's no such callback that I can find.

log_menu_choice() is the builtin-Ren'Py-history equivalent of delta's store_say(). At least it works as far as I can tell.

I don't know why, but using delta's menu() verbatim (other than replacing the store_say() call with log_menu_choice()) failed to log anything when I tried it (could swear it worked for me before, same Ren'Py version). I had to add '.value' in the third-to-last line for it to work. I went and simplified the function while I was at it, since I couldn't think of why the rest was needed.

Tested with Ren'Py 7.2.2.491.

Re: Adding choices selected to History without showing to user

Posted: Sun Oct 06, 2019 7:47 am
by RicharDann
Just wanted to say thank you so much to SleepKirby for sharing your code, I was having a hard time figuring out how to create HistoryEntry objects and update _history_list, the Docs are still not very clear about this.

This code gave me the clue I needed and thus saved me a great deal of headache.

Thanks a bunch!

Re: Adding choices selected to History without showing to user

Posted: Sun Oct 06, 2019 5:51 pm
by SleepKirby
Glad to hear it helped!

Re: Adding choices selected to History without showing to user

Posted: Mon Jul 27, 2020 3:32 pm
by Ayael
Old thread again, but this was super useful !!! Still, I was a bit uncomfortable to the idea of overwriting a renpy function, so I used your very useful code to do it a bit differently :

In choice screen I replaced by :

Code: Select all

for i, (caption, action, chosen) in enumerate(items):
        
                button:
                    
                    text caption 
                    action [Function(log_menu_choice, caption), action]
and then I used your function, with just a little if to avoid some menu prediction being displayed :

Code: Select all

def log_menu_choice(item_text):
        if item_text != "Menu Prediction":
            """Log a choice-menu choice, which is passed in as item_text.
            Implementation based on add_history() in renpy/character.py."""
            h = renpy.character.HistoryEntry()
            h.who = ""
            h.what = ">> " + item_text
            h.what_args = []

            if renpy.game.context().rollback:
                h.rollback_identifier = renpy.game.log.current.identifier
            else:
                h.rollback_identifier = None

            _history_list.append(h)

            while len(_history_list) > renpy.config.history_length:
                _history_list.pop(0)

Re: Adding choices selected to History without showing to user

Posted: Mon Jul 27, 2020 7:18 pm
by SleepKirby
That looks like a nice way of doing it! Yeah, I think customizing a screen seems cleaner than overwriting the menu handler. Thanks for sharing.

Re: Adding choices selected to History without showing to user

Posted: Mon Jul 27, 2020 8:12 pm
by Imperf3kt
Ren'Py actually has a python function for doing this, though manual.

Code: Select all

$ narrator.add_history(kind="adv", who=narrator.name, what="Text to appear in history.")
Add this after each choice, with the name of the choice or modified text.

Re: Adding choices selected to History without showing to user

Posted: Wed Nov 10, 2021 1:20 pm
by Azephir
Yeah, you can even add it to the screen choice, to not have to script it manually after each option.

Just remplace in the screens.rpy that :

Code: Select all

screen choice(items):
     style_prefix "choice"

     vbox:
         for i in items:
             textbutton i.caption action i.action


by that :

Code: Select all

screen choice(items):
     style_prefix "choice"

     vbox:
         for i in items:
             textbutton i.caption action [i.action, Function(narrator.add_history, kind="adv",who=__("Choice:"),what=__(i.caption))]
Hope it'll help you like it helped me.

Cheers

Edit : I know it's kind of necrobump, but it takes me time to found this (with help!), and it could save time to others, so i put it here.