Appending list items and displaying them live on a screen

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
User avatar
namastaii
Eileen-Class Veteran
Posts: 1350
Joined: Mon Feb 02, 2015 8:35 pm
Projects: Template Maker for Ren'Py, What Life
Github: lunalucid
Skype: Discord: lunalucid#1991
Soundcloud: LunaLucidMusic
itch: lunalucid
Location: USA
Contact:

Appending list items and displaying them live on a screen

#1 Post by namastaii »

How am I able to show a list of items (which already works) but also updates as I append items to the list?

Because what I have currently doesn't do so.

Example:


Just the variable for displaying the input and the list to store the user's input

Code: Select all

init python:
    input_active = False
    my_list = []
    newitem = ""
Functions to store input

Code: Select all

def input_item(newstring):
        store.newitem = newstring
def add_item(x):
            if x not in my_list:
                my_list.append(x)
        
Screen I shall add list items

Code: Select all

screen example():
    button:
        if input_active:
            input default "enter new list item here" changed input_item
            action [add_item(newitem), SetVariable("input_active", False)]
        else:
            text "click here"
            action SetVariable("input_active", True)
While the user is typing something, it stores all content into 'newitem'
Once the user clicks on the button again or presses enter, 'newitem' is now passed into my_list
This works fine but displaying the list on the screen will only show the items in the list that were rendered at init. Is my problem just where I've defined the list? Because I have text that shows updated list items but the bit of code I have on the screen doesn't.

This shows updates (kinda..):

Code: Select all

    hbox:
        text "My list:"
            text "[my_list]"
This doesn't update at all:

Code: Select all

vbox:
    for i in range(len(my_list)):
        text my_list[i]
I've tried defining a new variable to use as a "counter" per se but that just returns an error when I try to append the list "list index out of range" - because the counter is going up but the list isn't updating with it within the screen?

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

Re: Appending list items and displaying them live on a screen

#2 Post by Alex »

Code in an init block run on every game run, so your vars willl be reset. If you need to properly store them use 'default' statement - https://www.renpy.org/doc/html/python.h ... -statement

If you want to use custom function in a screen use Function action, like

Code: Select all

action [Function(add_item, newitem), SetVariable("input_active", False)]
https://www.renpy.org/doc/html/screen_a ... l#Function

To show items of a list try

Code: Select all

vbox:
    for i in my_list:
        text i
so you won't need counters, etc.

User avatar
namastaii
Eileen-Class Veteran
Posts: 1350
Joined: Mon Feb 02, 2015 8:35 pm
Projects: Template Maker for Ren'Py, What Life
Github: lunalucid
Skype: Discord: lunalucid#1991
Soundcloud: LunaLucidMusic
itch: lunalucid
Location: USA
Contact:

Re: Appending list items and displaying them live on a screen

#3 Post by namastaii »

I do use Function() sorry some of this didn't translate when I was trying to form a simple example of my code haha

yeah I'll try it outside python then

User avatar
namastaii
Eileen-Class Veteran
Posts: 1350
Joined: Mon Feb 02, 2015 8:35 pm
Projects: Template Maker for Ren'Py, What Life
Github: lunalucid
Skype: Discord: lunalucid#1991
Soundcloud: LunaLucidMusic
itch: lunalucid
Location: USA
Contact:

Re: Appending list items and displaying them live on a screen

#4 Post by namastaii »

Yeah it's still the same exact outcome hmm

User avatar
namastaii
Eileen-Class Veteran
Posts: 1350
Joined: Mon Feb 02, 2015 8:35 pm
Projects: Template Maker for Ren'Py, What Life
Github: lunalucid
Skype: Discord: lunalucid#1991
Soundcloud: LunaLucidMusic
itch: lunalucid
Location: USA
Contact:

Re: Appending list items and displaying them live on a screen

#5 Post by namastaii »

Basically what happens, is I input a new item. it doesn't update the list. I input a second item and press enter, it updates the list with the item I put in before that. it's delayed by one? so I'm looking at my code to see if it's my fault haha

also it's still not updating on the for i in list. just the text tag I have

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

Re: Appending list items and displaying them live on a screen

#6 Post by Alex »

Well, you see - it actually updates. But changes doesn't show up untill screen is refreshed/reshown (it happens when you click on button next time, or if you click to progress through the game).

So try to force screen refreshening - add renpy.restart_interaction() function to your 'input_item' function, like

Code: Select all

    def input_item(newstring):
        store.newitem = newstring
        renpy.restart_interaction()
https://www.renpy.org/doc/html/other.ht ... nteraction

User avatar
namastaii
Eileen-Class Veteran
Posts: 1350
Joined: Mon Feb 02, 2015 8:35 pm
Projects: Template Maker for Ren'Py, What Life
Github: lunalucid
Skype: Discord: lunalucid#1991
Soundcloud: LunaLucidMusic
itch: lunalucid
Location: USA
Contact:

Re: Appending list items and displaying them live on a screen

#7 Post by namastaii »

Why does everything else update fine? I have solid color squares and things that update automatically with inputs without refreshing the screen. My entire "game" is a screen, it's not really a game that uses a dialogue or buttons to open new screens and how hard would that be on the game to constantly restart the screen like that


update:
restart interaction doesn't change anything...

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

Re: Appending list items and displaying them live on a screen

#8 Post by Alex »

That's how it works. The variable do changes, but you need to refresh screen to see it.

This works for me (info updatse right when you click on button first time)

Code: Select all

default my_list = []
default newitem = ""
default input_active = False

init python:
    def input_item(newstring):
        store.newitem = newstring
        renpy.restart_interaction()
        
    def add_item(x):
        global my_list
        if x not in my_list:
            my_list.append(x)


screen example():
    on "show" action SetVariable("input_active", False)
    button:
        align(0.05, 0.05)
        if input_active:
            input default "enter new list item here" changed input_item
            action [Function(add_item, newitem), SetVariable("input_active", False)]
        else:
            text "click here"
            action SetVariable("input_active", True)
            

# The game starts here.
label start:
    "..."
    show screen example
    "?!"
P.S. the bar works 'cause 'VariableValue' has renpy.restart_interaction() function - check renpy-xxx-sdk\renpy\common\00barvalues.rpy if you wish.

User avatar
namastaii
Eileen-Class Veteran
Posts: 1350
Joined: Mon Feb 02, 2015 8:35 pm
Projects: Template Maker for Ren'Py, What Life
Github: lunalucid
Skype: Discord: lunalucid#1991
Soundcloud: LunaLucidMusic
itch: lunalucid
Location: USA
Contact:

Re: Appending list items and displaying them live on a screen

#9 Post by namastaii »

I can't put renpy.restart_interaction() under input_item() because this updates literally as you type so if you were trying to type hello, the list would look like ["h", "he", "hel","hell", "hello" ] but I did try it that way and it still wasn't working for the list under "for i"
there isn't a difference between your example and mine :/ haha

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Appending list items and displaying them live on a screen

#10 Post by trooper6 »

This code works for me. Note: I don't like that I can't highlight and delete, but ah well.

Code: Select all

screen example1():
    vbox:
        for i in my_list:
            text "[i]"
        button:
            if input_active:
                input default "enter new list item here" changed input_item
                action [Function(add_item, newitem), SetVariable("input_active", False)]
            else:
                text "click here"
                action SetVariable("input_active", True)

default input_active = False
default my_list = []
default newitem = ""

init python:
    def input_item(newstring):
        global newitem
        newitem = newstring
        renpy.restart_interaction()

    def add_item(x):
        global my_list
        if x not in my_list:
            my_list.append(x)


# The game starts here.
label start():
    show screen example1()
    "Let's test this thing."
    "1"
    "2"
    "3"

    return
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

User avatar
namastaii
Eileen-Class Veteran
Posts: 1350
Joined: Mon Feb 02, 2015 8:35 pm
Projects: Template Maker for Ren'Py, What Life
Github: lunalucid
Skype: Discord: lunalucid#1991
Soundcloud: LunaLucidMusic
itch: lunalucid
Location: USA
Contact:

Re: Appending list items and displaying them live on a screen

#11 Post by namastaii »

That works better for me thanks (to both of you) I'm giving myself a headache today haha

User avatar
namastaii
Eileen-Class Veteran
Posts: 1350
Joined: Mon Feb 02, 2015 8:35 pm
Projects: Template Maker for Ren'Py, What Life
Github: lunalucid
Skype: Discord: lunalucid#1991
Soundcloud: LunaLucidMusic
itch: lunalucid
Location: USA
Contact:

Re: Appending list items and displaying them live on a screen

#12 Post by namastaii »

How come a text list will work but I can't have an updated imagebutton list? oh my goodness I quit hahaha

edit: I think I had a typo on my end. I think it's okay now

Post Reply

Who is online

Users browsing this forum: Bing [Bot]