SetField Action Not Changing Persistent Variable's Value [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
User avatar
Treladon
Regular
Posts: 40
Joined: Sat Dec 31, 2016 3:20 pm
Projects: ToMaG, Feinted Game
Organization: Kuehler-Corrada Productions
Deviantart: Journie
Contact:

SetField Action Not Changing Persistent Variable's Value [SOLVED]

#1 Post by Treladon »

Hello, everyone. Could you please help me?

I'm trying to implement a menu that can only be called when the player has a certain amount of points. If an action is taken, the amount of points, defined as persistent.current_size, should be decreased by 1. However, this subtraction is not taking place. Here is the relevant code:

Code: Select all

init:
    python:
        if not persistent.current_size:
            persistent.current_size = 2 #starting at 2 for testing purposes 
Also:

Code: Select all

screen use_action(message="Are you sure you want to use an Action?"):
    
    modal True

    window:
        style "gm_root"

    frame:
        style_prefix "confirm"

        xfill True
        xmargin 200
        ypadding 25
        yalign .25

        vbox:
            xfill True
            spacing 25

            text _(message):
                text_align 0.5
                xalign 0.5

            hbox:
                spacing 100
                xalign .5
                textbutton _("Yes") action [Hide("use_action"), Function(renpy.call,"action_menu"), SetField(persistent,'current_size',persistent.current_size-1), Show("action_display")] #the problem seems to be here, but I am not sure.
                textbutton _("No") action [Hide("use_action")]
I test it in-game using a text interpolation after I use an action, but it always remains at 2.
Last edited by Treladon on Sat Jan 06, 2018 7:10 pm, edited 1 time in total.
WIP: The Tasks of Messengers and Guardians - Progress Page Here!

Errilhl
Regular
Posts: 164
Joined: Wed Nov 08, 2017 4:32 pm
Projects: HSS
Deviantart: studioerrilhl
Github: studioerrilhl
Contact:

Re: SetField Action Not Changing Persistent Variable's Value

#2 Post by Errilhl »

Not familiar with SetField(), but couldn't you just use a setattr()?
something like

Code: Select all

setattr(store,persistent.current_size,-1)
Currently working on: Image

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: SetField Action Not Changing Persistent Variable's Value

#3 Post by Remix »

Your syntax and everything seems correct though I have not had time to actually throw a test together. The only *niggles* I can think of that might be messing with it are:

Is it properly finding the old value? Have you tried SetField( persistent, 'current_size', 100 ) with hard coded value? Or store.persistent.current_size to access it?
The statement might run outside of a context and thereby not register the value update until next interact... try adding a RestartStatement() in the actions (note: I have not used that before and the documentation info is not great. It does mention persistent though, so maybe is worth a try)

Not much else I can think of. I've certainly used SetField( persistent, 'key', value ) before and it worked.
Frameworks & Scriptlets:

User avatar
Treladon
Regular
Posts: 40
Joined: Sat Dec 31, 2016 3:20 pm
Projects: ToMaG, Feinted Game
Organization: Kuehler-Corrada Productions
Deviantart: Journie
Contact:

Re: SetField Action Not Changing Persistent Variable's Value

#4 Post by Treladon »

Remix wrote: Sat Nov 25, 2017 6:28 am Your syntax and everything seems correct though I have not had time to actually throw a test together. The only *niggles* I can think of that might be messing with it are:

Is it properly finding the old value? Have you tried SetField( persistent, 'current_size', 100 ) with hard coded value? Or store.persistent.current_size to access it?
The statement might run outside of a context and thereby not register the value update until next interact... try adding a RestartStatement() in the actions (note: I have not used that before and the documentation info is not great. It does mention persistent though, so maybe is worth a try)

Not much else I can think of. I've certainly used SetField( persistent, 'key', value ) before and it worked.
Errilhl wrote: Sat Nov 25, 2017 3:15 am Not familiar with SetField(), but couldn't you just use a setattr()?
something like

Code: Select all

setattr(store,persistent.current_size,-1)
Hey, thanks so much for your responses and sorry for the late reply. Unfortunately, I tried everything each of you suggested, but nothing worked. However, I was able to figure out a solution. Instead of SetField, I used SetVariable and defined the new variable as the first line of the screen:

Code: Select all

screen use_action(message="Are you sure you want to use an Action?"):
    $ new_size = current_size-1 #Added variable definition here
    modal True

    window:
        style "gm_root"

    frame:
        style_prefix "confirm"

        xfill True
        xmargin 200
        ypadding 25
        yalign .25

        vbox:
            xfill True
            spacing 25

            text _(message):
                text_align 0.5
                xalign 0.5

            hbox:
                spacing 100
                xalign .5
                textbutton _("Yes") action [Hide("use_action"), Function(renpy.call,"action_menu"), SetVariable('current_size', new_size), Show("action_display")] #used SetVariable here
                textbutton _("No") action [Hide("use_action")] 
Fortunately, that fixed all my problems! I still don't know why SetField didn't work, but luckily all is well. Thanks anyway, though!
WIP: The Tasks of Messengers and Guardians - Progress Page Here!

Post Reply

Who is online

Users browsing this forum: Bing [Bot]