On changing variables while using ui.callsinnewcontext()

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
Kinsman
Regular
Posts: 130
Joined: Sun Jul 26, 2009 7:07 pm
Location: Fredericton, NB, Canada
Contact:

On changing variables while using ui.callsinnewcontext()

#1 Post by Kinsman »

I've doing some tests in Ren'Py 6.9.2, and I'm running into some trouble with variables.

What I wanted to do was create some "always-available" options. Two examples would be
a) A 'cyclopedia' that I could summon at any time to look up topics or histories;
b) A special option where I could at any time.. oh.. let's say "commune with my goddess, and after a short conversation, gain some new inner strength that I could take back to the conversation at hand."

Option a isn't too much trouble.

Code: Select all

    
image encyclopediabutton = Button(Text("Encyclopedia"),clicked=ui.callsinnewcontext("AClicked"))

# and then
show encyclopediabutton at Position(xalign=0.1, yalign=0.1)

# and then
label AClicked:
    hide encyclopediabutton
    nvl clear
    definition "We're pretending that this is an encyclopedia section."
    definition "Done?  Good."
    show encyclopediabutton
    return

But option B is trouble - it includes variable changes:

Code: Select all

    
image goddessbutton = Button(Text("Commune With Goddess"),clicked=ui.callsinnewcontext("Goddess"))

# and then
show goddessbutton at Position(xalign=0.1, yalign=0.1)

# and then
label Goddess:
    hide goddessbutton
    nvl clear
    "The present moment freezes, and then fades away into darkness."
    "Goddess" "So, Tetsuo, you've come again to pray to me?"
    if wishcount > 0:
       "Goddess" "So be it.  Let me give you the strength you need."
       $wishcount -= 1
       $tetsuo_innerstrength += 10
       if wishcount == 0:
          "Goddess" "But from now on, you're on your own."
    show goddessbutton
    return

Now, the issue here is that the goddess function occurs in another context. When I return to the main thread of the story, wishcount and tetsuo_innerstrength behave like they're changed; but if I save and then load that save, it's as if the goddess encounter had never happened.

This is even when those variables were defined at the main thread in the first place.

It downright confused me why that would be, until I did some studying and realized that Ren'py doesn't so much save the top-level context as watches it carefully, and notes any changes - and apparently, only notes them when the top context is running.

I found you could force Ren'py to save accurately by doing this:

Code: Select all

$wishcount = wishcount
$tetsuo_innerstrength = tetsuo_innerstrength
But scattering code like that everywhere would defeat the purpose of writing an always-available option.

I tried a few things. I read using renpy.dynamic to mark variables, and tried setting vars(renpy.store) directly from inside the new context, but in either case, there still was a discrepancy.

How can I get the save tracker to notice variable changes in a different context?
Flash To Ren'Py Exporter
See the Cookbook thread

Kinsman
Regular
Posts: 130
Joined: Sun Jul 26, 2009 7:07 pm
Location: Fredericton, NB, Canada
Contact:

Re: On changing variables while using ui.callsinnewcontext()

#2 Post by Kinsman »

I see what I can do now.

I can redefine the default say statement, and then use various UI functions to make it simulate a regular say statement, with the ability to click on extra buttons that can make calls, without having to deal with new contexts.

New contexts interfere with the natural rollback, anyway, so doing this is the best solution.
Flash To Ren'Py Exporter
See the Cookbook thread

JQuartz
Eileen-Class Veteran
Posts: 1265
Joined: Fri Aug 31, 2007 7:02 am
Projects: 0 completed game. Still haven't made any meaningfully completed games...
Contact:

Re: On changing variables while using ui.callsinnewcontext()

#3 Post by JQuartz »

Thanks for the warning. All my games uses this ui.callsinnewcontext to change a lot of variables(eg. Exp, Hp, MP, potions)...

Anyway, I found this fix is pretty okay(It can't save the data straight away though. The player needs to go to a new dialogue first):

Code: Select all

  
init:
    image goddessbutton = Button(Text("Commune With Goddess"),clicked=ui.callsinnewcontext("Goddess"))
    $wishcount = 1
    $tetsuo_innerstrength = 10
    
init python:
    
    def savedata(event, **kwargs):# added this
        global wishcount
        global tetsuo_innerstrength
        wishcount =wishcount
        tetsuo_innerstrength = tetsuo_innerstrength

    t = Character("Tetsuo", callback=savedata)#set the callback for any other character to savedata as well.

label Goddess:
    hide goddessbutton
    nvl clear
    "The present moment freezes, and then fades away into darkness."
    "Goddess" "So, Tetsuo, you've come again to pray to me?"
    if wishcount > 0:
        
       "Goddess" "So be it.  Let me give you the strength you need."
       $ wishcount -= 1
       $ tetsuo_innerstrength += 10
       
       if wishcount == 0:
          "Goddess" "But from now on, you're on your own."
    
    return
    

label start:
    $ ui.textbutton("Goddess", clicked=ui.callsinnewcontext("Goddess"))
    t "Nothing here."
    t "Okay,data saved"
    jump start
I suspect somebody is stealing my internet identity so don't believe everything I tell you via messages. I don't post or send messages anymore so don't believe anything I tell you via messages or posts.

Kinsman
Regular
Posts: 130
Joined: Sun Jul 26, 2009 7:07 pm
Location: Fredericton, NB, Canada
Contact:

Re: On changing variables while using ui.callsinnewcontext()

#4 Post by Kinsman »

In the new version (6.9.3) the problem is solved :) You can use new contexts as you want, change variables, and have the rollback and save remember everything.
Flash To Ren'Py Exporter
See the Cookbook thread

JQuartz
Eileen-Class Veteran
Posts: 1265
Joined: Fri Aug 31, 2007 7:02 am
Projects: 0 completed game. Still haven't made any meaningfully completed games...
Contact:

Re: On changing variables while using ui.callsinnewcontext()

#5 Post by JQuartz »

Kinsman wrote:In the new version (6.9.3) the problem is solved :)
Thanks. Gotta keep myself updated...
I suspect somebody is stealing my internet identity so don't believe everything I tell you via messages. I don't post or send messages anymore so don't believe anything I tell you via messages or posts.

Post Reply

Who is online

Users browsing this forum: No registered users