How to return the index of an array in a for loop?

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
henvu50
Veteran
Posts: 337
Joined: Wed Aug 22, 2018 1:22 am
Contact:

How to return the index of an array in a for loop?

#1 Post by henvu50 »

Code: Select all

for item in arrayTest:
  # what index array number are we on ?
  text arrayTest.index(item) + "\n" 
How do I get the index number the loop is on? When it loops, I should have access to a number that represents what count the loop is on, how do I access the loop count number?

rames44
Veteran
Posts: 233
Joined: Sun May 29, 2016 4:38 pm
Contact:

Re: How to return the index of an array in a for loop?

#2 Post by rames44 »

Well, one way is

Code: Select all

i = 0
for item in arrayTest:
    (Use i as the index)
    i += 1

User avatar
MaydohMaydoh
Regular
Posts: 165
Joined: Mon Jul 09, 2018 5:49 am
Projects: Fuwa Fuwa Panic
Tumblr: maydohmaydoh
Location: The Satellite of Love
Contact:

Re: How to return the index of an array in a for code]oop?

#3 Post by MaydohMaydoh »

What you want is range and len

Code: Select all

for i in range(len(arrayTest)):
    text arrayTest[i]

henvu50
Veteran
Posts: 337
Joined: Wed Aug 22, 2018 1:22 am
Contact:

Re: How to return the index of an array in a for loop?

#4 Post by henvu50 »

Thanks, I changed my code accordingly.

Is it safe to use $ in a vbox for a loop? Doesn't $ make the variable global? If I use $ index somewhere else couldn't it conflict?

Code: Select all

    vbox:
        $ index = 10
        for item in reversed(arrayTest):
            text "[index]" 
            $ index -= 1 

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: How to return the index of an array in a for loop?

#5 Post by Remix »

or...

Code: Select all

for idx, item in enumerate( arrayTest ):
    text "[idx] : [item]"
Frameworks & Scriptlets:

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: How to return the index of an array in a for loop?

#6 Post by trooper6 »

$ doesn’t make a variable global. $ is used to indicate the line is a line of python code and not RenPy code.
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

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot]