[Solved] Toggling Checkboxes - Remembering the "Check"?

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
Eldin
Newbie
Posts: 4
Joined: Thu Apr 12, 2012 12:45 am
Contact:

[Solved] Toggling Checkboxes - Remembering the "Check"?

#1 Post by Eldin »

Forgive me for any obvious mistakes, I'm pretty new to this.

I have made an image map for the game menu, as shown here: http://i.imgur.com/VaVte.png

And I've configured the "Auto Mode", so when the user clicks it, it toggles a check mark on and off beside it (as shown in the image). I've already implemented a portion of Python script written by PyTom that enables the toggling of "Auto Mode", and this works great... Just an aspect of the checkbox function I'm having trouble with.

What works:
Toggling the AFM on/off works, and 'toggling' the image of the checkmark works, using the "toggle_img" I have defined. (Is there an easier way to do this?)

What doesn't work: When I check the "Auto Mode" off and return to game, it happily putters along, but when I return to the game menu, the check mark is gone; so it doesn't remember that I've checked the box, even though Auto Mode is on: _preferences.afm_enable (Although I am aware the 'check' for it being enabled is done only when on 'clicked')

So, I thought if I added a sort of "check" to see if Auto Mode was on, when you opened the menu, that it would display the checkmark. The code I wrote for this is:

Code: Select all

    python: 
        ui.layer('checkmark')
        if _preferences.afm_enable:
           ui.image("imgon.png")
        ui.close()
And I popped it directly under:

Code: Select all

 screen navigation:
This sort of works, in a sense, the only problem being that, wherever in the game you are, including the game menu, when _preferences.afm_enable it shows the checkmark, everywhere, above everything else... So even during character dialogue, and in the options menu, for example.

My hacked up solution (sucks!), and doesn't even really work... I think I'm completely on the wrong track, halp!.


TL;DR: Checkbox not remembering that I've ticked it upon return to the menu where it was ticked.


Here's the code for the entire screen, untouched by my "solution". I'm using "imgon.png" for the check, and "imgoff.png" for a small section of the background that this just layers over top of, hiding the check. (Again, there must be a way to do it so it just hides the checkmark image, right?)

Note: I added the checkmark images to different layers, "screens" or "checkmark", as they were previously appearing behind the menu background.

Code: Select all

screen navigation:
    tag menu
    modal True
    
    window:
        style "gm_root"
        
    imagemap:
        ground "new_main_nav_ground_2.png"
        hover "new_main_nav_hover_bold.png"
        
        hotspot (226, 137, 457, 170) clicked Return()
        hotspot (226, 181, 457, 212) clicked QuickSave(message = "Progress saved.")
        hotspot (226, 226, 457, 260) clicked ShowMenu("load_game")
        hotspot (256, 271, 487, 300) clicked ShowMenu("text_history")
        hotspot (236, 314, 467, 344) clicked ShowMenu("preferences_game")
        hotspot (236, 358, 477, 388) clicked MainMenu(confirm=True)
        hotspot (226, 402, 457, 430) clicked Quit()
        hotspot (303, 460, 402, 474) clicked Skip()
        hotspot (429, 460, 457, 474) clicked (toggle_afm, toggle_img)
                    
 
        
init python:
   
     _preferences.afm_enable = False
     
     if not persistent.set_afm:
            _preferences.afm_time = 10 
            persistent.set_afm = True
            
     def toggle_img():
           ui.layer('screens')
           if _preferences.afm_enable:
                ui.image("imgon.png")
           else:
                ui.image("imgoff.png")
           ui.close()

     def toggle_afm():
           _preferences.afm_enable = not _preferences.afm_enable
           renpy.restart_interaction()
It would be useful to find a more universal approach, so checkboxes can be configured elsewhere, such as in the Options, without this massive amount of seemingly unecessary fuss and confusion.

Again, forgive me for my noobishness, just started my first attempts at doing any type of scripting on Sunday.

Thank you all very much for your time, in advance.
Last edited by Eldin on Sun Apr 15, 2012 2:15 pm, edited 1 time in total.

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: Toggling Checkboxes - Remembering the "Check"?

#2 Post by Mild Curry »

What would allow you to avoid a lot of this hassle is to take advantage of some natural screen functions.
Instead of having to write a whole separate python function to show an image, just stick this in the screen:

Code: Select all

  
           if _preferences.afm_enable:
                add "imgon.png"
           else:
                add "imgoff.png"
Additionally, screen language has a built in toggle action, so you don't need to write a new one for every variable you want to toggle.

Code: Select all

action ToggleVariable("whatever")
or, in the case of preferences...

Code: Select all

action ToggleField("preferences", "afm_enable")
(the above should work but I haven't tested it, so lemme know if something goes wrong)

sciencewarrior
Veteran
Posts: 356
Joined: Tue Aug 12, 2008 12:02 pm
Projects: Valentine Square (writer) Spiral Destiny (programmer)
Location: The treacherous Brazilian Rainforest
Contact:

Re: Toggling Checkboxes - Remembering the "Check"?

#3 Post by sciencewarrior »

There's a helper action for it:

Code: Select all

    action Preference("auto-forward", "toggle")
Keep your script in your Dropbox folder.
It allows you to share files with your team, keeps backups of previous versions, and is ridiculously easy to use.

User avatar
PyTom
Ren'Py Creator
Posts: 16093
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Toggling Checkboxes - Remembering the "Check"?

#4 Post by PyTom »

Also, the first argument to ToggleField is an object:

Code: Select all

ToggleField(myobject, "thefield")
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

Eldin
Newbie
Posts: 4
Joined: Thu Apr 12, 2012 12:45 am
Contact:

Re: Toggling Checkboxes - Remembering the "Check"?

#5 Post by Eldin »

Hello all, thanks very much for the help.

After implementing some solutions above, I've got everything to work, including toggling the "auto mode", image, and having it "remember" if the box was checked. Except there's a slight relapse to one of my original issues, the checkmark images appear behind the game menu, so they aren't visible... Is there a way to set the zorder, or put these images on the new layer that I have defined, as so that they appear in front of the game menu?

Here's the current code:

Code: Select all

screen navigation:
    tag menu
    modal True
    if _preferences.afm_enable:
                image ("imgon.png")    #or add
    else:
                image ("imgoff.png")    #or add

                
    window:
        style "gm_root"
        
    imagemap:
        ground "new_main_nav_ground_2.png"
        hover "new_main_nav_hover_bold.png"
        
        hotspot (226, 137, 457, 170) clicked Return()
        hotspot (226, 181, 457, 212) clicked QuickSave(message = "Progress saved.")
        hotspot (226, 226, 457, 260) clicked ShowMenu("load_game")
        hotspot (256, 271, 487, 300) clicked ShowMenu("text_history")
        hotspot (236, 314, 467, 344) clicked ShowMenu("preferences_game")
        hotspot (236, 358, 477, 388) clicked MainMenu(confirm=True)
        hotspot (226, 402, 457, 430) clicked Quit()
        hotspot (303, 460, 402, 474) clicked Skip()
        hotspot (429, 460, 457, 474) clicked Preference("auto-forward", "toggle")
                    
        
init python:
   
     _preferences.afm_enable = False
     
     if not persistent.set_afm:
            _preferences.afm_time = 10 
            persistent.set_afm = True
 
Thanks again!

sciencewarrior
Veteran
Posts: 356
Joined: Tue Aug 12, 2008 12:02 pm
Projects: Valentine Square (writer) Spiral Destiny (programmer)
Location: The treacherous Brazilian Rainforest
Contact:

Re: Toggling Checkboxes - Remembering the "Check"?

#6 Post by sciencewarrior »

Putting the if/else block after the imagemap should work. Elements added later have a higher zorder.
Keep your script in your Dropbox folder.
It allows you to share files with your team, keeps backups of previous versions, and is ridiculously easy to use.

Eldin
Newbie
Posts: 4
Joined: Thu Apr 12, 2012 12:45 am
Contact:

Re: Toggling Checkboxes - Remembering the "Check"?

#7 Post by Eldin »

That works, thanks!

Post Reply

Who is online

Users browsing this forum: Google [Bot]