Random number slows down game

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
Exiscoming
Regular
Posts: 132
Joined: Tue Apr 29, 2014 5:37 pm
Contact:

Random number slows down game

#1 Post by Exiscoming »

All right, so this is something I've been struggling with. Let's say I want a random number like this:

Code: Select all

$ randNumb = renpy.random.randint(1, 2)
That works fine, but takes a split second, causing a delay.

Now I'm currently coding something that requires a lot of random outcomes, which is starting to add up to about half a second worth of delay.
For example:

Code: Select all

label supermarket:
	$ discount1= renpy.random.randint(1, 10)
	$ discount2= renpy.random.randint(1, 20)
	$ discount3= renpy.random.randint(1, 25)
	$ discount4= renpy.random.randint(1, 30)
	$ discount5= renpy.random.randint(1, 50)
	scene supermarket
	b "Hi I'm bob."
If I were to jump to the above mentioned label supermarket. It would first calculate the random discounts on offer, causing a split second delay for every random number.
Is there an alternative to this that I can maybe use?

Suggestions are appreciated!

User avatar
namastaii
Eileen-Class Veteran
Posts: 1350
Joined: Mon Feb 02, 2015 8:35 pm
Projects: Template Maker for Ren'Py, What Life
Github: lunalucid
Skype: Discord: lunalucid#1991
Soundcloud: LunaLucidMusic
itch: lunalucid
Location: USA
Contact:

Re: Random number slows down game

#2 Post by namastaii »

Does defining these variables before the game starts help?

Code: Select all

define discount1 = renpy.random.randint(1, 10)
define discount2 = renpy.random.randint(1, 20)
define discount3 = renpy.random.randint(1, 25)
define discount4 = renpy.random.randint(1, 30)
define discount5 = renpy.random.randint(1, 50)
or you could put default instead of define

I don't know if these will be changing throughout the game?

Code: Select all

    e "1 You've created a new Ren'Py game."
    e "Discount amount [discount1]"
    $ discount1 = renpy.random.randint(1, 10)
    e "New discount amount [discount1]"

User avatar
Alex
Lemma-Class Veteran
Posts: 3090
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Random number slows down game

#3 Post by Alex »

Also, the delay might occure if some lines later in your code you have a huge image or movie or something else that Ren'Py tries to cache wile showing supermarket scene.

Try to add a dosen of text lines after that part of code for test purposes.

Exiscoming
Regular
Posts: 132
Joined: Tue Apr 29, 2014 5:37 pm
Contact:

Re: Random number slows down game

#4 Post by Exiscoming »

namastaii wrote: Tue Aug 20, 2019 11:31 am Does defining these variables before the game starts help?

Code: Select all

define discount1 = renpy.random.randint(1, 10)
define discount2 = renpy.random.randint(1, 20)
define discount3 = renpy.random.randint(1, 25)
define discount4 = renpy.random.randint(1, 30)
define discount5 = renpy.random.randint(1, 50)
or you could put default instead of define

I don't know if these will be changing throughout the game?

Code: Select all

    e "1 You've created a new Ren'Py game."
    e "Discount amount [discount1]"
    $ discount1 = renpy.random.randint(1, 10)
    e "New discount amount [discount1]"
Heya, so I tried defining / defaulting them which unfortunately doesn't really do anything.
These changes happen constantly throughout the game and are an important part, so having as little of a delay as possible is vital.

These delays do happen right before showing a movie in one case or a image (2mb) in another. However both of these have already been compressed to be as small as possible, so I can't really do anything about that.

I gave the supermarket setting just as an example. In actuality it's a more complex system where you guide a character through a randomly generated room. When they reach the exit, all the stats are randomized again and a new room is generated.
As soon as the player pressed on the exit, it very quickly assigns a new random number to a few variables before showing you the next room. Right now I do:

Code: Select all

scene black with dissolve 						# Just a black jpg
$ random1= renpy.random.randint(1, 3)
$ random2= renpy.random.randint(1, 5)
$ random3= renpy.random.randint(1, 3)
$ random4= renpy.random.randint(1, 2)
$ random5= renpy.random.randint(1, 6)
show globalImage with dissolve					# A layeredimage with objects appearing depending on the above generated random numbers.
It's a little tricky to do, but I'd say every Random number takes about 0.1 seconds. So the above code would put in a delay of 0.5sec.
That might not seem as a lot, but the player goes through a room quite quickly, so they'll constantly be watching a 0.5sec black screen.

User avatar
namastaii
Eileen-Class Veteran
Posts: 1350
Joined: Mon Feb 02, 2015 8:35 pm
Projects: Template Maker for Ren'Py, What Life
Github: lunalucid
Skype: Discord: lunalucid#1991
Soundcloud: LunaLucidMusic
itch: lunalucid
Location: USA
Contact:

Re: Random number slows down game

#5 Post by namastaii »

Are you sure it isn't your computer as well? I made a character dress up demo once and it ran smoothly on my computer but lagged on someone else's

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3785
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: Random number slows down game

#6 Post by Imperf3kt »

You could probably generate the new random numbers before reaching the new room, then switch and use the old random numbers to generate new numbers.

This would at least hide or mask the lag.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

drKlauz
Veteran
Posts: 239
Joined: Mon Oct 12, 2015 3:04 pm
Contact:

Re: Random number slows down game

#7 Post by drKlauz »

Generation of random number is very fast operation, it can't slow down game. Most likely issue is failed image prediction, as Alex mentioned. Issue is more glaring when using higher resolutions, 1280x720 would run smooth, but 1920x1080 may show visible lag during some scenes transitions.
I may be available for hire, check my thread: viewtopic.php?f=66&t=51350

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 number slows down game

#8 Post by Remix »

The randoms are definitely not the problem here.

Just ran a test with

Code: Select all

init python:

    import time

    def get_time_for_thousand_rands():

        start = time.time()

        rands = [ renpy.random.randint(0,50) for k in range(1000) ]

        end = time.time()

        return end - start


label start:

    $ duration = get_time_for_thousand_rands()

    "[duration!q]"
Which shows generating 1000 random numbers between 0 and 50 took just 3 thousandths of a second ... ( 0.003 seconds )
Frameworks & Scriptlets:

Exiscoming
Regular
Posts: 132
Joined: Tue Apr 29, 2014 5:37 pm
Contact:

Re: Random number slows down game

#9 Post by Exiscoming »

All right, I'll do some more digging to figure out what's causing it. Thanks for all your feedback!
I'll update this thread if I figure it out. =)

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot]