Random event code. (Example)

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Post Reply
Message
Author
helcries
Newbie
Posts: 17
Joined: Thu Oct 27, 2016 3:30 am
Contact:

Random event code. (Example)

#1 Post by helcries »

I struggled with trying to come up with some random event code that would do what I wanted, I took several days to look at the ways other people were doing it and couple find exactly what I was wanting to do.

Then I finally got it working earlier today, just thought I would toss this out there for anyone having the same problem I was.

Code: Select all

label base_travel_events:
        $ chance = renpy.random.randint(1,100)  ## Just the range I want the code to look at to determine what happens
        if chance <= 10: ## This is 10% of the time something happens.
            $ choice = renpy.random.choice(("choice1","choice2","choice3","choice4","choice5","choice6")) ## These are the events I want to have the code choose from.
            
            if choice == "choice1": ## Same as what is listed above.
                jump choice1a ## This is the label that you want the code to point at
            elif choice == "choice2":
                jump choice2a
            elif choice == "choice3":
                jump choice3a
            elif choice == "choice4":
                jump choice4a
            elif choice == "choice5":
                jump choice5a
            elif choice == "choice6":
                jump choice6a
            
        else: ## This is the other 90% of the time.
            jump hallways
            
hope this helps out if anyone is trying to do their own random events.

(If theres anyway to make this better or more effective feel free to comment. But please don't raz me to much if this seems a little to simple I am a noobish beginner atm.)

User avatar
+mike
Newbie
Posts: 7
Joined: Sat Oct 07, 2017 1:50 pm
Contact:

Re: Random event code. (Example)

#2 Post by +mike »

Thanks for sharing, this will be helpful for my project.
What makes this interesting is that you only have a random choice (choice1 ~choice6) IF your chance belongs to a given range (less than 10)
otherwise nothing happens.

An easy and elegant solution that can easily be modified for other values and settings.
• make it simple, it should work! •

irredeemable
Regular
Posts: 78
Joined: Thu Feb 08, 2018 7:57 am
Contact:

Re: Random event code. (Example)

#3 Post by irredeemable »

There are tons of different ways to handle this, just depending on what you're trying to accomplish. Sticking to the specifics of your example, it could be simplified like this:

Code: Select all

label base_travel_events:
        if renpy.random.randint(1,100) <= 10:
            jump expression "choice{0}a".format(renpy.random.randint(1,6))
        jump hallways
expression is a Ren'py keyword that allows you to use a Python expression to create a string that is interpreted as a Ren'py label. I used the the format() method of the string class as it's more readable(to me, anyway) when you have strings on either side of a variable. It also happens to be faster in this situation(though that hardly matters here) since we're would be doing string conversion with str(). The alternative, simple concatention with +: jump expression "choice" + str(renpy.random.randint(1,6)) + "a" is also fine.

User avatar
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: Random event code. (Example)

#4 Post by Remix »

All nice when you get something working all on your own. Good job :)

As irredeemable notes though, it could be reduced quite a lot and still keep the same functionality. The main point is that (as he eluded) you are choosing from a set of choices and then using hard-typed code to run conditionals against that when most of the work has already been done.

In my own mind, to make it more usable, perhaps pass the choices into the label so that the code/label can be re-used many times just by altering the call rather than having to write a new label...

Simple (except maybe the use of *args and the 'incomprehensible' nested conditional list comprehension) example:

Code: Select all

label choice_jump(*choice_targets):
    # *args can be lists/tuples:
    # [ 'labelname' (string), 5 (integer weight) ]
    # [ 'labelname' (string) ] # given weight 1
    #  'labelname' (string) # expanded to [ 'labelname', 1 ]

    # build a list of weighted choices
    $ choice_jumps = [g for h in [
               [k[0]]*k[1] for k in [
                [j,1] if isinstance(j, basestring)
                else j if len(j)>1 
                else [j[0],1] for j in choice_targets]
               ]
              for g in h]
              
    # choose one from the list
    $ choice_target = choice_jumps[renpy.random.randint(0,len(choice_jumps)-1)]
    # could use " $ choice_target = renpy.random.choice( choice_jumps ) " instead
    
    ## comment out these to avoid the debug dialogue
    "Jumps: [choice_jumps]"
    "Goto: [choice_target]"
    
    jump expression choice_target


label start:
    call choice_jump( ['label_a',5], ['label_b', 7], ['label_c'], 'label_d' )
    # or (for your use-case-scenario)
    call choice_jump( 'choice1a', 'choice2a', 'choice3a', 'choice4a', 'choice5a', 'choice6a', ['hallways',54] ) 
Though that might not make a huge amount of sense, maybe just play around with it, see what it does and then backtrack and deduce. (some code comments added to help)

Edit: Checked, deleted. tested, fixed, re-posted... Oops
Frameworks & Scriptlets:

Post Reply

Who is online

Users browsing this forum: No registered users