Shifting python list and showing on 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
TellerFarsight
Veteran
Posts: 230
Joined: Sun Jun 04, 2017 8:09 pm
Projects: Vora, Secrets Untold
Location: Toronto, ON
Contact:

Shifting python list and showing on screen

#1 Post by TellerFarsight »

Code: Select all

default unit_list = ["A", "B", "C"]

screen show_the_list:
    vbox:
        text "First, there's" + unit_list[0]
        text "Then, there's" + unit_list[1]
        text "And finally, there's" + unit_list[2]
I'm actually doing it with images, but I think it should work the same. This part works fine, but what I want to do is manipulate unit_list and then show it on the screen.
I can put a line:

Code: Select all

$ unit_list = ["B", "C", "A"]
and it shifts everything, but I want to make this somewhat automatic, with something like

Code: Select all

$ unit_list.insert(0, unit_list.pop)
maybe that's the problem, but if it's this or something else, trying

Code: Select all

$ unit_list.insert(0, unit_list.pop)
pause
show screen show_the_list
returns the error
"coercing to Unicode: need string or buffer, instancemethod found
Current Project: Vora
Also Check Out: Devil Survivor [Reverse-Engineered]

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

Re: Shifting python list and showing on screen

#2 Post by philat »

Some variation on modulo should work. Not particularly refined here, but you get the basic idea.

Code: Select all

default unit_list = ["A", "B", "C"]

screen show_the_list(turn):
    vbox:
        text "First, there's" + unit_list[turn % 3]
        text "Then, there's" + unit_list[(turn+1) % 3]
        text "And finally, there's" + unit_list[(turn+2) % 3]
        
label start:
    show screen show_the_list(1)
    pause
    show screen show_the_list(2)
    pause

User avatar
TellerFarsight
Veteran
Posts: 230
Joined: Sun Jun 04, 2017 8:09 pm
Projects: Vora, Secrets Untold
Location: Toronto, ON
Contact:

Re: Shifting python list and showing on screen[SOLVED]

#3 Post by TellerFarsight »

That looks like it would work, but I did figure out another solution.
Whenever I want it to change, I just put

Code: Select all

$ unit_list = unit_list[1:] + unit_list[:1]
If you need to do this kind of thing *without* altering the actual list at all, your solution would be good.
Current Project: Vora
Also Check Out: Devil Survivor [Reverse-Engineered]

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Shifting python list and showing on screen

#4 Post by Remix »

You list.insert, list.pop version should work if you actually call pop ( with the .pop() calling parenthesis )

a.insert( 0, a.pop() )
a = [ a.pop() ] + a
a = a[-1:] + a[:-1]
Frameworks & Scriptlets:

User avatar
TellerFarsight
Veteran
Posts: 230
Joined: Sun Jun 04, 2017 8:09 pm
Projects: Vora, Secrets Untold
Location: Toronto, ON
Contact:

Re: Shifting python list and showing on screen

#5 Post by TellerFarsight »

Right. I was told initially that if I list.pop without specifying an index, it would just take the last one, but because of that I forgot to put parentheses at all.
Current Project: Vora
Also Check Out: Devil Survivor [Reverse-Engineered]

Post Reply

Who is online

Users browsing this forum: apocolocyntose, Bing [Bot]