Infinite money glitch when scrolling up

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
kedta35
Regular
Posts: 46
Joined: Wed Aug 30, 2023 1:31 pm
Contact:

Infinite money glitch when scrolling up

#1 Post by kedta35 »

I'm trying to make it so that if any character wins in a blackjack mini-game, they get money added to their inventory. It works but the issue is that if you scroll up to one click before the money gets given to them and then let it play out again, you will get the same amount of money added on top of what you got before scrolling back up and you can infinitely repeat it.

I don't get why that's happening since I'm not using any persistent data to add money. In case it helps to understand better, the winner is chosen on a screen then the screen jumps to a "x" won blackjack label at which point they get money added to their inventory.

Inventory system:

Code: Select all

init python:

    class Items(object):
        def __init__(self, name, cost, weight, NoOwned, ID, description):
            self.name = name
            self.cost = cost
            self.weight = weight
            self.NoOwned = NoOwned
            self.ID = ID
            self.description = description

        def add_item(self):
            self.NoOwned += 1

        def remove_item(self):
            self.NoOwned -= 1
        
        def add_money_char1(self, currency_char1, amount):
            self.amount = amount
            currency_char1.NoOwned += amount            

        def add_item_store_char2(self, currency_char2):
            if currency_char2.NoOwned >= self.cost:
                currency_char2.NoOwned -= self.cost
                self.NoOwned += 1
            else:
                return

        def add_item_store_char1(self, currency_char1):
            if currency_char1.NoOwned >= self.cost:
                currency_char1.NoOwned -= self.cost
                self.NoOwned += 1
            else:
                return

    InventoryChar1 = []
    InventoryChar2 = []
    p = 0

    while p < 50:
        InventoryChar1.append(Items("none", 0, 0, 0, p, "none"))
        InventoryChar2.append(Items("none", 0, 0, 0, p, "none"))
        p += 1


    InventoryChar1 = [
        Items("Dolars", 1, 1, 0, 0, "Currency"),
    ]


    InventoryChar2 = [
        Items("Dollars", 1, 1, 0, 0, "Currency"),
    ]


Blackjack:

Code: Select all

            if char2Points > 21:
                play sound BJ_Lose volume .1
                show text "{size=+100}{b}BUST" at centerToOverTop(.5, 1.0)
                $ char1_blackjack_wins += 1
                if char1_blackjack_wins == 5:
                    jump Char1Win

Code: Select all

label Char1Win:
    
    if char1_blackjack_wins == 5:
        $ char1_win_money.add_money_char1(currency_char1, 50)
        $ char2_blackjack_wins = 0
        $ char1_blackjack_wins = 0
    else:
        pass
    

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

Re: Infinite money glitch when scrolling up

#2 Post by jeffster »

kedta35 wrote: Thu Apr 04, 2024 7:54 am the issue is that if you scroll up to one click before the money gets given to them and then let it play out again, you will get the same amount of money added on top of what you got before scrolling back up and you can infinitely repeat it.
I don't see how it happens exactly, but it's probably rollback.
https://www.renpy.org/doc/html/save_load_rollback.html

Scrolling up in Ren'Py rolls back the game state to the previous interaction.

To roll back and forth properly, values should be defined with "default" keyword:

Code: Select all

default InventoryChar1 = []
default InventoryChar2 = []
I have no idea what happens if you define them like this:

Code: Select all

init python:
    InventoryChar1 = []
    InventoryChar2 = []
They will probably not be rolled back, so running the same function again would add that money again.

Not mentioning that here you set InventoryChar1 and InventoryChar2 as some weird lists, and then rewrite them with different values:

Code: Select all

    InventoryChar1 = []
    InventoryChar2 = []
    p = 0
    while p < 50:
        InventoryChar1.append(Items("none", 0, 0, 0, p, "none"))
        InventoryChar2.append(Items("none", 0, 0, 0, p, "none"))
        p += 1

    InventoryChar1 = [
        Items("Dolars", 1, 1, 0, 0, "Currency"),
    ]
    InventoryChar2 = [
        Items("Dollars", 1, 1, 0, 0, "Currency"),
    ]
That doesn't make any sense, as you set one value then discard it totally rewriting with another value..

Instead, there should be something like this:

Code: Select all

init python:
    class Items(object):
        #...

default InventoryChar1 = [
    Items("Dolars", 1, 1, 0, 0, "Currency"),
    ]

default InventoryChar2 = [
    Items("Dollars", 1, 1, 0, 0, "Currency"),
    ]

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

Re: Infinite money glitch when scrolling up

#3 Post by m_from_space »

jeffster wrote: Thu Apr 04, 2024 8:44 am I have no idea what happens if you define them like this:

Code: Select all

init python:
    InventoryChar1 = []
    InventoryChar2 = []
Renpy treats variables defined inside python blocks as constants. It doesn't care about them so to speak.

kedta35
Regular
Posts: 46
Joined: Wed Aug 30, 2023 1:31 pm
Contact:

Re: Infinite money glitch when scrolling up

#4 Post by kedta35 »

m_from_space wrote: Thu Apr 04, 2024 4:16 pm
jeffster wrote: Thu Apr 04, 2024 8:44 am I have no idea what happens if you define them like this:

Code: Select all

init python:
    InventoryChar1 = []
    InventoryChar2 = []
Renpy treats variables defined inside python blocks as constants. It doesn't care about them so to speak.
I just played around with it and thought I came up with a good way of getting around that issue using persistent data but I just realized that there is another issue, the python data is persistent through different saves.

Like, if one character has 100 dollars, then you start a completely new game, that character will still have 100 dollars in the new game. I tried adding a function to reset all items/money to 0 at the beginning of the start label but when I did that then went back to a different save where a character should have had 100 dollars, it was also reset.

Is there any way of fixing this or should python just not be used for items/inventory in Ren'Py?

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

Re: Infinite money glitch when scrolling up

#5 Post by jeffster »

kedta35 wrote: Thu Apr 04, 2024 7:04 pm Is there any way of fixing this or should python just not be used for items/inventory in Ren'Py?
Please read
https://renpy.org/doc/html/python.html# ... -statement
including Define Statement etc.

Post Reply

Who is online

Users browsing this forum: Bing [Bot]