Hey!! Thanks for your time. So I was wondering how I would write the code for something where I want the chances of success to be based on a percentage. I'm trying to implement lottery tickets, and I need it to have a 0.09% chance of success. And also, how would I make it so you get a different response for each outcome? Like if you failed to get one, I want your buddy to say "aw that's too bad, maybe next time!" And if not I want everyone to freak out, plus the earnings to be added. Thank you!!
I return from the void now that everyone is migrating back from the land of the South African Muskrat! Yours is a rather involved answer to see all of it below the cut
The answer to your question, at least the percentage part, is a matter of the randint() function in the Python library. In this case, you want something to happen only 1 in so many times. For example, 1 in 10 is a 10% chance. To simulate this in code, you can have the interpreter pick a random(note this isnât true randomness, but itâs good enough for a video game we arenât making a cyber security system!) number between 1 and 10. If it comes out to one number and one number only, then you pass the check. This makes it a 10% chance, just like if you rolled a 10 sided die.Â
For your .09% chance(very rare!), it would be 1 in ~1,100 chance.(.090909...%). So if you have Python pick a number between 1 and 1,100 and check to see if itâs one specific number, then youâll have your lottery win.
The easiest way to implement this in game is to record the results of the 1 in 1,100 chance inside a function wrapper so that you can call it multiple times and they can check lots of tickets.
init python:Â Â def Lottery_Ticket():Â Â Â Â ticket_num = renpy.random.randint(1, 1100)Â Â Â Â return ticket_num
In order to use the randint() function from the random library, you just need to invoke it! Itâs a part of Renpyâs library. So youâd type renpy.random.randint(1,1100) like above. :)
Once that is done in your game code itself youâll put the logic for the winning number. Letâs say itâs 42, for meme reasons.Â
You could write in your game file:
label check_ticket:  $ ticket = Lottery_Ticket()  if ticket == 42:    jump you_win  else:    jump you_lose
This simple logic sets the ticket equal to the result of the Lottery_Ticket function. It then checks if the number is exactly the winning number you chose. If so, it takes you to the winning section of your game code, if not, it falls through and says you lose.
Every time they buy a ticket, you can send them to this label, it will re-run the Lottery_Ticket function anew, picking a new random number, and see if they won.
Under your winning code, since you asked, this is where youâd put your characters reacting positively! Note the thing in brackets is the player characterâs name as a variable being interpolated by the engine. If you let the player pick their own name, thatâs how youâd get it into the script. If they have a set name, just type that instead.Â
label you_win:  e âOh my God! [PC_Name] you did it!  e âItâs like a dream come true!â  $ Inv.add(lottery_winnings)  $ renpy.notify(âYou addedâ + str(lottery_winnings) + â to your wallet!â)
As you see, Iâm using to functions. One that comes with RenâPy calls in the notify screen context through the function. It needs to be a string inside which is why Iâve turned the int representing the lottery winnings into a string here.(This assumes you have a variable called âlottery_winningsâ that has a specific integer value! Please initialize one before the start of your game!) (example: $ lottery_winnings = 10000)
The second is me assuming that you have an Inventory class instance with a method called âaddâ that can add money into a wallet variable. To make something like that:
init python:  class Inventory(python_object):    def__init__(self, wallet = 0):      self.wallet = wallet      self.pocket = []       def add(num):      self.wallet += num      return self.wallet
Then you just need an instance of it:$ Inv = Inventory() (I wrote it with defaults, so you donât need to pass any arguments to the instance when you make it, though you are welcome to pass an INTEGER to it when you instantiate it so that your character starts with a certain amount of money. :D )
Then you can call the method on the variable to add the earnings to their wallet. :)Â
Thereâs also an empty pockets list inside of the inventory you can use to store the string names of objects they also own. For something like that:
$ Inv.pocket.append(âappleâ)
This will add a string called âappleâ to the pocket list inside the inventory. Which you can use to check if they âownâ an object by checking if it is in the list.
if âappleâ in Inv.pocket:  .......
But thatâs more detail for another time.Â
You need to put the class in an init python block before the game starts so that you can make an instance of it to manipulate later.
For not winning, just have the âyou_loseâ label have the dialogue you wanted.
label you_lose:    e âAw, thatâs too bad. Maybe next time!â
And then continue on with the rest of the game.
I hope everything here is clear and this helps. I know this ask came in ages ago, but I still think answering it will help RenâPy users in the now. Remember: If your code wonât compile, and you donât what to do; who you gonna call? The RenâPy Help Desk!
Anya is live and ready to show you everything. Watch her strip, dance, and perform exclusive shows just for you. Interact in real-time and make your fantasies come true.
â Live Streamingâ Interactive Chatâ Private Showsâ HD Quality
Anya is LIVE right now
FREE
Free to watch ⢠No registration required ⢠HD streaming
Hello! I'm not sure if this blog is still active, but it's worth a shot. When doing something with multiple endings, how would you do a dating sim with multiple endings for each character, like a Good one and a Bad one?
Heya Isaacie! Better late than never I hope?
I did an initial treatment of the idea of multiple endings here. Iâd go about it a little differently now, so Iâll give more detail about architecture for a game below.
The basic substance of your question is âhow do I make the game interpret the difference between different states?â. If youâre using variables to keep track of things, all you need to do is have the game test if the player has met certain conditions in order to determine an outcome.Â
For a game with multiple romance-ables, it might work best to separate the romance paths into different files. Then, at the end of each romance file, have the conditionals for if something is a good or bad end. You can bring the player back into the MAIN file for things that are the same across all runs, and then have the game check to see which character they are romancing to go decide where to go.Â
This would work something like:Main story line ~> romance path chosen ~> go to characterâs romance file ~> romance interaction #1 ~> back to main file ~> romance interaction #2 ~> ect.
In that case, it would be simplest to CALL the new fileâs first label, then RETURN at the end of the romance scene after choices are made. This will put you back into the main script right where you last left off to do general same story line things. Then you can call the next label in from the romance file and return back, ect.
If the story never intersects again once a main love interest is chosen(think something like Katawa Shoujo) then you can just continue the story in those romance files. (Youâll probably want to set a variable like Romance_X = True in your files too if it affect the main plotline minimally, but you still want ppl to reference it in dialogue or something!)
At the end of the romance files you need some way to determine if theyâve done âgoodâ or âbadâ to see what ending they should get. You can do this by keeping track of how many âaffection pointsâ they got and deciding if they have below a certain value they get a âBad Endâ.Â
Example:
label determine_ending:     if Leo_love <= 10:    jump bad_ending  elif Leo_love ....    .....
Ect for each scenario. This only works if they can only romance one person at a time. They get locked into a romance path(whether or not the rest of the game has story beats in common between routes) and so only one person needs to be checked.
But what if you wanna let them romance multiple ppl at once and see which one they got the farthest with?
For something like that youâll need to put the affections in a dictionary, determine which one is the largest and then do the good/bad ending logic above.The logic would look something like this:
init python:Â Â John_love = 0Â Â Mike_love = 0Â Â Jimmy_love = 0Â Â Sasha_love = 0(game play where you add affection with variable_name += number amount)(then a python block right before the end of the game)python:Â Â affection_dict = {"John": John_love, "Mike": Mike_love, "Jim": Jimmy_love, "Sasha": Sasha_love}Â Â love_holder = []Â Â winner = None
  for x in affection_dict.values():    love_holder.append(x)  love_max = max(love_holder)  for x in affection_dict:      if affection_dict[x] == love_max:        winner = x
Then at the end of your main file youâd type something like this:
if winner == âSashaâ:  jump Sasha_endingselif winner == âMikeâ:  jump Mike_endings  ....
Ect for each romanceable. Then you once again determine if they got a good or bad ending with them or not based on affection, which is separate from who they had the most affection with.(they could theoretically have 0, 0, 5, 9 and need a minimum of 10 for any non-bad end, if can even check to see if the max of the list is bigger than 10 and if not just have them giga fail with an ultimate bad end or something. Youâd just add a line after love_max = max(love_holder) to say:
if love_max < 10:Â Â jump ultra_fail
They wonât even get to the rest of the code to pick the winner because it doesnât matter lol.)
All that being said, the basic principals are:-have conditions that are failable-figure out what level of failure justifies a bad ending based on how your game plays-test the state of the game based on those conditions-use the result of that state testing to produce an end state result
I know this was long, and a very delayed answer, but I hope this helps you and others! Remember: If your code wonât compile and you donât know why; who ya gonna call? The RenâPy Help Desk!
This is a matter of the configuration of Renpy itself. This is commonly changed though and can be accessed through an init python block and the âconfigâ namespace.
To change the resolution of the game youâd just need to type:
init python:     config.screen_width = new integer  config.screen_height = new integer
This needs to be done before the gameâs start label and can be accomplished at the beginning of the renpy.gui file for your game, for example. You could also put it in the main file for your game but uh, try to stay organized please?
Thatâs all for this one. Remember: If your code wonât compile, and you donât know why; who ya gonna call? The RenâPy Help Desk!