[SOLVED] Why can't I change a variable in a list?

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
span4ev
Regular
Posts: 77
Joined: Fri Jul 08, 2022 9:29 pm
itch: rpymc
Contact:

[SOLVED] Why can't I change a variable in a list?

#1 Post by span4ev » Sun Sep 25, 2022 12:53 am

Here I have a list:

Code: Select all

init:
    $ nums = ['one', 'two', 'three']
    $ num = 'one'
The list is a mutable object. For python it would be perfectly fine to do something like this:

nums[0] = 'something'
nums[-1] = 'something'

But it doesn't work for SetVariable

Here it works:

Code: Select all

action SetVariable('num', 'something')
This is no longer working:

Code: Select all

action SetVariable('nums[0]', 'something')

Code: Select all

screen nansns:
    frame: 
        vbox:
            text num  # one
            text nums # one two three
            textbutton 'Change':
                action SetVariable('num', 'something') # it works

                action SetVariable('nums[0]', 'something') # The variable nums[0] does not exist 
Last edited by span4ev on Sat Oct 15, 2022 4:57 am, edited 1 time in total.

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

Re: Why can't I change a variable in a list?

#2 Post by Ocelot » Sun Sep 25, 2022 2:36 am

This is to be expected: there is no variable nums[0]. There is variable nums which can be indexed to acccess another object, but does not make nums[0] a valid variable name. Even in Python anything accessing variables by name will break on trying to access object at list index.

Just use Function with custom Python function to access object you want.
< < insert Rick Cook quote here > >

User avatar
enaielei
Regular
Posts: 114
Joined: Fri Sep 17, 2021 2:09 am
Tumblr: enaielei
Deviantart: enaielei
Github: enaielei
Skype: enaielei
Soundcloud: enaielei
itch: enaielei
Discord: enaielei#7487
Contact:

Re: Why can't I change a variable in a list?

#3 Post by enaielei » Sun Sep 25, 2022 5:47 am

If you want to set an item in a dict/list use SetDict.

Code: Select all

action SetDict("nums", 0, "something")

span4ev
Regular
Posts: 77
Joined: Fri Jul 08, 2022 9:29 pm
itch: rpymc
Contact:

Re: Why can't I change a variable in a list?

#4 Post by span4ev » Sun Sep 25, 2022 12:37 pm

Ocelot wrote:
Sun Sep 25, 2022 2:36 am
This is to be expected: there is no variable nums[0]. There is variable nums which can be indexed to acccess another object, but does not make nums[0] a valid variable name. Even in Python anything accessing variables by name will break on trying to access object at list index.

Just use Function with custom Python function to access object you want.
Yes you are right. SetVariable is a change to a variable, not one of the collection's values...

span4ev
Regular
Posts: 77
Joined: Fri Jul 08, 2022 9:29 pm
itch: rpymc
Contact:

Re: Why can't I change a variable in a list?

#5 Post by span4ev » Sun Sep 25, 2022 12:37 pm

enaielei wrote:
Sun Sep 25, 2022 5:47 am
If you want to set an item in a dict/list use SetDict.

Code: Select all

action SetDict("nums", 0, "something")
Thank You!
Only the variable name must be without quotes

Code: Select all

action SetDict(nums, 0, "something")
Thank You!

Post Reply

Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot]