Set Variable To Something Complicated
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.
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.
-
- Regular
- Posts: 91
- Joined: Mon Jul 31, 2017 8:33 am
- Deviantart: LedianWithACamera
- Contact:
Set Variable To Something Complicated
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
- Eileen-Class Veteran
- Posts: 1628
- Joined: Tue May 30, 2017 6:10 am
- Completed: None... yet (as I'm still looking for an artist)
- Projects: An un-named anime based trainer game
- Contact:
Re: Set Variable To Something Complicated
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.
Frameworks & Scriptlets:
- Speech Bubble dialogue system
- Multiple Notify with ATL and history
- (WIP) Radial Masking - needs updating to use Shader
- 7.4 - Smooth Tinting using ATL and matrixcolor
- Several other repositories there too
-
- Regular
- Posts: 91
- Joined: Mon Jul 31, 2017 8:33 am
- Deviantart: LedianWithACamera
- Contact:
Re: Set Variable To Something Complicated
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
- Eileen-Class Veteran
- Posts: 1628
- Joined: Tue May 30, 2017 6:10 am
- Completed: None... yet (as I'm still looking for an artist)
- Projects: An un-named anime based trainer game
- Contact:
Re: Set Variable To Something Complicated
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
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
Frameworks & Scriptlets:
- Speech Bubble dialogue system
- Multiple Notify with ATL and history
- (WIP) Radial Masking - needs updating to use Shader
- 7.4 - Smooth Tinting using ATL and matrixcolor
- Several other repositories there too
-
- Regular
- Posts: 91
- Joined: Mon Jul 31, 2017 8:33 am
- Deviantart: LedianWithACamera
- Contact:
Re: Set Variable To Something Complicated
Thank you! I'll try that
-
- Regular
- Posts: 91
- Joined: Mon Jul 31, 2017 8:33 am
- Deviantart: LedianWithACamera
- Contact:
Re: Set Variable To Something Complicated
To do that, I just set the list items like this "$ fruits = ["orange", "apple", "pear"]" right?
- Remix
- Eileen-Class Veteran
- Posts: 1628
- Joined: Tue May 30, 2017 6:10 am
- Completed: None... yet (as I'm still looking for an artist)
- Projects: An un-named anime based trainer game
- Contact:
Re: Set Variable To Something Complicated
yup, then
$ result = renpy.random.choice( fruits )
"[result]"
$ result = renpy.random.choice( fruits )
"[result]"
Frameworks & Scriptlets:
- Speech Bubble dialogue system
- Multiple Notify with ATL and history
- (WIP) Radial Masking - needs updating to use Shader
- 7.4 - Smooth Tinting using ATL and matrixcolor
- Several other repositories there too
- xavimat
- Eileen-Class Veteran
- Posts: 1461
- Joined: Sat Feb 25, 2012 8:45 pm
- Completed: Yeshua, Jesus Life, Cops&Robbers
- Projects: Fear&Love
- Organization: Pilgrim Creations
- Github: xavi-mat
- itch: pilgrimcreations
- Location: Spain
- Discord: xavimat
- Contact:
Re: Set Variable To Something Complicated
If you are feeling lazy...
Or, a different laziness,
Code: Select all
init python:
def f():
return renpy.random.choice(["orange", "apple", "pear"])
label whatever:
$ result = f()
"[result]"
Code: Select all
default fruits = ["orange", "apple", "pear"]
init python:
r = renpy.random.choice
label whatever:
$ result = r(fruits)
"[result]"
Comunidad Ren'Py en español: ¡Únete a nuestro Discord!
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)
-
- Regular
- Posts: 91
- Joined: Mon Jul 31, 2017 8:33 am
- Deviantart: LedianWithACamera
- Contact:
Re: Set Variable To Something Complicated
That! That is exactly what I wanted. Functions. Thank you so much.xavimat wrote: ↑Fri Oct 06, 2017 6:26 pm If you are feeling lazy...Code: Select all
init python: def f(): return renpy.random.choice(["orange", "apple", "pear"]) label whatever: $ result = f() "[result]"
-
- Regular
- Posts: 91
- Joined: Mon Jul 31, 2017 8:33 am
- Deviantart: LedianWithACamera
- Contact:
Re: Set Variable To Something Complicated
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]")
-
- Regular
- Posts: 91
- Joined: Mon Jul 31, 2017 8:33 am
- Deviantart: LedianWithACamera
- Contact:
Re: Set Variable To Something Complicated
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.
Re: Set Variable To Something Complicated
You are already in a python block, that's why the $ reads as invalid. Remove it.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 also seem to have an indentation error on the ultrarare...
- xavimat
- Eileen-Class Veteran
- Posts: 1461
- Joined: Sat Feb 25, 2012 8:45 pm
- Completed: Yeshua, Jesus Life, Cops&Robbers
- Projects: Fear&Love
- Organization: Pilgrim Creations
- Github: xavi-mat
- itch: pilgrimcreations
- Location: Spain
- Discord: xavimat
- Contact:
Re: Set Variable To Something Complicated
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.
Comunidad Ren'Py en español: ¡Únete a nuestro Discord!
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)
- Belgerum
- Regular
- Posts: 110
- Joined: Thu Nov 06, 2014 12:24 am
- Skype: belgerum09
- Soundcloud: Belgerum
- itch: Belgerum
- Contact:
Re: Set Variable To Something Complicated
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?
Which would then be used like this:
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
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
-
- Regular
- Posts: 91
- Joined: Mon Jul 31, 2017 8:33 am
- Deviantart: LedianWithACamera
- Contact:
Re: Set Variable To Something Complicated
you're the greatest human alive tysm omgBelgerum 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?
Which would then be used 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
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
Who is online
Users browsing this forum: Reficul Hopes