Toggle a non boolean variable in an image button action

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
sculpteur
Veteran
Posts: 312
Joined: Fri Nov 17, 2017 6:40 pm
Completed: Apocalypse Lovers
Projects: Apocalypse Lovers
Organization: Awake_Production
Location: France
Discord: https://discord.gg/apocalypse-lovers
Contact:

Toggle a non boolean variable in an image button action

#1 Post by sculpteur »

Hello, I'am having an issue to do something really simple but it's because I am strugguling with imagebutton action.
Even if I read carfully this page https://www.renpy.org/doc/html/screen_a ... magebutton,
I can't find a way to do what I want.

I have this imagebutton in the mainscreen :

Code: Select all

screen inventory_main:
    modal True
    add "gui/Inventory_bar_simple.png" xpos 0.0015 ypos 0.75

    imagebutton:
        xpos 0.174 ypos 0.75
        idle "gui/Inventory_bar_simple_button_blank.png"
        hover "gui/Inventory_bar_simple_button_hover.png"
        action [ToggleScreen("inventory_main_button_trash"), ToggleScreen("inventory_main_button_equip")]
I have this other screen and imagebutton :

Code: Select all

screen inventory_main_button_trash:
    # modal True
    imagebutton:
        xpos 0.174 ypos 0.75
        idle "gui/inventory_main_button_trash.png"
        hover "gui/inventory_main_button_trash_hover.png"
        action [ToggleScreen("container_trash"), 'THE DIFFICULT PART IS HERE"]
What I want to do is to toggle a variable here. But it's not a boolean variable (this I know how to do it).
The variable ("container") will have to switch between this to
container=container_trash
container=container

First I used the "call" fonction in the action line, like this :

Code: Select all

        action [ToggleScreen("container_trash"), Function(toggle_trash)]

Code: Select all

 label inventory_main_activate_trash:
     if trash_session:
         $ container=container
         $ trash_session=False
     else:
         $ container=container_trash
         $ trash_session=True
There was some probleme with other part of my code so i tried to do something simplier, by using a fonction, like this :

Code: Select all

        action [ToggleScreen("container_trash"), Function(toggle_trash)]

Code: Select all

init -1 python:
    def toggle_trash():
        if trash_session:
            container=container
            trash_session=False
        else:
            container=container_trash
            trash_session=True
But it's giving me an error : UnboundLocalError : local variable 'trash session' referenced before assignement.

So if someone is enough talented in Python can help me understand the error or give me a better and cleaner way to do what I want I would be really grateful !

Thanks
Image

“He asked me to calm down, close my eyes and be quiet. He explained to me that if I was afraid, the shadow that ran barefoot in the street would feel it. I got scared seeing Jumanji on TV, so let me tell you, we didn't stay hidden for long and had to start running again.”
Jessica's Diary.

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: Toggle a non boolean variable in an image button action

#2 Post by Remix »

Umm, you cannot set it back to container once it is container_trash (as at that time setting it to container IS setting it to container_trash)
You likely want a container_trash and container_non_trash

Anyway, an example using varA and varB:

SetVariable( "container", (varA if container == varB else varB) )

Sub-note:
Your function one might have worked at init +1 python... at -1 it runs before 0 so before defaults or defines
Frameworks & Scriptlets:

sculpteur
Veteran
Posts: 312
Joined: Fri Nov 17, 2017 6:40 pm
Completed: Apocalypse Lovers
Projects: Apocalypse Lovers
Organization: Awake_Production
Location: France
Discord: https://discord.gg/apocalypse-lovers
Contact:

Re: Toggle a non boolean variable in an image button action

#3 Post by sculpteur »

SetVariable( "container", (varA if container == varB else varB) )
Thank you really much dude, that's work.
For some reason above my knowlegde I had to inverse it in order to make it work
(varB if container == varA else varA) instead of (varA if container == varB else varB) ^^

And about your sub-note:
Sub-note:
Your function one might have worked at init +1 python... at -1 it runs before 0 so before defaults or defines
Okey but why ? i don't understand. I mean why it woulnd work if it's run before defaults or defines ?
I don't need it so its just for information, I will die a little bit smarter like this ^^
Image

“He asked me to calm down, close my eyes and be quiet. He explained to me that if I was afraid, the shadow that ran barefoot in the street would feel it. I got scared seeing Jumanji on TV, so let me tell you, we didn't stay hidden for long and had to start running again.”
Jessica's Diary.

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: Toggle a non boolean variable in an image button action

#4 Post by Remix »

All the inits run in order, from negative lots to positive lots.

default and define run at init 0 (zero) so any negative init runs before them and any positive runs after them

UnboundLocalError : local variable 'trash session' referenced before assignement.

Referencing 'trash session' (or trash_session) in an init -1 is therefore effectively saying to python " if 'something_that_you_have_not_defined_yet' == True " or somesuch as that trash_session is not created until init 0
Frameworks & Scriptlets:

sculpteur
Veteran
Posts: 312
Joined: Fri Nov 17, 2017 6:40 pm
Completed: Apocalypse Lovers
Projects: Apocalypse Lovers
Organization: Awake_Production
Location: France
Discord: https://discord.gg/apocalypse-lovers
Contact:

Re: Toggle a non boolean variable in an image button action

#5 Post by sculpteur »

All right great I understand know.
It's because I had declared my variable trashsession in the "start" label

Code: Select all

label start:
$ trash_session = False
So i understand know, because the start label is after init -1.
That's make sens.
Thanks again !
Image

“He asked me to calm down, close my eyes and be quiet. He explained to me that if I was afraid, the shadow that ran barefoot in the street would feel it. I got scared seeing Jumanji on TV, so let me tell you, we didn't stay hidden for long and had to start running again.”
Jessica's Diary.

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot]