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
Regular
Posts: 94
Joined: Fri Nov 17, 2017 6:40 pm
Contact:

Toggle a non boolean variable in an image button action

#1 Post by sculpteur » Wed Oct 10, 2018 3:30 pm

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

User avatar
Remix
Eileen-Class Veteran
Posts: 1021
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 » Wed Oct 10, 2018 3:54 pm

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
Mad Scientist Todo List:
  • Finish Improved Event Handler
  • Implement DragonBones animation as a Creator Defined Container
  • Finish Catmull-Rom Spline pathing
  • Finish Bitmask collision and rebound vector system
  • Develop time based building mechanic
  • Others
  • *Find a superb artist and actually write a game*

sculpteur
Regular
Posts: 94
Joined: Fri Nov 17, 2017 6:40 pm
Contact:

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

#3 Post by sculpteur » Thu Oct 11, 2018 4:16 pm

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 ^^

User avatar
Remix
Eileen-Class Veteran
Posts: 1021
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 » Thu Oct 11, 2018 4:43 pm

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
Mad Scientist Todo List:
  • Finish Improved Event Handler
  • Implement DragonBones animation as a Creator Defined Container
  • Finish Catmull-Rom Spline pathing
  • Finish Bitmask collision and rebound vector system
  • Develop time based building mechanic
  • Others
  • *Find a superb artist and actually write a game*

sculpteur
Regular
Posts: 94
Joined: Fri Nov 17, 2017 6:40 pm
Contact:

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

#5 Post by sculpteur » Thu Oct 11, 2018 5:57 pm

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 !

Post Reply

Who is online

Users browsing this forum: No registered users