Set Variable To Something Complicated

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
TheChatotMaestro
Regular
Posts: 59
Joined: Mon Jul 31, 2017 8:33 am
Deviantart: LedianWithACamera
Contact:

Set Variable To Something Complicated

#1 Post by TheChatotMaestro » Sun Sep 24, 2017 3:10 pm

What I want to do is set a variable to something randomly picked from a list. Say- you have a list of 5 random fruits, and it has a random number generator, and then it thinks 'oh, I picked 1, and 1 means apple'. I know how to do this... but I want to be able to do it without copy-pasting the list each time I use it. Is there a way to just have a line of code that simply generates the number and refers to a list somewhere else?

Remix
Veteran
Posts: 244
Joined: Tue May 30, 2017 6:10 am
Contact:

Re: Set Variable To Something Complicated

#2 Post by Remix » Sun Sep 24, 2017 3:27 pm

Not entirely sure what you mean. Surely one of the renpy random functions would do what you want though, even if you just pick a float and then times it by the list length then round it.

TheChatotMaestro
Regular
Posts: 59
Joined: Mon Jul 31, 2017 8:33 am
Deviantart: LedianWithACamera
Contact:

Re: Set Variable To Something Complicated

#3 Post by TheChatotMaestro » Sun Sep 24, 2017 3:36 pm

To be more specific: what I want is renpy.random.choice, but I don't have to copy and paste the whole list every time, because I'd probably be using very large lists and it could get incredibly difficult to work with. Any way to do this, or do I have to just use renpy.random.choice and deal with it?

Remix
Veteran
Posts: 244
Joined: Tue May 30, 2017 6:10 am
Contact:

Re: Set Variable To Something Complicated

#4 Post by Remix » Sun Sep 24, 2017 4:15 pm

umm
renpy.random.choice( python_variable_name_of_list )

You do not have to write the list each time, just assign it to variable and pass that

TheChatotMaestro
Regular
Posts: 59
Joined: Mon Jul 31, 2017 8:33 am
Deviantart: LedianWithACamera
Contact:

Re: Set Variable To Something Complicated

#5 Post by TheChatotMaestro » Sun Sep 24, 2017 4:48 pm

Thank you! I'll try that :)

TheChatotMaestro
Regular
Posts: 59
Joined: Mon Jul 31, 2017 8:33 am
Deviantart: LedianWithACamera
Contact:

Re: Set Variable To Something Complicated

#6 Post by TheChatotMaestro » Sun Sep 24, 2017 5:01 pm

To do that, I just set the list items like this "$ fruits = ["orange", "apple", "pear"]" right?

Remix
Veteran
Posts: 244
Joined: Tue May 30, 2017 6:10 am
Contact:

Re: Set Variable To Something Complicated

#7 Post by Remix » Sun Sep 24, 2017 5:09 pm

yup, then

$ result = renpy.random.choice( fruits )
"[result]"

User avatar
xavimat
Eileen-Class Veteran
Posts: 1049
Joined: Sat Feb 25, 2012 8:45 pm
Completed: Yeshua, Jesus Life, Cops&Robbers
Projects: Fear&Love, unknown
Organization: Pilgrim Creations
Github: xavi-mat
Location: Italy
Contact:

Re: Set Variable To Something Complicated

#8 Post by xavimat » Fri Oct 06, 2017 6:26 pm

If you are feeling lazy... 8)

Code: Select all

init python:
    def f():
        return renpy.random.choice(["orange", "apple", "pear"])

label whatever:
    $ result = f()
    "[result]"
Or, a different laziness,

Code: Select all

default fruits = ["orange", "apple", "pear"]
init python:
    r = renpy.random.choice
    
label whatever:
    $ result = r(fruits)
    "[result]"
Cops&Robbers A two-player experiment
Fear&Love Why can’t we simply express our feelings? Why am I afraid to say ‘I love you’?
unknown [Sci-Fi - mystery - computer]
Honest Critique (Avatar made with Chibi Maker by ~gen8)

TheChatotMaestro
Regular
Posts: 59
Joined: Mon Jul 31, 2017 8:33 am
Deviantart: LedianWithACamera
Contact:

Re: Set Variable To Something Complicated

#9 Post by TheChatotMaestro » Sat Oct 14, 2017 4:23 pm

xavimat wrote:
Fri Oct 06, 2017 6:26 pm
If you are feeling lazy... 8)

Code: Select all

init python:
    def f():
        return renpy.random.choice(["orange", "apple", "pear"])

label whatever:
    $ result = f()
    "[result]"
That! That is exactly what I wanted. Functions. Thank you so much.

TheChatotMaestro
Regular
Posts: 59
Joined: Mon Jul 31, 2017 8:33 am
Deviantart: LedianWithACamera
Contact:

Re: Set Variable To Something Complicated

#10 Post by TheChatotMaestro » Sun Oct 15, 2017 11:36 am

Code: Select all

init python:
    def rarity():
        return renpy.random.randint (1, 20)
    def rareroll():
        renpy.random.choice(["Taree R", "Waki R", "Mikashi R", "Tamaki R", "Sachiko R", "Sakae R", "Emiya R", "Kanaya R", "Wakuri R", "Ruru R", "Orime R", "Kunie R"])
    def superrareroll():
        renpy.random.choice(["We Are Very Happy Girls' Waki SR", "'At A Crossroads' Tamaki SR", "'Ring My Bell' Wakuri SR", "'What A Night Owl' Sachiko SR", "'Day By Day' Emiya SR", "'Song For A Sunny Day' Orime SR", "'Piece Of Cake' Ruru SR'", "'Rule Of Thumb' Kunie SR"])
    def supersuperrareroll():
        renpy.random.choice(["'Step By Step' Sakae SSR'," "'Biker Jacket Chic' Kanaya SSR"])
    def ultrarareroll():
    renpy.random.choice(["'Smiling, Smiling!' Taree UR", "'The First Live Show' Mikashi UR"])
    
    def soloyolo():
        if coins >= 5:
                $ coins -= 5
                $ cardrarity = rarity()
                if (cardrarity <= 10):
                    $ solocard = rareroll()
                elif cardrarity > 10 and cardrarity <= 15:
                    $ solocard = superrareroll()
                elif cardrarity > 15 and cardrarity <= 19:
                    $ solocard = supersuperrareroll()
                else cardrarity == 20:
                    $ solocard = ultrarareroll()
                $ cards.append("[solocard]")
                
    def tenplusone():
        if coins >= 50:
                $ coins -= 50
                $ cardrarity = rarity()
                if (cardrarity <= 10):
                    $ cardone = rareroll()
                elif cardrarity > 10 and cardrarity <= 15:
                    $ cardone = superrareroll()
                elif cardrarity > 15 and cardrarity <= 19:
                    $ cardone = supersuperrareroll()
                else cardrarity == 20:
                    $ cardone = ultrarareroll()
                $ cards.append("[cardone]")
                
                $ cardrarity = rarity()
                if (cardrarity <= 10):
                    $ cardtwo = rareroll()
                elif cardrarity > 10 and cardrarity <= 15:
                    $ cardtwo = superrareroll()
                elif cardrarity > 15 and cardrarity <= 19:
                    $ cardtwo = supersuperrareroll()
                else cardrarity == 20:
                    $ cardtwo = ultrarareroll()
                $ cards.append("[cardtwo]")
                
                $ cardrarity = rarity()
                if (cardrarity <= 10):
                    $ cardthree = rareroll()
                elif cardrarity > 10 and cardrarity <= 15:
                    $ cardthree = superrareroll()
                elif cardrarity > 15 and cardrarity <= 19:
                    $ cardthree = supersuperrareroll()
                else cardrarity == 20:
                    $ cardthree = ultrarareroll()
                $ cards.append("[cardthree]")
                
                $ cardrarity = rarity()
                if (cardrarity <= 10):
                    $ cardfour = rareroll()
                elif cardrarity > 10 and cardrarity <= 15:
                    $ cardfour = superrareroll()
                elif cardrarity > 15 and cardrarity <= 19:
                    $ cardfour = supersuperrareroll()
                else cardrarity == 20:
                    $ cardfour = ultrarareroll()
                $ cards.append("[cardfour]")
                
                $ cardrarity = rarity()
                if (cardrarity <= 10):
                    $ cardfive = rareroll()
                elif cardrarity > 10 and cardrarity <= 15:
                    $ cardfive = superrareroll()
                elif cardrarity > 15 and cardrarity <= 19:
                    $ cardfive = supersuperrareroll()
                else cardrarity == 20:
                    $ cardfive = ultrarareroll()
                $ cards.append("[cardfive]")
                
                $ cardrarity = rarity()
                if (cardrarity <= 10):
                    $ cardsix = rareroll()
                elif cardrarity > 10 and cardrarity <= 15:
                    $ cardsix = superrareroll()
                elif cardrarity > 15 and cardrarity <= 19:
                    $ cardsix = supersuperrareroll()
                else cardrarity == 20:
                    $ cardsix = ultrarareroll()
                $ cards.append("[cardsix]")
                
                $ cardrarity = rarity()
                if (cardrarity <= 10):
                    $ cardseven = rareroll()
                elif cardrarity > 10 and cardrarity <= 15:
                    $ cardseven = superrareroll()
                elif cardrarity > 15 and cardrarity <= 19:
                    $ cardseven = supersuperrareroll()
                else cardrarity == 20:
                    $ cardseven = ultrarareroll()
                $ cards.append("[cardseven]")
                
                $ cardrarity = rarity()
                if (cardrarity <= 10):
                    $ cardeight = rareroll()
                elif cardrarity > 10 and cardrarity <= 15:
                    $ cardeight = superrareroll()
                elif cardrarity > 15 and cardrarity <= 19:
                    $ cardeight = supersuperrareroll()
                else cardrarity == 20:
                    $ cardeight = ultrarareroll()
                $ cards.append("[cardeight]")
                
                $ cardrarity = rarity()
                if (cardrarity <= 10):
                    $ cardnine = rareroll()
                elif cardrarity > 10 and cardrarity <= 15:
                    $ cardnine = superrareroll()
                elif cardrarity > 15 and cardrarity <= 19:
                    $ cardnine = supersuperrareroll()
                else cardrarity == 20:
                    $ cardnine = ultrarareroll()
                $ cards.append("[cardnine]")
                
                $ cardrarity = rarity()
                if (cardrarity <= 10):
                    $ cardten = rareroll()
                elif cardrarity > 10 and cardrarity <= 15:
                    $ cardten = superrareroll()
                elif cardrarity > 15 and cardrarity <= 19:
                    $ cardten = supersuperrareroll()
                else cardrarity == 20:
                    $ cardten = ultrarareroll()
                $ cards.append("[cardten]")
                
                $ cardrarity = rarity()
                if (cardrarity <= 10):
                    $ cardeleven = rareroll()
                elif cardrarity > 10 and cardrarity <= 15:
                    $ cardeleven = superrareroll()
                elif cardrarity > 15 and cardrarity <= 19:
                    $ cardeleven = supersuperrareroll()
                else cardrarity == 20:
                    $ cardeleven = ultrarareroll()
                $ cards.append("[cardeleven]")
Does this work?

TheChatotMaestro
Regular
Posts: 59
Joined: Mon Jul 31, 2017 8:33 am
Deviantart: LedianWithACamera
Contact:

Re: Set Variable To Something Complicated

#11 Post by TheChatotMaestro » Sun Oct 15, 2017 11:50 am

When trying to call the functions, two things happen. First, it says I can't do just "tenplusone()" in the script, it's 'expecting a statement'. What do I put to make it do what it's supposed to? Second, it won't let me mess with variables, anything beginning with "$ =" gets an "invalid syntax" response.

nananame
Newbie
Posts: 6
Joined: Fri Oct 13, 2017 1:40 pm
Contact:

Re: Set Variable To Something Complicated

#12 Post by nananame » Mon Oct 16, 2017 6:14 am

TheChatotMaestro wrote:
Sun Oct 15, 2017 11:50 am
it won't let me mess with variables, anything beginning with "$ =" gets an "invalid syntax" response.
You are already in a python block, that's why the $ reads as invalid. Remove it.
You also seem to have an indentation error on the ultrarare...

User avatar
xavimat
Eileen-Class Veteran
Posts: 1049
Joined: Sat Feb 25, 2012 8:45 pm
Completed: Yeshua, Jesus Life, Cops&Robbers
Projects: Fear&Love, unknown
Organization: Pilgrim Creations
Github: xavi-mat
Location: Italy
Contact:

Re: Set Variable To Something Complicated

#13 Post by xavimat » Mon Oct 16, 2017 1:22 pm

Also, you dropped the "return" in the definition of functions, see my example. The function must "return" something, in your case, the chosen element of the list.
Cops&Robbers A two-player experiment
Fear&Love Why can’t we simply express our feelings? Why am I afraid to say ‘I love you’?
unknown [Sci-Fi - mystery - computer]
Honest Critique (Avatar made with Chibi Maker by ~gen8)

User avatar
Belgerum
Regular
Posts: 65
Joined: Thu Nov 06, 2014 12:24 am
Projects: Eternal Hour, Maid Mansion
Organization: Seventh Heart Studios
Skype: belgerum09
Soundcloud: Belgerum
Contact:

Re: Set Variable To Something Complicated

#14 Post by Belgerum » Mon Oct 16, 2017 6:25 pm

In this situation, I might just use a label call instead of a function, and use a single label for drawing any number of cards, instead of two for different amounts.

Perhaps you'd prefer code that looks something like this?

Code: Select all

init python:
    R_cards = ["Taree R", "Waki R", "Mikashi R", "Tamaki R", "Sachiko R", "Sakae R", "Emiya R", "Kanaya R", "Wakuri R", "Ruru R", "Orime R", "Kunie R"]
    SR_cards = ["'We Are Very Happy Girls' Waki SR", "'At A Crossroads' Tamaki SR", "'Ring My Bell' Wakuri SR", "'What A Night Owl' Sachiko SR", "'Day By Day' Emiya SR", "'Song For A Sunny Day' Orime SR", "'Piece Of Cake' Ruru SR'", "'Rule Of Thumb' Kunie SR"]
    SSR_cards = ["'Step By Step' Sakae SSR", "'Biker Jacket Chic' Kanaya SSR"]
    UR_cards = ["'Smiling, Smiling!' Taree UR", "'The First Live Show' Mikashi UR"]

label draw_cards(number_to_draw=1):
    while number_to_draw > 0:
        $ chance = renpy.random.randint(1, 20)
        if chance <= 10:
            $ card_drawn = renpy.random.choice(R_cards)
        elif chance <= 15:
            $ card_drawn = renpy.random.choice(SR_cards)
        elif chance <= 19:
            $ card_drawn = renpy.random.choice(SSR_cards)
        elif chance == 20:
            $ card_drawn = renpy.random.choice(UR_cards)
        $ cards.append(card_drawn)
        $ number_to_draw -= 1
        "You drew:\n[card_drawn]"
    return
Which would then be used like this:

Code: Select all

label start:
    $ cards = []
    $ coins = 100
    "Welcome to the card game!"


label card_choice:
    "You have [coins] coins, and the following cards:\n[cards]"
    
    if coins == 0:
        "You don't have any coins left! Game over!"
        return
    
    menu:
        "How many cards do you want to draw?"
        "One" if coins >= 5:
            $ coins -= 5
            "You put 5 coins in the machine."
            call draw_cards(1)
        "Five" if coins >= 25:
            $ coins -= 25
            "You put 25 coins in the machine."
            call draw_cards(5)
        "Ten" if coins >= 50:
            $ coins -= 50
            "You put 50 coins in the machine."
            call draw_cards(10)
        "As many as I can buy!":
            $ play_count = coins/5
            $ coins = 0
            "You put all of your coins in the machine."
            call draw_cards(play_count)
    "Thanks for playing!"
    jump card_choice

TheChatotMaestro
Regular
Posts: 59
Joined: Mon Jul 31, 2017 8:33 am
Deviantart: LedianWithACamera
Contact:

Re: Set Variable To Something Complicated

#15 Post by TheChatotMaestro » Wed Oct 18, 2017 4:02 pm

Belgerum wrote:
Mon Oct 16, 2017 6:25 pm
In this situation, I might just use a label call instead of a function, and use a single label for drawing any number of cards, instead of two for different amounts.

Perhaps you'd prefer code that looks something like this?

Code: Select all

init python:
    R_cards = ["Taree R", "Waki R", "Mikashi R", "Tamaki R", "Sachiko R", "Sakae R", "Emiya R", "Kanaya R", "Wakuri R", "Ruru R", "Orime R", "Kunie R"]
    SR_cards = ["'We Are Very Happy Girls' Waki SR", "'At A Crossroads' Tamaki SR", "'Ring My Bell' Wakuri SR", "'What A Night Owl' Sachiko SR", "'Day By Day' Emiya SR", "'Song For A Sunny Day' Orime SR", "'Piece Of Cake' Ruru SR'", "'Rule Of Thumb' Kunie SR"]
    SSR_cards = ["'Step By Step' Sakae SSR", "'Biker Jacket Chic' Kanaya SSR"]
    UR_cards = ["'Smiling, Smiling!' Taree UR", "'The First Live Show' Mikashi UR"]

label draw_cards(number_to_draw=1):
    while number_to_draw > 0:
        $ chance = renpy.random.randint(1, 20)
        if chance <= 10:
            $ card_drawn = renpy.random.choice(R_cards)
        elif chance <= 15:
            $ card_drawn = renpy.random.choice(SR_cards)
        elif chance <= 19:
            $ card_drawn = renpy.random.choice(SSR_cards)
        elif chance == 20:
            $ card_drawn = renpy.random.choice(UR_cards)
        $ cards.append(card_drawn)
        $ number_to_draw -= 1
        "You drew:\n[card_drawn]"
    return
Which would then be used like this:

Code: Select all

label start:
    $ cards = []
    $ coins = 100
    "Welcome to the card game!"


label card_choice:
    "You have [coins] coins, and the following cards:\n[cards]"
    
    if coins == 0:
        "You don't have any coins left! Game over!"
        return
    
    menu:
        "How many cards do you want to draw?"
        "One" if coins >= 5:
            $ coins -= 5
            "You put 5 coins in the machine."
            call draw_cards(1)
        "Five" if coins >= 25:
            $ coins -= 25
            "You put 25 coins in the machine."
            call draw_cards(5)
        "Ten" if coins >= 50:
            $ coins -= 50
            "You put 50 coins in the machine."
            call draw_cards(10)
        "As many as I can buy!":
            $ play_count = coins/5
            $ coins = 0
            "You put all of your coins in the machine."
            call draw_cards(play_count)
    "Thanks for playing!"
    jump card_choice
you're the greatest human alive tysm omg

Post Reply

Who is online

Users browsing this forum: No registered users