[Solved]Creating a loop that searches a specific variable

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
Doeny
Regular
Posts: 145
Joined: Wed Sep 07, 2022 8:28 pm
Contact:

[Solved]Creating a loop that searches a specific variable

#1 Post by Doeny »

Hi, I was wondering if there is a way to search for e.g. three variables from a whole dict that meets certain requirements.

So let's say we have a...

Code: Select all

default switch = buttons("button_1", 0, 1, 0 ,4 , 5, 0} #in sequence: image, and other corresponding values

screen value_screen:
	vbox:
		text "[value_1]
		text "[value_2]
		text "[value_3]
Can I make it so the text (values 1-3) is listed as every button's value that is higher than/isn't equal 0?
Last edited by Doeny on Wed Feb 14, 2024 2:52 pm, edited 1 time in total.

User avatar
m_from_space
Miko-Class Veteran
Posts: 985
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: Creating a loop that searches a specific variable

#2 Post by m_from_space »

Doeny wrote: Sat Jan 27, 2024 2:25 am Can I make it so the text (values 1-3) is listed as every button's value that is higher than/isn't equal 0?
Not sure I'm following. You also have typos in your code. What is this "buttons" class anyway? It's not a dict, that's for sure. You have to post the corresponding class if you need help.

Doeny
Regular
Posts: 145
Joined: Wed Sep 07, 2022 8:28 pm
Contact:

Re: Creating a loop that searches a specific variable

#3 Post by Doeny »

m_from_space wrote: Sat Jan 27, 2024 10:12 am Not sure I'm following. You also have typos in your code. What is this "buttons" class anyway? It's not a dict, that's for sure. You have to post the corresponding class if you need help.
The class itself isn't too complex. Just a placeholder for test purposes.

Code: Select all

init python:
    class buttons:
        def __init__(self, img, place1, place2, place3, place4, place5, place6):
            self.img = img
            self.place1= place1
            self.place2= place2
            self.place3= place3
            self.place4= place4
            self.place5= place5
            self.place6 = place6
What I'm trying to do is write a code that checks every 'place' variable in which value in higher than 0. And yes, I know you don't really need a class for what I've given, but at this point it's just a placeholder.

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2411
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Creating a loop that searches a specific variable

#4 Post by Ocelot »

1) If you have a bunch of varaibles with name that differs only by the number on the end, you really want to use a list there.

Code: Select all

init python:
    class buttons:
        def __init__(self, img, place1, place2, place3, place4, place5, place6):
            self.img = img
            self.places = [place1, place2, place3, place4, place5, place6]

# Or even
        def __init__(self, img, *args):
            self.img = img
            assert len(args) == 6
            self.places = list(args)
After that you can simply iterate over the list and pick values you need. Or use built-ins:

Code: Select all

    def extract_non_zero(self):
        return filter(None, self.places) # This returns a list of non-zero numbers
Then you can do whatever you like with this list:

Code: Select all

screen value_screen:
    vbox:
        for item in switch.extract_non_zero():
            text str(item)
< < insert Rick Cook quote here > >

Doeny
Regular
Posts: 145
Joined: Wed Sep 07, 2022 8:28 pm
Contact:

Re: Creating a loop that searches a specific variable

#5 Post by Doeny »

Ocelot wrote: Thu Feb 01, 2024 3:38 am

Code: Select all

init python:
    class buttons:
        def __init__(self, img, place1, place2, place3, place4, place5, place6):
            self.img = img
            self.places = [place1, place2, place3, place4, place5, place6]

    def extract_non_zero(self):
        return filter(None, self.places) # This returns a list of non-zero numbers
        
screen value_screen:
    vbox:
        for item in switch.extract_non_zero():
            text str(item)
For the first method it gives me an error saying that 'name switch" is not definied

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2411
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Creating a loop that searches a specific variable

#6 Post by Ocelot »

Doeny wrote: Wed Feb 07, 2024 6:31 pm For the first method it gives me an error saying that 'name switch" is not definied
I was relying on code you have shown, and switch was defined there...
< < insert Rick Cook quote here > >

Doeny
Regular
Posts: 145
Joined: Wed Sep 07, 2022 8:28 pm
Contact:

Re: Creating a loop that searches a specific variable

#7 Post by Doeny »

Ocelot wrote: Thu Feb 08, 2024 12:48 pm
Doeny wrote: Wed Feb 07, 2024 6:31 pm For the first method it gives me an error saying that 'name switch" is not definied
I was relying on code you have shown, and switch was defined there...
My bad, I forgot it's name after changing it.

Doeny
Regular
Posts: 145
Joined: Wed Sep 07, 2022 8:28 pm
Contact:

Re: Creating a loop that searches a specific variable

#8 Post by Doeny »

Ocelot wrote: Thu Feb 01, 2024 3:38 am

Code: Select all

screen value_screen:
    vbox:
        for item in switch.extract_non_zero():
            text str(item)
It's something I realized just by now, but the extract_non_zero method doesn't update it's value as one of the buttons are changed. Maybe 'cause it's pre-defined or I overlooked something with my code. If I'm right with the first case, is there a way to fit it into updated values?

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2411
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: [Solved]Creating a loop that searches a specific variable

#9 Post by Ocelot »

This depends on your code. Maybe you are changing external list which is not used by switch. Maybe you are changing it by something not restarting interaction. Maybe something else...
< < insert Rick Cook quote here > >

Post Reply

Who is online

Users browsing this forum: No registered users