Can I display one image several times in a loop? [solved]

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
leon
Miko-Class Veteran
Posts: 554
Joined: Sun Oct 09, 2011 11:15 pm
Completed: Visual Novel Tycoon, Night at the Hospital, Time Labyrinth, The Buried Moon, Left of Center, Super Otome Quest
Projects: Lemon Project, Porcelain Heart, Dream's Dénouement
Organization: Team ANARKY
Contact:

Can I display one image several times in a loop? [solved]

#1 Post by leon » Tue Jan 10, 2012 9:45 am

The code below draws different plants on a 4x4 grid (garden). It's a bit of a mess and probably not the best way to do it, but it's the best I can do for now with my limited knowlege or Ren'Py...

Code: Select all

    #display the plants:
    $ xfield = 1
    $ yfield = 1
    #field1, for each plant
    if 'plant1' in field1:
        if '0' in field1:
            show plantnew zorder 2 as ha1 at Position(ypos=plant_ypos, yanchor='bottom', xpos=plant_xpos+120*(xfield-1), xanchor='left'):
                size (109, 92)
        if '1' in field1:
            show plant1 zorder 2 as ha1 at Position(ypos=plant_ypos, yanchor='bottom', xpos=plant_xpos+120*(xfield-1), xanchor='left'):
                zoom 0.60
        if '2' in field1:
            show plant1 zorder 2 as ha1 at Position(ypos=plant_ypos, yanchor='bottom', xpos=plant_xpos+120*(xfield-1), xanchor='left')
    if 'plant2' in field1:
        if '0' in field1:    
            show plantnew zorder 2 as ha1 at Position(ypos=plant_ypos, yanchor='bottom', xpos=plant_xpos+120*(xfield-1), xanchor='left'):
        if '1' in field1:
            show plant2 zorder 2 as ha1 at Position(ypos=plant_ypos-15, yanchor='bottom', xpos=plant_xpos+120*(xfield-1)+20, xanchor='left'):
                zoom 0.60
        if '2' in field1:
            show plant2 zorder 2 as ha1 at Position(ypos=plant_ypos, yanchor='bottom', xpos=plant_xpos+120*(xfield-1), xanchor='left'):
    if 'plant3' in field1:
        if '0' in field1:    
            show plantnew zorder 2 as ha1 at Position(ypos=plant_ypos, yanchor='bottom', xpos=plant_xpos+120*(xfield-1), xanchor='left'):
        if '1' in field1:
            show plant3_new zorder 2 as ha1 at Position(ypos=plant_ypos, yanchor='bottom', xpos=plant_xpos+120*(xfield-1), xanchor='left')               
        if '2' in field1:
            show plant3_ripe zorder 2 as ha1 at Position(ypos=plant_ypos, yanchor='bottom', xpos=plant_xpos+120*(xfield-1), xanchor='left')
    if 'plant4' in field1:
        if '0' in field1:    
            show plantnew zorder 2 as ha1 at Position(ypos=plant_ypos, yanchor='bottom', xpos=plant_xpos+120*(xfield-1), xanchor='left'):
        if '1' in field1:
            show plant4_new zorder 2 as ha1 at Position(ypos=plant_ypos, yanchor='bottom', xpos=plant_xpos+120*(xfield-1), xanchor='left')               
        if '2' in field1:
            show plant4_ripe zorder 2 as ha1 at Position(ypos=plant_ypos, yanchor='bottom', xpos=plant_xpos+120*(xfield-1), xanchor='left')
            
    #field2, for each plant
    $ xfield = 2
    $ yfield = 1
    if 'plant1' in field2:
        if '0' in field2:
            show plantnew zorder 2 as ha2 at Position(ypos=plant_ypos, yanchor='bottom', xpos=plant_xpos+120*(xfield-1), xanchor='left'):
        if '1' in field2:
            show plant1 zorder 2 as ha2 at Position(ypos=plant_ypos, yanchor='bottom', xpos=plant_xpos+120*(xfield-1), xanchor='left'):
                zoom 0.60
        if '2' in field2:
            show plant1 zorder 2 as ha2 at Position(ypos=plant_ypos, yanchor='bottom', xpos=plant_xpos+120*(xfield-1), xanchor='left'):               
    if 'plant2' in field2:
        if '0' in field2:    
            show plantnew zorder 2 as ha2 at Position(ypos=plant_ypos, yanchor='bottom', xpos=plant_xpos+120*(xfield-1), xanchor='left'):
        if '1' in field2:
            show plant2 zorder 2 as ha2 at Position(ypos=plant_ypos-15, yanchor='bottom', xpos=plant_xpos+120*(xfield-1)+20, xanchor='left'):
                zoom 0.60
        if '2' in field2:
            show plant2 zorder 2 as ha2 at Position(ypos=plant_ypos, yanchor='bottom', xpos=plant_xpos+120*(xfield-1), xanchor='left'):
    if 'plant3' in field2:
        if '0' in field2:    
            show plantnew zorder 2 as ha2 at Position(ypos=plant_ypos, yanchor='bottom', xpos=plant_xpos+120*(xfield-1), xanchor='left'):
        if '1' in field2:
            show plant3_new zorder 2 as ha2 at Position(ypos=plant_ypos, yanchor='bottom', xpos=plant_xpos+120*(xfield-1), xanchor='left')               
        if '2' in field2:
            show plant3_ripe zorder 2 as ha2 at Position(ypos=plant_ypos, yanchor='bottom', xpos=plant_xpos+120*(xfield-1), xanchor='left')
    if 'plant4' in field2:
        if '0' in field2:    
            show plantnew zorder 2 as ha2 at Position(ypos=plant_ypos, yanchor='bottom', xpos=plant_xpos+120*(xfield-1), xanchor='left'):
        if '1' in field2:
            show plant4_new zorder 2 as ha2 at Position(ypos=plant_ypos, yanchor='bottom', xpos=plant_xpos+120*(xfield-1), xanchor='left')               
        if '2' in field2:
            show plant4_ripe zorder 2 as ha2 at Position(ypos=plant_ypos, yanchor='bottom', xpos=plant_xpos+120*(xfield-1), xanchor='left')
This code (example is for 2 fields) would go on for each of the 16 fields and each plant.

1. How could I use the same code in a loop that repeats 16 times?

The only difference in the code parts is the 'field' variable (field1, field2, field3...) and 'as' statement (as ha1, as ha2, ...).

2. Is there a way to use a string as variable name? Something like:
fieldvar = 'field' + fieldnum
if '0' in fieldvar: # replacing if '0' in field1:, if '0' in field2:, ...
Last edited by leon on Wed Jan 11, 2012 4:33 pm, edited 2 times in total.

User avatar
MoPark
Regular
Posts: 98
Joined: Sat Dec 31, 2011 7:05 pm
Projects: Kangaroo, Terminal Love
Location: DC
Contact:

Re: Draw instances of a sprite in loop? Can I use string as

#2 Post by MoPark » Tue Jan 10, 2012 8:53 pm

It sounds like you need to use nested for loops in some fashion. I don't think Renpy has those natively, but Python definitely does. If you wanted to do it purely with Renpy, however, you can play around with labels, changing necessary variables within them. For example:

Code: Select all

$ plant = 1
label field1:
    # code you want to execute goes here
    $ plant += 1
    if plant is 5:
        $ plant = 1
        jump field2
    else:
        jump field1
This is simple to explain. You started with plant being 1. It will run through the code you want, increasing the plant by 1 each time it does so. After four executions, plant should now equal 5, so reset plant to 1, and jump to the next field. It will only jump after four executions of plant.

Since you're using Renpy-specific statements like show, I actually think I'd recommend using Renpy in this manner, as opposed to Python for loops. As a note, any line of code preceded by $ is a Python line of code, which is what we use for variables. I'm sure other people have their own things to say on the manner, and I'm positive my method isn't the best (I didn't think it fully for every single possibility you want, nor is it quite for the type of data you want to execute), but I think it's the right idea to think about, and would certainly be less lines of code than what you have currently.

User avatar
Mild Curry
Regular
Posts: 107
Joined: Fri Jul 02, 2010 3:03 pm
Projects: That's The Way The Cookie Crumbles
Organization: TwinTurtle Games
Contact:

Re: Draw instances of a sprite in loop? Can I use string as

#3 Post by Mild Curry » Tue Jan 10, 2012 9:42 pm

While the above method will probably work fine, just wanted to point out that Renpy does natively support 'while' loops.

Code: Select all

label populate_field:
   while numFields < 16:
      #execute whatever code 
      numFields += 1
also, for your second question, that should work, just remember to convert integer variables to strings:

$ fieldvar = "field" +str(fieldnum)

User avatar
leon
Miko-Class Veteran
Posts: 554
Joined: Sun Oct 09, 2011 11:15 pm
Completed: Visual Novel Tycoon, Night at the Hospital, Time Labyrinth, The Buried Moon, Left of Center, Super Otome Quest
Projects: Lemon Project, Porcelain Heart, Dream's Dénouement
Organization: Team ANARKY
Contact:

Re: Draw instances of a sprite in loop? Can I use string as

#4 Post by leon » Tue Jan 10, 2012 11:02 pm

Thanks for helping both of you!

I could actually even do without nesting, since there probably won't be that many plants and variations, but I need a loop for 16 fields, so I don't have to make every change 16 times.

@Mild Curry:
No, that didn't work...

I tried:

Code: Select all

while fieldnum <= 16:
    $ fieldvar = "field" +str(fieldnum)
    if 'plant1' in fieldvar:
I think Python/Ren'Py also needs to be told somehow, to treat fieldvar as a variable and not as a string?

I think the above gets interpreted as:

Code: Select all

if 'plant1' in 'field1':
instead of:

Code: Select all

if 'plant1' in field1:

User avatar
MoPark
Regular
Posts: 98
Joined: Sat Dec 31, 2011 7:05 pm
Projects: Kangaroo, Terminal Love
Location: DC
Contact:

Re: Draw instances of a sprite in loop? Can I use string as

#5 Post by MoPark » Tue Jan 10, 2012 11:10 pm

You could make a Python list, and populate it with field1 and field2.
Then you can access it as fieldList[0] and fieldList[1]

This may be useful: http://docs.python.org/tutorial/datastructures.html

User avatar
leon
Miko-Class Veteran
Posts: 554
Joined: Sun Oct 09, 2011 11:15 pm
Completed: Visual Novel Tycoon, Night at the Hospital, Time Labyrinth, The Buried Moon, Left of Center, Super Otome Quest
Projects: Lemon Project, Porcelain Heart, Dream's Dénouement
Organization: Team ANARKY
Contact:

Re: Draw instances of a sprite in loop? Can I use string as

#6 Post by leon » Wed Jan 11, 2012 3:06 pm

MoPark wrote:You could make a Python list, and populate it with field1 and field2.
Then you can access it as fieldList[0] and fieldList[1]

This may be useful: http://docs.python.org/tutorial/datastructures.html
Thanks,

I was looking at that page before, but I couldn't quite figure out how to do it and if list is what I'm looking for...

I think I need a 2-dimensional list. Or something like that?

Variables field1 to field16 are lists that describe each field in the garden grid and look something like ['plant1', '1'] or to be more descriptive ['strawberries', 'young'].

So I would need something like:
[1]['plant1', '1']
[2]['plant2', '3']
[3]['rough']

I tried:

Code: Select all

$ fieldlist[1] = field1
$ fieldlist[2] = field2
But I got an error: name 'fieldlist' is not defined

I'm new to Python. I think I'm just not doing this with its logic...

Update: I solved this by adding:

$ current_field = field1

$ current_field = field2
...

It will require 16 if-sentences, but I can now use current_field in the loop. It's an ugly solution, but it solves this part of the problem.

------

There is another (probably the biggest) issue, to make this working in a loop. Can I display one image several times in a loop?

Each iteration in the loop should display a new instance for every plant not replace the previous one.

For example, if you are growing strawberies on two fields, both should be displayed:
#1st field, 1st loop iteration:
show plant1 as ha1
#2nd field, 2nd loop iteration:
show plant1 as ha2

How could I have the parts 'as ha1', 'as ha2' ... 'as ha16', working in a loop? Either replaced with a variable or with some other method.

User avatar
Mild Curry
Regular
Posts: 107
Joined: Fri Jul 02, 2010 3:03 pm
Projects: That's The Way The Cookie Crumbles
Organization: TwinTurtle Games
Contact:

Re: Can I display one image several times in a loop?

#7 Post by Mild Curry » Wed Jan 11, 2012 3:37 pm

1) Don't worry, it's just your syntax. You're treating lists like arrays. (which they are, kinda, but different)

$ fieldlist = [field1, field2]

You can change the elements in a list using the method you were using, but you have to define the list first.

2) Totally possible.

Code: Select all

$ fieldCount = 0
while fieldCount <16:
  show plant1 as plant_tag
  $ plant_tag = "ha"+str(field_count)
  $ fieldCount += 1
The above is untested, if 'as' doesn't accept variables, then there's always renpy.show, which does the same thing with a python function.

User avatar
leon
Miko-Class Veteran
Posts: 554
Joined: Sun Oct 09, 2011 11:15 pm
Completed: Visual Novel Tycoon, Night at the Hospital, Time Labyrinth, The Buried Moon, Left of Center, Super Otome Quest
Projects: Lemon Project, Porcelain Heart, Dream's Dénouement
Organization: Team ANARKY
Contact:

Re: Can I display one image several times in a loop?

#8 Post by leon » Wed Jan 11, 2012 4:32 pm

Thanks! It's all working now. Yes, I had to use renpy.show to use a variable.

Post Reply

Who is online

Users browsing this forum: No registered users