[SOLVED] The value of persistent list changes if its value is assigned to a regular variable and changed it

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
NoFanru
Newbie
Posts: 14
Joined: Wed Jun 21, 2023 3:34 am
Projects: Yandere pjSEKAI boys (Yandere Rui)
Github: NoFanru
Discord: nofanru
Contact:

[SOLVED] The value of persistent list changes if its value is assigned to a regular variable and changed it

#1 Post by NoFanru »

I have a list as a persistent variable and I need to assign its value to a regular variable and change the value of the regular variable using Python methods for lists. However, changing the value of a regular variable also changes the value of persistent. Here's a simple example of what's happening:

Code: Select all

    $ persistent.kk = [0, 1, 2]

    "[persistent.kk]" # [0, 1, 2]

    $ n = persistent.kk
    $ n.append(0)

    "[persistent.kk]" # [0, 1, 2, 0] ????
So, is it possible to change a regular variable without changing the persistent variable?
I would be grateful for any help
Last edited by NoFanru on Sat Nov 25, 2023 6:37 am, edited 1 time in total.

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2400
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: The value of persistent list changes if its value is assigned to a regular variable and changed it using methods of

#2 Post by Ocelot »

Yes, you just need to make sure that regular variable points to a copy and not the same list your persistent variable points to.
Choose your approach:

Code: Select all

$ n = persistent.kk.copy()
$ n = persistent.kk[:]
$ n = list(persistent.kk)
< < insert Rick Cook quote here > >

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

Re: The value of persistent list changes if its value is assigned to a regular variable and changed it using methods of

#3 Post by m_from_space »

NoFanru wrote: Sat Nov 25, 2023 4:02 am So, is it possible to change a regular variable without changing the persistent variable?
I would be grateful for any help
This has nothing to do with persistent or regular variables. It's just a Python thing. If you want to create a copy of a list (or dictionary etc.) you have to do it in a way @Ocelot describes. Otherwise in Python you would just create a reference. It's how Python works, not Renpy.

NoFanru
Newbie
Posts: 14
Joined: Wed Jun 21, 2023 3:34 am
Projects: Yandere pjSEKAI boys (Yandere Rui)
Github: NoFanru
Discord: nofanru
Contact:

Re: The value of persistent list changes if its value is assigned to a regular variable and changed it using methods of

#4 Post by NoFanru »

Ocelot wrote: Sat Nov 25, 2023 4:59 am Yes, you just need to make sure that regular variable points to a copy and not the same list your persistent variable points to.
Choose your approach:

Code: Select all

$ n = persistent.kk.copy()
$ n = persistent.kk[:]
$ n = list(persistent.kk)
Ohh, thank you very much for your help, and I'm sorry that my question was so stupid, haha :<

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot]