It's works with the lastest Ren'Py 7.2.0
Let's say you're making a game where you have random events that only show up by chance and have different versions. This code decides if the player get to witness the scene and which one. For example, your player goes to the park and by chance gets to have a date with another character. You have 10 different date scenes or versions that could possible show up. You can use this code to quickly determined if there's a date and which one to show in a single line of code.
Code: Select all
init python:
import random
# Jumps or calls a random label in your game with chance.
def random_event(odds, choices, label_prefix, type=True):
chance = random.randint(1,100)
if chance <=odds:
num = random.randint(1,choices)
label_name = str((label_prefix+str(num)))
if type:
renpy.jump(label_prefix)
else:
renpy.call(label_prefix)
Odds is the odds of seeing the event. The code picks a number between 1 and 100. Then sees if chance is less than or equal to your odds. If your odds is 50 and the chance number is 25 you'll see the event. Otherwise the game doesn't return anything and goes to the next line.
Choices are the sequential numbers that relate to how many different version or choices of something are. If there are three labels that can be shown it will use one of those to show the event.
label_prefix is the prefix of the labels you're calling. This is a string of the label name. So if your picking which date the player sees it could be 'eileen_date'
type Doesn't have to be supplied if you want the player to jump to the label instead of call. If you want it to be called supply False.
Here's how to use it in game.
Code: Select all
label start:
$ variable = 0
label game_loop:
"My function uses odds and a chance to determined if the player witnesses a random scene. If you do get past the chance, the game will also randomize what label is shown to the player."
"Let's turn the random_event() to call the labels instead of jumping to them."
$ random_event(50,3,'choice', False)
if variable == 1:
"I see that you came from the first label."
elif variable == 2:
"You saw the second one right?"
elif variable == 3:
"You saw the third scene, touching."
else:
"Oh, I guess you weren't lucky and didn't see any of the called labels."
"Fun right, but let's say you want to use a jump instead. Which is sometimes a nicer to prevent accidentally returning to the game's start menu."
label jump_loop:
$ random_event(50,3,'choice')
"You are seeing this because you didn't get the random event after a jump. My jump labels all come back to jump_loop."
"End game."
"Before I go, if the player rolls back it will be an entirely different option."
return
So they should look like this. I've given examples of called labels and jumped labels.
Code: Select all
# This is an example of the label that is called. It uses Return at the bottom to go back to where it was called from.
label choice1:
$ variable = 1
"I picked choice 1."
"This is an example of a label that has been called."
return
label choice2:
$ variable = 2
"I picked choice 2."
"This is an example of a label that has been called."
return
label eileen_choice1:
"I've jumped to eileen_choice 1."
jump jump_loop
label eileen_choice2:
"I've jumped to eileen_choice 2."
jump jump_loop
Here's a full sample for those interested in it.
Code: Select all
# Random Event by Chance credit wyverngem 2019 Ren'py Version 7.2.0
init python:
import random
# Jumps to a random label in your game.
def random_event(odds, choices, label_name, type=True):
chance = random.randint(1,100)
if chance <=odds:
num = random.randint(1,choices)
label_name = str((label_name+str(num)))
if type:
renpy.jump(label_name)
else:
renpy.call(label_name)
label start:
$ variable = 0
"Hello"
label game_loop:
"My function uses odds and a chance to determined if the player witnesses a random scene. If you do get past the chance, the game will also randomize what label show the player."
"Because I've added False to the end of my line I call labels instead of jumping to them. Meaning that you'll see what's past this line each time. I've added a variable check to see what scene you actually saw, but you don't need it in game."
$ random_event(50,3,'choice', False)
"The game will always come back to this line when using call instead of jump."
if variable == 1:
"I see that you came from the first label."
elif variable == 2:
"You saw the second one right?"
elif variable == 3:
"You saw the third scene, touching."
else:
"Oh, I guess you weren't lucky and didn't see any of the called labels."
"Fun right, but let's say you want to use a jump instead. Which is sometimes a nicer to prevent accidentally returning to the game's start menu."
label jump_loop:
$ random_event(50,3,'eileen_choice')
"You are seeing this because you didn't get the random event after using random_event() with jump...I'm sorry."
"End game."
"Oh, before I go, rollback will choose something else."
return
# These are the labels that are effected by choices. Feel free to name them whatever prefix you want, but keep them numbered sequential starting at one to work with the code.
label choice1:
$ variable = 1
"I picked choice 1."
"This is an example of a label that has been called."
return
label choice2:
$ variable = 2
"I picked choice 2."
"This is an example of a label that has been called."
return
label choice3:
$ variable = 3
"I picked choice 3."
"This is an example of a label that has been called."
return
# A different name for a jumping label.
label eileen_choice1:
"I've jumped to eileen_choice 1."
"I'm going to go back to jump_loop, and pick another random event."
jump jump_loop
label eileen_choice2:
"I've jumped to eileen_choice 2."
"I'm going to go back to jump_loop, and pick another random event."
jump jump_loop
label eileen_choice3:
"I've jumped to eileen_choice 3, but I want to continue the game."
label end_game:
"The previous choice was eileen_choice3."
"This means I end the game, because sometimes you want to break the cycle of jumps."
"Oh, before I go, rollback will choose something else."
return