How to loop an array when it ends? Here are my failures...

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
abeplaga
Regular
Posts: 35
Joined: Mon Oct 15, 2018 10:09 am
Contact:

How to loop an array when it ends? Here are my failures...

#1 Post by abeplaga »

Hi. So I'm testing how works arrays and I want a botton to show one by one the contents of the array and once it reaches the end it will restart (like A, B, C, A, B, C, A, B, C...).

With the following code I think that I have created it: I have a counter that shows the content one by one and when I get to the last content of the array it becomes 0 and therefore I return to the beginning but I do not get it, I get the error IndexError: list index out of range.

Code: Select all

define myarray = ["A", "B", "C"]
define counter = 0

screen test_array:
    imagebutton idle "button.png" action SetVariable("counter", counter + 1)

    if counter == len(myarray):
        $ counter = 0 

    text myarray[counter]
Looking and looking I do not find anything wrong in the code and I think it is quite logical but for some reason (ignorance maybe) I can not find out :(
  1. With if counter == len(myarray) it loop and shows "A" but next appears IndexError: list index out of range
  2. With if counter >= len(myarray) it loop and shows "A" but then it stagnates and stays in "A" no matter how much I press the button
  3. With if counter <= len(myarray) I press the botton but only appears "A" but on the 4th botton press it appears IndexError: list index out of range :shock:

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3792
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: How to loop an array when it ends? Here are my failures...

#2 Post by Imperf3kt »

I don't know much about this, but you should use default for "counter" variable as it changes. define is for things that won't change.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

User avatar
IrinaLazareva
Veteran
Posts: 399
Joined: Wed Jun 08, 2016 1:49 pm
Projects: Legacy
Organization: SunShI
Location: St.Petersburg, Russia
Contact:

Re: How to loop an array when it ends? Here are my failures...

#3 Post by IrinaLazareva »

Code: Select all

default counter  = 0
define myarray = ['A', 'B', 'C']
screen test_array():
    imagebutton idle 'button.png' action If(counter<len(myarray)-1, SetVariable('counter', counter+1), SetVariable('counter', 0))
    text myarray[counter]
https://www.renpy.org/doc/html/screen_actions.html#If


In your example, the list consists of three elements. And the index for them is measured from zero to two.
I.e. max index is not equal to the number of elements, so such comparison causes the 'list index out of range' exception.
Last edited by IrinaLazareva on Sat Feb 02, 2019 2:55 am, edited 1 time in total.

Human Bolt Diary
Regular
Posts: 111
Joined: Fri Oct 11, 2013 12:46 am
Contact:

Re: How to loop an array when it ends? Here are my failures...

#4 Post by Human Bolt Diary »

What you want is a modulo: https://en.wikipedia.org/wiki/Modulo_operation

Code: Select all

textbutton "Foo" action SetVariable("counter", (counter + 1) % len(myarray))
The remainder can never be greater than the length of your list

ie:

1 % 4 = 1
2 % 4 = 2
3 % 4 = 3
4 % 4 = 0
5 % 4 = 1

Post Reply

Who is online

Users browsing this forum: No registered users