SetVariable not setting variable [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
pucedragonlord
Regular
Posts: 159
Joined: Wed May 09, 2012 2:49 am
Projects: The Diviner
Organization: Two Crowns Entertainment
Location: Now: Charlottesville, VA
Contact:

SetVariable not setting variable [solved]

#1 Post by pucedragonlord »

I'm trying to get my game to remember the slot the player last manually saved in. If there's a way to call renpy.newest_slot() when the screen closes, that could work, but I cannot recall if there's a way to do that.

As an alternative, I'm using the SetVariable() action when the player saves or loads a slot by sticking the action in next to FileAction(i) in file_picker:

Code: Select all

                # Each file slot is a button.
                button:
                    action [SetVariable("thisFile", i), FileAction(i)]
                    xfill True

                    has hbox

                    # Add the screenshot.
                    add FileScreenshot(i)

                    $ file_name = FileSlotName(i, 99)
                    $ file_time = FileTime(i, empty=_("Empty Slot."))
                    $ save_name = FileSaveName(i)

                    text "[file_name]. [file_time!t]\n[save_name!t]"

                    key "save_delete" action FileDelete(i)
Trouble is, the variable doesn't change. It just stays as its default value of -1. No errors are thrown, the game saves okay, but the variable doesn't set. It's pre defined in an init block and at the beginning of the start label so it sticks with the save file if I need it to. I've tried saving at several points to make it work, but it never changes. Any ideas?
Last edited by pucedragonlord on Thu Aug 14, 2014 5:50 pm, edited 1 time in total.
The more you know

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: SetVariable not setting variable

#2 Post by xela »

Ren'Py documentation wrote:

Code: Select all

On

The on statement allows the screen to execute an action when an event occurs. It takes one parameter, a string giving the name of an event. This should be one of:

    "show"
    "hide"
    "replace"
    "replaced"

It then takes an action property, giving an action to run if the event occurs.

screen preferences:
    frame:
        has hbox

        text "Display"
        textbutton "Fullscreen" action Preferences("display", "fullscreen")
        textbutton "Window" action Preferences("display", "window")

    on "show" action Show("navigation")
    on "hide" action Hide("navigation")
Ren'Py documentation wrote:

Code: Select all

class Action

    To define a new action, inherit from this class. Override the methods in this class to change the behavior of the action.

    __call__(self)

        This is the method that is called when the action is activated. In many cases, returning a non-None value from the action will cause the current interaction to end.

        This method must be overriden, as the default method will raise NotImplemented (and hence cause Ren'Py to report an error).

    get_sensitive(self)

        This is called to determine if the button with this action should be sensitive. It should return true if the button is sensitive.

        Note that __call__ can be called, even if this returns False.

        The default implementation returns True.

    get_selected(self)

        This should return true if the button should be rendered as a selected button, and false otherwise.

        The default implemention returns False.

    periodic(self, st)

        This method is called once at the start of each interaction, and then is called periodically thereafter. If it returns a number, it will be called before that many seconds elapse, but it might be called sooner.

        The main use of this is to call renpy.restart_interaction() if the value of get_selected or get_sensitive should change.

        It takes one argument:

        st
            The number of seconds since the screen or displayable this action is associated with was first shown.

    unhovered(self):

        When the action is used as the hovered parameter to a button (or similar object), this method is called when the object loses focus.
I have no idea how this whole thing is useful but you should be able to call renpy.newest_slot() with a custom screen action if that helps you out.

**Calling default Ren'Py menu screens (save/load/preferences, etc..) using ShowMenu pools ya right out of games context. That's why you SetVariable doesn't do anything fruitful, it cannot access store through usual lookup process.
Like what we're doing? Support us at:
Image

User avatar
Alex
Lemma-Class Veteran
Posts: 3093
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: SetVariable not setting variable

#3 Post by Alex »

It's pre defined in an init block
This might be the issue, try to use persistent variable instead and declare it as usual

Code: Select all

if not persistent.my_var:
    persistent.my_var = "value"

pucedragonlord
Regular
Posts: 159
Joined: Wed May 09, 2012 2:49 am
Projects: The Diviner
Organization: Two Crowns Entertainment
Location: Now: Charlottesville, VA
Contact:

Re: SetVariable not setting variable

#4 Post by pucedragonlord »

xela wrote:
I have no idea how this whole thing is useful but you should be able to call renpy.newest_slot() with a custom screen action if that helps you out.

**Calling default Ren'Py menu screens (save/load/preferences, etc..) using ShowMenu pools ya right out of games context. That's why you SetVariable doesn't do anything fruitful, it cannot access store through usual lookup process.
That's probably going to have to be how I go about it. I did dig up the on "hide"/"replaced" bit in the documentation and tried it, but it only works the first time you open and close the save menu for some reason. Someone will have to explain that one to me before I get it. I got nothing on why.

I didn't know new contexts broke the lookup process. I would have thought an init-defined global would be accessible even in new contexts.

I've never tried inheriting from Action before. For it to work the syntax is just normal Python nonsense like

Code: Select all

class CustomAction(Action.object):
    def __call__(self, args):
        dostuffwithargs(args)
right?
The more you know

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: SetVariable not setting variable

#5 Post by xela »

Inherit from Action, not Action.object.

You call would work but you need to unpack the arguments and pass them from __init__.

+ I don't have any clue to what you're trying to achieve, this all seems useless to me.
Like what we're doing? Support us at:
Image

pucedragonlord
Regular
Posts: 159
Joined: Wed May 09, 2012 2:49 am
Projects: The Diviner
Organization: Two Crowns Entertainment
Location: Now: Charlottesville, VA
Contact:

Re: SetVariable not setting variable

#6 Post by pucedragonlord »

I'm trying to get the game to remember which file the player chose for their game so the game can save over it automatically, like how the Souls series saves whenever you do anything. It's for a higher difficulty setting, where it's impossible to take back your decisions, it's just odd that I can't change this one variable.

Saving it as persistent data works as intended. Thanks for all your help, everyone. Much was learned today.
The more you know

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot]