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.
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:
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:
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