Work with python instance

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
JFillip
Newbie
Posts: 14
Joined: Thu Sep 03, 2015 12:59 am
Contact:

Work with python instance

#1 Post by JFillip »

Hello. I'm new in renpy and so I want to apologize for stupid questions.
Question:
I import python module in renpy, then I create new instance in my game. Then I will change some variables of instance and go to main menu.

Code: Select all

init python:
    from py.test import A
    
    a = A()

#    class A:
#    def __init__(self):
#        self.a = 20
    
    def let():
        global a
        a.a += 1000
init:
    image bg = "bg washington.jpg"

screen count():
    add "bg washington.jpg"
    frame:
        add "money.png"
        background None
        vbox:
            text "{b}{color=#000000}[a.a]{/color}{/b}" xpos 46 ypos 46 xanchor 0.5 yanchor 0.5

screen test(): 
        use count
        imagebutton:
            auto "button_%s.png"
            action Function(let)
            focus_mask True

label start:
    call screen test
When I start new game all variables changing will saved. But I need clear new instance.
Any links are welcome.
Sorry if I spend your time and thank you for any help. :)

User avatar
PyTom
Ren'Py Creator
Posts: 16096
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: Work with python instance

#2 Post by PyTom »

Basically, you have to create A in a default statement, rather than an init python block.

Code: Select all

default a = A()
But note that fields on a won't participate in rollback, since the magic Ren'Py uses to make rollback work doesn't work in python files.
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

JFillip
Newbie
Posts: 14
Joined: Thu Sep 03, 2015 12:59 am
Contact:

Re: Work with python instance

#3 Post by JFillip »

I will try this code:

Code: Select all

init python:
    from py.test import A

#    class A:
#    def __init__(self):
#        self.a = 20
    
    def let():
        a
        a.a += 1000

init:
    image bg = "bg washington.jpg"

define a = A()

screen count():
    add "bg washington.jpg"
    frame:
        add "money.png"
        background None
        vbox:
            text "{b}{color=#000000}[a.a]{/color}{/b}" xpos 46 ypos 46 xanchor 0.5 yanchor 0.5

screen test(): 
        use count
        imagebutton:
            auto "button_%s.png"
            action Function(let)
            focus_mask True

label start:
    call screen test
It doesn't help.

User avatar
PyTom
Ren'Py Creator
Posts: 16096
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: Work with python instance

#4 Post by PyTom »

You want default, not define. And remember, this code won't work with rollback.
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

JFillip
Newbie
Posts: 14
Joined: Thu Sep 03, 2015 12:59 am
Contact:

Re: Work with python instance

#5 Post by JFillip »

Thank you! Sorry for the carelessness, I was programming all night...
But now everything has fallen into place (i mean using rollback). :D

Post Reply

Who is online

Users browsing this forum: Google [Bot]