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
Regular
Posts: 92
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 » Thu Sep 06, 2018 11:58 am

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
Regular
Posts: 187
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 » Thu Sep 06, 2018 12:06 pm

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: 101
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 » Thu Sep 06, 2018 12:11 pm

What you want is range and len

Code: Select all

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

henvu50
Regular
Posts: 92
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 » Thu Sep 06, 2018 12:19 pm

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: 1349
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 » Thu Sep 06, 2018 4:12 pm

or...

Code: Select all

for idx, item in enumerate( arrayTest ):
    text "[idx] : [item]"
Mad Scientist Todo List:
  • Finish Improved Event Handler
  • Implement DragonBones animation as a Creator Defined Container
  • Develop Cartoon Speech Bubble dialogue
  • Finish Bitmask collision and rebound vector system
  • Develop time based building mechanic
  • Others
  • *Find a superb artist and actually write a game*

User avatar
trooper6
Lemma-Class Veteran
Posts: 3645
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 » Thu Sep 06, 2018 5:17 pm

$ 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: Google [Bot]