Setting list variable with screen language

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
Mild Curry
Regular
Posts: 107
Joined: Fri Jul 02, 2010 3:03 pm
Projects: That's The Way The Cookie Crumbles
Organization: TwinTurtle Games
Contact:

Setting list variable with screen language

#1 Post by Mild Curry »

I feel like I've been asking a lot of questions lately, but I'm trying to figure out screen language, so I have to say I'm really grateful for all the people who take time to help out. :D

So. I have a list of nine variables, all set to 'True'. I also have nine imagebuttons. When one of these imagebuttons is pressed, I'd like it to change the corresponding variable in the list to 'False.'
Logically, it seems like the best action to use would be 'ToggleField', but when I try this:

Code: Select all

 for i in range(9):
      if selected[i]:    
        imagebutton idle "backsm.png" hover "backsm2.png" action [ToggleField(selected, "selected[i]"), SetVariable("cards_picked", (cards_picked+1))] xalign xx ypos 0.3
      else:
       add "backsm.png" xalign xx ypos 0.4
I get this error:

Code: Select all

AttributeError: 'RevertableList' object has no attribute 'selected[i]'

Help! Should I be using a different action? Is my syntax wrong? Am I going about this entirely in the wrong way?

Anima
Veteran
Posts: 448
Joined: Wed Nov 18, 2009 11:17 am
Completed: Loren
Projects: PS2
Location: Germany
Contact:

Re: Setting list variable with screen language

#2 Post by Anima »

Yes you are using the wrong action. A field is a variable that is part of an object. You need ToggleScreenVariable instead.
Avatar created with this deviation by Crysa
Currently working on:
  • Winterwolves "Planet Stronghold 2" - RPG Framework: Phase III

Mild Curry
Regular
Posts: 107
Joined: Fri Jul 02, 2010 3:03 pm
Projects: That's The Way The Cookie Crumbles
Organization: TwinTurtle Games
Contact:

Re: Setting list variable with screen language

#3 Post by Mild Curry »

Okay. Changing it to this:

Code: Select all

ToggleScreenVariable("selected[i]")
Gave me this:

Code: Select all

KeyError: 'selected[i]'
T-T

Anima
Veteran
Posts: 448
Joined: Wed Nov 18, 2009 11:17 am
Completed: Loren
Projects: PS2
Location: Germany
Contact:

Re: Setting list variable with screen language

#4 Post by Anima »

Is selected a variable inside the screen or a global variable defined in your script?
Avatar created with this deviation by Crysa
Currently working on:
  • Winterwolves "Planet Stronghold 2" - RPG Framework: Phase III

Mild Curry
Regular
Posts: 107
Joined: Fri Jul 02, 2010 3:03 pm
Projects: That's The Way The Cookie Crumbles
Organization: TwinTurtle Games
Contact:

Re: Setting list variable with screen language

#5 Post by Mild Curry »

I think I've tried it as both!

Like this:

Code: Select all

label start:
  $ selected = [True, True, True, True, True, True, True, True, True]
  call screen pick5

screen pick5:
    for i in range(9):  
      if selected[i]:    
        imagebutton idle "backsm.png" hover "backsm2.png" action [ToggleScreenVariable("selected[i]"), SetVariable("cards_picked", (cards_picked+1))] xalign xx ypos 0.3
      else:
       add "backsm.png" xalign xx ypos 0.4   
And like this:

Code: Select all

screen pick5:
    $ selected = [True, True, True, True, True, True, True, True, True] 
    for i in range(9):  
      if selected[i]:    
        imagebutton idle "backsm.png" hover "backsm2.png" action [ToggleScreenVariable("selected[i]"), SetVariable("cards_picked", (cards_picked+1))] xalign xx ypos 0.3
      else:
       add "backsm.png" xalign xx ypos 0.4   
if that's what you mean. Both return the same error.

User avatar
SleepKirby
Veteran
Posts: 255
Joined: Mon Aug 09, 2010 10:02 pm
Projects: Eastern Starlight Romance, Touhou Mecha
Organization: Dai-Sukima Dan
Location: California, USA
Contact:

Re: Setting list variable with screen language

#6 Post by SleepKirby »

I don't think you can use ToggleScreenVariable like this:

Code: Select all

ToggleScreenVariable("selected[i]")
This makes Ren'Py literally search for a screen variable named "selected". But selected isn't a variable name; the variable name is selected, and is a list index.

To do an array access, you'd need something like ToggleDict... but hmm, that doesn't seem to access a screen variable.

How about a new action like this?

Code: Select all

init python:
    def ToggleScreenDict(dict_name, key, true_value=None, false_value=None):
    
        cs = renpy.current_screen()

        if cs is not None:
            return ToggleDict(cs.scope[dict_name], key, true_value=true_value, false_value=None)
        else:
            return None
I didn't test this, but I'm basing this off of the code for ToggleScreenVariable and ToggleDict. (These are in common/00screen.rpy.)
You would use it like this:

Code: Select all

ToggleScreenVariable("selected", i)
Note how selected is in quotes, while i isn't.

Mild Curry
Regular
Posts: 107
Joined: Fri Jul 02, 2010 3:03 pm
Projects: That's The Way The Cookie Crumbles
Organization: TwinTurtle Games
Contact:

Re: Setting list variable with screen language

#7 Post by Mild Curry »

Okay. After looking up some stuff, if I'm understanding this correctly, a screen variable is defined inside the screen, and I'd need something with "ToggleScreen" to change it, and a global variable is defined outside the screen, and I'd need an action with just "Toggle" to change it. (assuming these are all boolean, of course)

But how does making a variable one or the other affect the behavior of the program? Does it make a difference where you define it?

Also, are lists synonymous with dictionaries? I can't seem to get "ToggleDict" to work with a list.

(Sorry if these are stupid/irrelevant questions, I'm a novice to both Renpy and programming.)

0ion9
Regular
Posts: 79
Joined: Thu Jun 16, 2011 9:17 am
Contact:

Re: Setting list variable with screen language

#8 Post by 0ion9 »

Mild Curry wrote: Also, are lists synonymous with dictionaries? I can't seem to get "ToggleDict" to work with a list.
No * 9^9(9).
See http://diveintopython.org/getting_to_kn ... aries.html and http://diveintopython.org/native_data_types/lists.html
. Alternatively, http://en.wikipedia.org/wiki/Associative_array and http://en.wikipedia.org/wiki/List_(computer_science) give a more language-agnostic view of the concept of each.

There is some overlap in how they can be used, but generally, passing a list to something which is expecting a dict or vice versa ... is asking for trouble.

Anima
Veteran
Posts: 448
Joined: Wed Nov 18, 2009 11:17 am
Completed: Loren
Projects: PS2
Location: Germany
Contact:

Re: Setting list variable with screen language

#9 Post by Anima »

Code: Select all

screen pick5:
    for entry in selected: 
      if entry:   
        imagebutton idle "backsm.png" hover "backsm2.png" action [ToggleScreenVariable("entry"), SetVariable("cards_picked", (cards_picked+1))] xalign xx ypos 0.3
      else:
       add "backsm.png" xalign xx ypos 0.4   
That would be the normal syntax to iterate trough a list. I hope entry is recognised as a normal screen variable. Sorry, I somehow missed that you used manual iteration.
Avatar created with this deviation by Crysa
Currently working on:
  • Winterwolves "Planet Stronghold 2" - RPG Framework: Phase III

User avatar
SleepKirby
Veteran
Posts: 255
Joined: Mon Aug 09, 2010 10:02 pm
Projects: Eastern Starlight Romance, Touhou Mecha
Organization: Dai-Sukima Dan
Location: California, USA
Contact:

Re: Setting list variable with screen language

#10 Post by SleepKirby »

Yeah, lists aren't the same as dicts at all, but I looked at ToggleDict's code and I thought either one could fit in there. Oh well.

Mild Curry
Regular
Posts: 107
Joined: Fri Jul 02, 2010 3:03 pm
Projects: That's The Way The Cookie Crumbles
Organization: TwinTurtle Games
Contact:

Re: Setting list variable with screen language

#11 Post by Mild Curry »

Anima- It doesn't recognize 'entry', so far as I can tell.

But the problem's solved anyway. I ended up constructing a little workaround that will suit my purposes. Not the cleanest thing ever, but at least it works.

Thanks for all the help, guys. I'm learning things.

Post Reply

Who is online

Users browsing this forum: No registered users