Determining the variable based on probable inputs?

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
twistkill
Newbie
Posts: 14
Joined: Sat Feb 24, 2018 4:02 am
Contact:

Determining the variable based on probable inputs?

#1 Post by twistkill » Sat Mar 28, 2020 7:15 pm

Hi Guys,

I hope ya'll are safe and healthy. I am trying to code a small input method where based on the answers, the variable will be set. The code I have used below is asking the user which currency does he operate in, for now its divided into just American and international currency to keep it simple. What I am wondering is-
1. Is the code correct?
2. Is my approach correct?

Any insight will be appreciated. Thank you!

Code: Select all

label currency:
    $ curr = renpy.input("Which currency do you use?")
    $ curr = curr.strip().lower()
    if curr == (dollar or dollars or cent or cents or usd):
        $ curr = "American currency"
    else:
        $ curr = "International currency"
    jump nextlabel

User avatar
gas
Miko-Class Veteran
Posts: 686
Joined: Mon Jan 26, 2009 7:21 pm
Contact:

Re: Determining the variable based on probable inputs?

#2 Post by gas » Sat Mar 28, 2020 7:22 pm

Code: Select all

label currency:
    $ curr = renpy.input("Which currency do you use?")
    $ curr = curr.strip().lower()
    if curr == ("dollar" or "dollars" or "cent" or "cents" or "usd"):
        $ curr = "American currency"
    else:
        $ curr = "International currency"
    jump nextlabel
Or also

Code: Select all

label currency:
    $ curr = renpy.input("Which currency do you use?")
    $ curr = curr.strip().lower()
    if curr in ["dollar", "dollars", "cent", "cents", "usd"]:
        $ curr = "American currency"
    else:
        $ curr = "International currency"
    jump nextlabel
10 ? "RENPY"
20 GOTO 10

RUN

twistkill
Newbie
Posts: 14
Joined: Sat Feb 24, 2018 4:02 am
Contact:

Re: Determining the variable based on probable inputs?

#3 Post by twistkill » Sat Mar 28, 2020 7:26 pm

Oh yeah, quotes! Stupid me.
I think your second suggestion is slightly cleaner. Thank you!

User avatar
gas
Miko-Class Veteran
Posts: 686
Joined: Mon Jan 26, 2009 7:21 pm
Contact:

Re: Determining the variable based on probable inputs?

#4 Post by gas » Sat Mar 28, 2020 8:03 pm

If you want go very overboard and process the thing even further for a lot of values, you can do this.

Code: Select all

default currencies = {"American currency" : ["dollars","dollar","cent","cents","usd"], "European currency": ["euro","eurocents"], "Asian currency" : ["yen", "yuan", "ruble"]} # enlarging at will...

label currency:
    $ curr = renpy.input("Which currency do you use?")
    $ curr = curr.strip().lower()
    $ curr = [x for x, y in currencies.items() if curr in y]
    if not curr:
        $ curr = "Martian Coins"
    jump nextlabel
By abstracting the code a bit more you can turn it into a function, and pass any dictonary and input value.
10 ? "RENPY"
20 GOTO 10

RUN

twistkill
Newbie
Posts: 14
Joined: Sat Feb 24, 2018 4:02 am
Contact:

Re: Determining the variable based on probable inputs?

#5 Post by twistkill » Sat Mar 28, 2020 9:05 pm

Yes! That helps, eventually I will have a a lot of values and your last code would extrapolate easily. Thanks!
I would not build this into a function though, as I find it very confusing and out of my league. :D

User avatar
gas
Miko-Class Veteran
Posts: 686
Joined: Mon Jan 26, 2009 7:21 pm
Contact:

Re: Determining the variable based on probable inputs?

#6 Post by gas » Sun Mar 29, 2020 5:15 am

twistkill wrote:
Sat Mar 28, 2020 9:05 pm
Yes! That helps, eventually I will have a a lot of values and your last code would extrapolate easily. Thanks!
I would not build this into a function though, as I find it very confusing and out of my league. :D
Basic functions are very easy! A quick tutorial (I'm in quarantine, so I have a lot of time to share!).
Think of them as washing machines. You'll input your shirt, the machine do his programa and output the clean shirt.
You're free to have your washing machine do everything you want, the ones that follow are some basic rules.
as you can input any kind of shirt, when you create the washing machine, you'll use fake input names, that later becomethe actual shirts you'll put in use.

Code: Select all

init python:
    def function_name(parameter):
        # do something or not
        return something
So, for example:

Code: Select all

init python:
    def sum_up(number):
        number +=1
        return number
This function is now ready to be used. It get one parameter, that later you must pass. The function eat that number, add 1 and spit it (return) back.

You can do:

Code: Select all

    $ my_cash = sum_up(13)
and, by effect stated, sum_up function get 13, add 1 and return 14. So my_cash is 14.
That's obviously the most basic example.

One thing you can do with functions is to manipulate other variables of the game without any value to 'return'.
So, for example:

Code: Select all

default cash = 10
init python:
    def magic_money():
        global cash
        cash = cash*2
As you can see, we don't pass any parameter. The variable 'cash', that belong to the game, get doubled. The function return none (in fact, if you'll miss the return statement, the function invisibly return None).

Now you can do:

Code: Select all

    $ magic_money()
and the 'cash' global variable that belong to the game get doubled.

Miind that python (renpy) when you quote a function always take his time to compute it, so you can use a function in place of any other variable, using the returned value.
You can do:

Code: Select all

python:
    for i in range(sum_up(10)):
        # bla bla bla
And, by the sum_up function that add 1 to 10, you have a for cycle of 11.
It's practically speaking like creating a new statement.

Hope this clarified a bit what a function is and how you can use it.

In your actual case, you can do...

Code: Select all

init python:
    def get_response(reply, dict):
        reply = reply.strip().lower()
        reply = [x for x, y in dict.items() if reply in y]
        return reply
And that's a function that get a string and a dictionary as input, check if that string is into any value of the dictionary, and return the relative key (or None).
The function stay put until you'll use it and pass some value to it.

Later in your game you can do:

Code: Select all

default pet_species = { "Dog" : ["dog", "corgi", "pomeranian"], "Cat" : ["cat", "persian", "cougar"]}
default currencies = bla bla bla as above

label currency:
    $ curr = renpy.input("Which currency do you use?")
    $ curr = get_response(curr, currencies)
    if not curr:
        $ curr = "Martian Coins"
    jump nextlabel
    
label pet_kind:
    $ pet = renpy.input("What kind of pet do you own?")
    $ pet = get_response(pet, pet_species)
    if not pet:
        $ pet = "Exotic specie"
    jump anotherlabel
10 ? "RENPY"
20 GOTO 10

RUN

twistkill
Newbie
Posts: 14
Joined: Sat Feb 24, 2018 4:02 am
Contact:

Re: Determining the variable based on probable inputs?

#7 Post by twistkill » Tue Mar 31, 2020 2:26 am

I got it! Thank you for the step by step explanation! Thank you so much. I am going to try this out on other things as well.

Much appreciated, kind sir!

twistkill
Newbie
Posts: 14
Joined: Sat Feb 24, 2018 4:02 am
Contact:

Re: Determining the variable based on probable inputs?

#8 Post by twistkill » Fri Apr 03, 2020 9:38 pm

gas wrote:
Sat Mar 28, 2020 8:03 pm
If you want go very overboard and process the thing even further for a lot of values, you can do this.

Code: Select all

default currencies = {"American currency" : ["dollars","dollar","cent","cents","usd"], "European currency": ["euro","eurocents"], "Asian currency" : ["yen", "yuan", "ruble"]} # enlarging at will...

label currency:
    $ curr = renpy.input("Which currency do you use?")
    $ curr = curr.strip().lower()
    $ curr = [x for x, y in currencies.items() if curr in y]
    if not curr:
        $ curr = "Martian Coins"
    jump nextlabel
By abstracting the code a bit more you can turn it into a function, and pass any dictonary and input value.
So I gave this code a test run and entered 'dollars' as my answer and this is the result I get. See the area marked in red? I can usually fix this of it is a regular variable, but not sure how to fix this when using data ranges.
Clipboard01.jpg

twistkill
Newbie
Posts: 14
Joined: Sat Feb 24, 2018 4:02 am
Contact:

Re: Determining the variable based on probable inputs?

#9 Post by twistkill » Sun Apr 05, 2020 2:22 am

I changed the code a bit to allow for blank responses and responses that are not listed in the dictionary. But still getting the u'chosencurrency' error.

Code: Select all

default currencies = {"American currency" : ["dollars","dollar","cent","cents","usd"], "European currency" : ["euro","eurocents"], "Asian currency" : ["yen", "yuan", "ruble"]}

label currency:
    $ curr = renpy.input("Which currency do you use?")
    $ curr = curr.strip().lower()
    $ currtemp = curr
    if curr == "":
        $ curr = "Default Currency"
    else:
        $ curr = [x for x, y in currencies.items() if curr in y]
        if not curr:
            $ curr = currtemp
    jump currency_conf

label currency_conf:
    menu:
        "Your currency is [curr]. Is that right?"
        "Yes":
            "You clicked YES!"
            jump endgame
        "No":
            jump currency

twistkill
Newbie
Posts: 14
Joined: Sat Feb 24, 2018 4:02 am
Contact:

Re: Determining the variable based on probable inputs?

#10 Post by twistkill » Fri Apr 10, 2020 1:30 am

Figured it out. It was being saved as a list, a list of 1 item. Calling the item from the list works just fine. Thanks Gas!

Post Reply

Who is online

Users browsing this forum: No registered users