Problem with changing value of variable by pressing the imagebutton

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
cursedarchi
Newbie
Posts: 8
Joined: Tue Feb 27, 2024 9:47 am
Projects: Грёзы Фантома 3: Война в Ярославле/Sweet Dreams of Fantom 3: Yaroslavl' at War
Discord: cursedarchi
Contact:

Problem with changing value of variable by pressing the imagebutton

#1 Post by cursedarchi »

i want to make a scene where player clicks on clothes like "collecting" them around the room. i making whole game on russian, don't mind it.
when player press the imagebutton of any cloth, it dissapears and add an 1 to count value. i read about IncrementVariable(), added it to my code, and... it doesnt work >_<

here is the code

Code: Select all

#экран с вещами в начале
init -1:
    $ clothesCount = 0

screen clothesRoom1():
    imagebutton:
        activate_sound "audio/sound/activate_button2.mp3"
        ypos 0.4
        xpos 0.7
        idle 'wolf_idle.png'
        hover 'wolf_hover.png'
        action [Hide('clothesRoom1'), Notify('Вы подобрали трусы с волком'), IncrementVariable("clothesCount", clothesCount+1)]

screen clothesRoom2():
    imagebutton:
        activate_sound "audio/sound/activate_button2.mp3"
        action [Hide('clothesRoom2'), Notify('Вы подобрали носок'), IncrementVariable("clothesCount", clothesCount+1)]
        ypos 0.5
        xpos 0.5
        idle 'nosok_idle.png'
        hover 'nosok_hover.png'  
maybe problem in sintax, pls help!

error says that IncrementVariable is not defined

User avatar
m_from_space
Eileen-Class Veteran
Posts: 1030
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: Problem with changing value of variable by pressing the imagebutton

#2 Post by m_from_space »

cursedarchi wrote: Tue Feb 27, 2024 1:00 pmi read about IncrementVariable(), added it to my code, and... it doesnt work >_<
IncrementVariable() is new and only available since Renpy 8.2 (or 7.7 if you're using Python2, which you really shouldn't). So make sure to update your game using the launcher!

But do also understand, that your code still is using it in the wrong way, the function usually only takes one argument, the variable.

Code: Select all

# will increment by 1
IncrementVariable("clothesCount")

# former way of doing it
SetVariable("clothesCount", clothesCount + 1)

# will increment by 5
IncrementVariable("clothesCount", amount=5)
Also I advice to never define variables inside a (Python) init block, if you're using those variables for Renpy. Renpy will treat your variable as a constant doing so!

Correct way (outside of any init):

Code: Select all

default clothesCount = 0

User avatar
cursedarchi
Newbie
Posts: 8
Joined: Tue Feb 27, 2024 9:47 am
Projects: Грёзы Фантома 3: Война в Ярославле/Sweet Dreams of Fantom 3: Yaroslavl' at War
Discord: cursedarchi
Contact:

Re: Problem with changing value of variable by pressing the imagebutton

#3 Post by cursedarchi »

thanks, i updated sdk and changed code as u recommended:

Code: Select all

default clothesCount = 0

screen clothesRoom1():
    imagebutton:
        activate_sound "audio/sound/activate_button2.mp3"
        ypos 0.4
        xpos 0.7
        idle 'wolf_idle.png'
        hover 'wolf_hover.png'
        action [Hide('clothesRoom1'), Notify('Вы подобрали трусы с волком'), IncrementVariable("clothesCount")]

screen clothesRoom2():
    imagebutton:
        activate_sound "audio/sound/activate_button2.mp3"
        action [Hide('clothesRoom2'), Notify('Вы подобрали носок'), IncrementVariable("clothesCount")]
        ypos 0.5
        xpos 0.5
        idle 'nosok_idle.png'
        hover 'nosok_hover.png'
the game start working without errors, that's very good. but its still doesnt count my clothes(... for clarity i made a textbar

Code: Select all

    show screen clothesRoom1
    show screen clothesRoom2
    show text 'picked [clothesCount]':
        yalign 0.5
        xalign 1.0
    python:
        ui.interact()

jeffster
Veteran
Posts: 499
Joined: Wed Feb 03, 2021 9:55 pm
Contact:

Re: Problem with changing value of variable by pressing the imagebutton

#4 Post by jeffster »

It's easier to do it in one screen.
Show there the room background (let's say it's an image called "room_background"):

Code: Select all

default wolf = True
default nosok = True

screen clean_room():
    add room_background

    if wolf:
        imagebutton:
            activate_sound "audio/sound/activate_button2.mp3"
            ypos 0.4
            xpos 0.7
            idle 'wolf_idle.png'
            hover 'wolf_hover.png'
            action Return("wolf")

    if nosok:
        imagebutton:
            activate_sound "audio/sound/activate_button2.mp3"
            ypos 0.5
            xpos 0.5
            idle 'nosok_idle.png'
            hover 'nosok_hover.png'
            action Return("nosok")
And call this screen until all items are collected (or whatever your condition to move on is):

Code: Select all

label start_cleaning:
    call screen clean_room

    if _return == "wolf":
        $ wolf = False
        $ renpy.notify('Вы подобрали трусы с волком')

    elif _return == "nosok":
        $ nosok = False
        $ renpy.notify('Вы подобрали носок')

    if wolf or nosok:
        jump start_cleaning

    "Вы подобрали all of the 2 clothing items!"
PS. And if you really need to count how many items you gathered, you can use type conversion from bool to integer, because True is equivalent to 1, and False is equivalent to 0. Hence

Code: Select all

    not_found = nosok + wolf
If both "nosok" and "wolf" were found, "not_found" = 0.
If one was found, but not another, "not_found" = 1.
If both were not found, "not_found" = 2.

Or if you want, you can use other values, for example

Code: Select all

default wolf = 0
default nosok = 0

screen clean_room():
    add room_background

    if not wolf:
        imagebutton:
        #...
    if not nosok:
        imagebutton:
        #...

# And calling the screen
label start_cleaning:
    call screen clean_room

    if _return == "wolf":
        $ wolf = 1
        #...

    elif _return == "nosok":
        $ nosok = 1
        #...

    found = wolf + nosok
It will work the same way as the first variant, and "found" would be the amount of found items.
If the problem is solved, please edit the original post and add [SOLVED] to the title. 8)

User avatar
m_from_space
Eileen-Class Veteran
Posts: 1030
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: Problem with changing value of variable by pressing the imagebutton

#5 Post by m_from_space »

cursedarchi wrote: Fri Mar 01, 2024 1:44 am the game start working without errors, that's very good. but its still doesnt count my clothes(... for clarity i made a textbar

Code: Select all

  show text 'picked [clothesCount]'
You created a text object with the contents of clothesCount at the time of creation. It will not alter its contents even if clothesCount changes.

If you want to check the contents of a variable you just can hit Shift+D and then click "variable viewer". I bet it changed.

User avatar
cursedarchi
Newbie
Posts: 8
Joined: Tue Feb 27, 2024 9:47 am
Projects: Грёзы Фантома 3: Война в Ярославле/Sweet Dreams of Fantom 3: Yaroslavl' at War
Discord: cursedarchi
Contact:

Re: Problem with changing value of variable by pressing the imagebutton

#6 Post by cursedarchi »

IT WORKS! Thank you kind man, I will pray for your health. I am now sitting happy like a child. I slightly changed the code to suit myself: I showed the background before cleaning, since my hero goes there and says a line and only then starts collecting clothes, and I added more of it. thanks again man

Post Reply

Who is online

Users browsing this forum: Google [Bot]