[Solved]Multiple variables in a single variable

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
bonnie_641
Regular
Posts: 124
Joined: Sat Jan 13, 2018 10:57 pm
Projects: Código C.O.C.I.N.A. (pending); Aprende Chino conmigo (active)
Deviantart: rubymoonlily
Contact:

[Solved]Multiple variables in a single variable

#1 Post by bonnie_641 » Mon Aug 30, 2021 10:15 pm

Edit:
I was able to understand about the use of variables. I clarify that what was solved was an issue that was left pending from the previous topic I wrote: viewtopic.php?f=8&t=62938&p=546109#p545669
But the use of several variables in a variable is still an unknown (especially because of the use of boleans, which in theory may be easy, in practice it gets very complicated).
Hello everyone.
I have searched all over the forum and can't find an example about using multiple variables in one variable.

For example:
If I have a general (boolean) variable:

Code: Select all

$ cant_move = False
I have several extra variables.
If I want to move one, it would be:

Code: Select all

$ can_move_one_whatever = True
# the second one,
$ can_move_two_whatever = True
# etc.
It is easy to stop all variables if $ cant_move is true (since it is the main variable),
but, if I want to restrict one by one (and not all at once and without resorting directly to cant_move) the logical thing is that each variable (can_move_one_whatever or any other is changed to False).

The problem is the following: when there is a restriction of use of variables (as for example in the use of "draggable" that can be True, False or another variable is there the question. If I place the main in this way:

Code: Select all

drag:
    # (rest of code)
    draggable cant_move
I can change from True to False or vice versa anywhere in the code. Most of the examples I found use drag groups and drops, which simplify the overall process, but not when you want to apply an individual change to each element.
I thank you in advance for your answer and I hope I have explained my doubt correctly.
Last edited by bonnie_641 on Wed Sep 01, 2021 10:11 pm, edited 2 times in total.
I speak and write in Spanish. I use an English-Spanish translator to express myself in this forum. If I make any mistakes, please forgive me.
I try my best to give an answer according to your question. :wink:

User avatar
hell_oh_world
Miko-Class Veteran
Posts: 777
Joined: Fri Jul 12, 2019 5:21 am
Projects: The Button Man
Organization: NILA
Github: hell-oh-world
Location: Philippines
Contact:

Re: Multiple variables in a single variable

#2 Post by hell_oh_world » Mon Aug 30, 2021 10:28 pm

try using a list, and a single variable to control all...

Code: Select all

default ucan_move = True
default can_move = [i for i in range(10)] # how many do you need? this one populates 10 booleans

Code: Select all

drag:
  draggable (ucan_move and can_move[x]) # x should be the index of the boolean starting from 0.

User avatar
bonnie_641
Regular
Posts: 124
Joined: Sat Jan 13, 2018 10:57 pm
Projects: Código C.O.C.I.N.A. (pending); Aprende Chino conmigo (active)
Deviantart: rubymoonlily
Contact:

Re: Multiple variables in a single variable

#3 Post by bonnie_641 » Mon Aug 30, 2021 11:12 pm

Thank you very much for replying :D
hell_oh_world wrote:
Mon Aug 30, 2021 10:28 pm
try using a list, and a single variable to control all...

Code: Select all

default ucan_move = True
default can_move = [i for i in range(10)] # how many do you need? this one populates 10 booleans
This is my short list.

Code: Select all

$ items_list = [
        {"name":"uno", "child":"images/d.png", "x_pos":197, "y_pos":202},
        {"name":"dos", "child":"images/d.png", "x_pos":197, "y_pos":202},
        {"name":"tres", "child":"images/d.png", "x_pos":197, "y_pos":202},
        ]
So, my variables to apply the boolean set would be (correct me if I'm wrong please):

Code: Select all

default ucan_move = True
default can_move = [{"can_move1": False, "can_move2": False, "can_move3": False}] # ??? or it is applied directly [i for i in range(10)] ??
hell_oh_world wrote:
Mon Aug 30, 2021 10:28 pm

Code: Select all

drag:
  draggable (ucan_move and can_move[x]) # x should be the index of the boolean starting from 0.
Does it work for a non-numeric index?

My drag group:

Code: Select all

draggroup:
        
        #  slots from their description.
        for each_slot in slots_list:
            drag:
                drag_name each_slot["name"]
                draggable False
                child each_slot["child"]
                xpos each_slot["x_pos"] ypos each_slot["y_pos"]
            
        
        # items from their descriptions (these elements can be moved depending on draggable)
        for each_item in items_list:
            drag:
                drag_name each_item["name"]
                child each_item["child"]
                droppable True
                draggable can_move # <--------------------- (What do I add here? O... Do I put X somewhere else?)
                dragged my_dragged
                xpos each_item["x_pos"] ypos each_item["y_pos"]
                
                #... other code
  
  # At the beginning?     in this way?  
$ items_list = [
        {"name":"uno", "child":"images/d.png", "x_pos":197, "y_pos":202, "can_move1": True}, #starts as a true
        {"name":"dos", "child":"images/d.png", "x_pos":197, "y_pos":202, "can_move2": True},
        {"name":"tres", "child":"images/d.png", "x_pos":197, "y_pos":202, "can_move3": True},
        ]
I tried this option:

Code: Select all

default can_move = [{"can_move1": False, "can_move2": False, "can_move3": False}]
But I don't know what I'm doing wrong.
I speak and write in Spanish. I use an English-Spanish translator to express myself in this forum. If I make any mistakes, please forgive me.
I try my best to give an answer according to your question. :wink:

User avatar
hell_oh_world
Miko-Class Veteran
Posts: 777
Joined: Fri Jul 12, 2019 5:21 am
Projects: The Button Man
Organization: NILA
Github: hell-oh-world
Location: Philippines
Contact:

Re: Multiple variables in a single variable

#4 Post by hell_oh_world » Mon Aug 30, 2021 11:33 pm

You almost got it right...

Code: Select all

default ucan_move = True
default items_list = [
        {"name":"uno", "child":"images/d.png", "x_pos":197, "y_pos":202, "can_move": True}, #starts as a true
        {"name":"dos", "child":"images/d.png", "x_pos":197, "y_pos":202, "can_move": True},
        {"name":"tres", "child":"images/d.png", "x_pos":197, "y_pos":202, "can_move": True},
]
Then in your loop...

Code: Select all

for each_item in items_list:
            drag:
                drag_name each_item["name"]
                child each_item["child"]
                droppable True
                draggable (ucan_move and each_item["can_move"])

User avatar
bonnie_641
Regular
Posts: 124
Joined: Sat Jan 13, 2018 10:57 pm
Projects: Código C.O.C.I.N.A. (pending); Aprende Chino conmigo (active)
Deviantart: rubymoonlily
Contact:

Re: Multiple variables in a single variable

#5 Post by bonnie_641 » Tue Aug 31, 2021 6:44 am

hell_oh_world wrote:
Mon Aug 30, 2021 11:33 pm
You almost got it right...

Code: Select all

default ucan_move = True
default items_list = [
        {"name":"uno", "child":"images/d.png", "x_pos":197, "y_pos":202, "can_move": True}, #starts as a true
        {"name":"dos", "child":"images/d.png", "x_pos":197, "y_pos":202, "can_move": True},
        {"name":"tres", "child":"images/d.png", "x_pos":197, "y_pos":202, "can_move": True},
]
Then in your loop...

Code: Select all

for each_item in items_list:
            drag:
                drag_name each_item["name"]
                child each_item["child"]
                droppable True
                draggable (ucan_move and each_item["can_move"])
Thank you very much :D

Case 1: the variable "ucan_move" is general.
Locks all objects when false
It can be applied with "len"

Code: Select all

# I defined open_cards_list as an empty list at start
default opened_cards_list = [] 
# (rest of code)

if len(opened_cards_list) == 3:
            $ ucan_move = False #works for all, but not individually
            "Blocked cards yay~"
Case 2: This is where the problem begins :oops:
When putting the restriction on each card, it does not work at all (ucan_move = False and can_move = False)

Code: Select all

label start:
    while True:

        $ interaccion = True
        call screen shop_screen

        if _return == "uno":
            $ renpy.pause(0.001, hard=True) 
            $ interaccion = False
            $ can_move = False #<------------- nothing happens
            $ ucan_move = False # <----------- blocks everything
            
            show screen shop_screen
        
            "with 'ucan_move= False' blocks all cards :C"
I speak and write in Spanish. I use an English-Spanish translator to express myself in this forum. If I make any mistakes, please forgive me.
I try my best to give an answer according to your question. :wink:

User avatar
hell_oh_world
Miko-Class Veteran
Posts: 777
Joined: Fri Jul 12, 2019 5:21 am
Projects: The Button Man
Organization: NILA
Github: hell-oh-world
Location: Philippines
Contact:

Re: Multiple variables in a single variable

#6 Post by hell_oh_world » Tue Aug 31, 2021 12:17 pm

Locks all objects when false
That's supposed to happen. You're using the and logical operator here, meaning the condition will only evaluate to True if both left and right conditions are also True. In other words if either ucan_move or item[can_move] is False then it will always be False.
If you will use the or operator here, you will be able to drag if either of the left and right is True.

User avatar
bonnie_641
Regular
Posts: 124
Joined: Sat Jan 13, 2018 10:57 pm
Projects: Código C.O.C.I.N.A. (pending); Aprende Chino conmigo (active)
Deviantart: rubymoonlily
Contact:

Re: Multiple variables in a single variable

#7 Post by bonnie_641 » Tue Aug 31, 2021 8:38 pm

hell_oh_world wrote:
Tue Aug 31, 2021 12:17 pm
Locks all objects when false
That's supposed to happen. You're using the and logical operator here, meaning the condition will only evaluate to True if both left and right conditions are also True. In other words if either ucan_move or item[can_move] is False then it will always be False.
If you will use the or operator here, you will be able to drag if either of the left and right is True.
:shock:
That means I should rephrase the question. It is not possible to do what I want because the expressions will be false.
Thank you very much. You are very kind to share your knowledge with me.
For now, I will leave this issue pending until I can rethink the problem from another perspective.
That doesn't mean I'm giving up. I will just try to find another way to solve the initial problem.
I speak and write in Spanish. I use an English-Spanish translator to express myself in this forum. If I make any mistakes, please forgive me.
I try my best to give an answer according to your question. :wink:

User avatar
Alex
Lemma-Class Veteran
Posts: 2981
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Multiple variables in a single variable

#8 Post by Alex » Wed Sep 01, 2021 2:21 pm

bonnie_641 wrote:
Tue Aug 31, 2021 6:44 am
...

Code: Select all

label start:
    while True:

        $ interaccion = True
        call screen shop_screen

        if _return == "uno":
            $ renpy.pause(0.001, hard=True) 
            $ interaccion = False
            $ can_move = False #<------------- nothing happens
            $ ucan_move = False # <----------- blocks everything
            
            show screen shop_screen
        
            "with 'ucan_move= False' blocks all cards :C"
That doesn't work 'cause every card has its own 'can_move' variable. So, you can change them either in label

Code: Select all

        if _return == "uno":
            $ renpy.pause(0.001, hard=True) 
            $ interaccion = False
            $ items_list[0]['can_move'] = False # uno-card is first in a list, so its index is 0
            $ ucan_move = False # <----------- blocks everything
or in my_dragged function

Code: Select all

        # Let's place dragged item over item it was dropped onto.
        items_list[ind]["child"]["current"] = items_list[ind]["child"]["face"] # <--- change card's image
        store.opened_cards_list.append(drags[0].drag_name) # <--- add the name of the card to a list
        drags[0].snap(drop.x, drop.y, 0.1)

        items_list[ind]["can_move"] = False # <---

User avatar
bonnie_641
Regular
Posts: 124
Joined: Sat Jan 13, 2018 10:57 pm
Projects: Código C.O.C.I.N.A. (pending); Aprende Chino conmigo (active)
Deviantart: rubymoonlily
Contact:

Re: Multiple variables in a single variable

#9 Post by bonnie_641 » Wed Sep 01, 2021 10:05 pm

:D :D :D
Thank you very much Alex.

Code: Select all

$ items_list[0]['can_move'] = False # uno-card is first in a list, so its index is 0
I was very surprised by this part of the code. I didn't know it was possible to do that.
Alex wrote:
Wed Sep 01, 2021 2:21 pm
(...)
or in my_dragged function

Code: Select all

        # Let's place dragged item over item it was dropped onto.
        items_list[ind]["child"]["current"] = items_list[ind]["child"]["face"] # <--- change card's image
        store.opened_cards_list.append(drags[0].drag_name) # <--- add the name of the card to a list*****new
        drags[0].snap(drop.x, drop.y, 0.1)

        items_list[ind]["can_move"] = False # <--- *****new
The my_dragged function worked perfectly.

I am very happy because thanks to Alex I was able to solve my big problem about the drag and drop system. You are my hero <3

And, I also thank hell-oh-world for helping me with the variables problem (I learned a lot).
I speak and write in Spanish. I use an English-Spanish translator to express myself in this forum. If I make any mistakes, please forgive me.
I try my best to give an answer according to your question. :wink:

Post Reply

Who is online

Users browsing this forum: Bing [Bot]