renpy.randint not working as expected.

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
User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 2804
Joined: Mon Dec 14, 2015 5:05 am
Location: Your monitor
Contact:

renpy.randint not working as expected.

#1 Post by Imperf3kt » Wed Apr 12, 2017 7:58 pm

I'm not able to supply code snippets at the moment due to not having access to the computer they're on.

The short version:
renpy.randint is giving me headaches, is there an alternative for displaying a random image on a screen such as the main menu?

The long version:

I am making an app for Android and I want to have a single image on the main menu, cycle between 4 choices.
I used renpy.randint to randomly generate a number from 1 to 4, then added an if clause to my main menu which basically checks which number the generator came up with, and used an assigned image accordingly.
This works perfectly except for one small issue.

Since I am only just starting development of this app, there is no real content yet, so I added a few lines of dialogue.
Upon reaching the end of the dialogue, the game returns to the main menu. This is where the issue appears, approximately 0.25 seconds pass between arriving at the main menu, and a new image showing. The old image being replaced as expected, but still being visible for that fraction of a second is rather unwanted.

This happens on both my PC and an Android device.
Should I define a default variable for the image to use, or would that not make a difference?


Honestly, I'd prefer not to use random at all and would prefer to load the images in sequence each time a user visits the screen. Is there a better way of doing this?
I can supply code in about 8 hours if necessary.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Twitter

User avatar
nyaatrap
Crawling Chaos
Posts: 1824
Joined: Mon Feb 13, 2012 5:37 am
Location: Kimashi Tower, Japan
Contact:

Re: renpy.randint not working as expected.

#2 Post by nyaatrap » Thu Apr 13, 2017 12:01 pm

Imperf3kt wrote:the game returns to the main menu. This is where the issue appears, approximately 0.25 seconds pass between arriving at the main menu, and a new image showing. The old image being replaced as expected, but still being visible for that fraction of a second is rather unwanted.
This is a ren'py bug - store variables are reset to the default values before transition to the main menu finished. One solution is use persistent variables instead of store variables. Another solution is always hide images before returning to the main menu.
load the images in sequence each time a user visits the screen. Is there a better way of doing this?
This may also work as well (Sometimes screen variables give an 'undefined error' when returning to the main menu. But If you carefully code it, it will work)

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

Re: renpy.randint not working as expected.

#3 Post by Imperf3kt » Thu Apr 13, 2017 3:46 pm

Ah okay, thats helpful to know.
I'll see what I can come up with. Thanks for the help.

E:
Works like a charm!

Here's what I did:

screens.rpy

Code: Select all

screen main_menu():

    tag menu

    style_prefix "main_menu"

    add gui.main_menu_background
    add gui.main_menu_text
    if persistent.main_menu_sprite == 1:
        add gui.main_menu_sprite_1
    if persistent.main_menu_sprite == 2:
        add gui.main_menu_sprite_2
    if persistent.main_menu_sprite == 3:
        add gui.main_menu_sprite_3
    if persistent.main_menu_sprite == 4:
        add gui.main_menu_sprite_4
gui.rpy

Code: Select all

## Main and Game Menus #########################################################

## The images used for the main and game menus.
define gui.main_menu_background = "gui/base.png"
define gui.main_menu_text= "gui/main_menu.png"
define gui.game_menu_background = "gui/game_menu.png"#not needed, remove later
define gui.main_menu_sprite_1 = "gui/menusprites/1.png"
define gui.main_menu_sprite_2 = "gui/menusprites/2.png"
define gui.main_menu_sprite_3 = "gui/menusprites/3.png"
define gui.main_menu_sprite_4 = "gui/menusprites/4.png"
script.rpy

Code: Select all

init -1:
    default persistent.main_menu_sprite = 1

label start:

    #some text goes here#

    $ persistent.main_menu_sprite += 1
    if persistent.main_menu_sprite >= 5:
        $ persistent.main_menu_sprite -= 4

    return
Now all images cycle between the available sprites as intended with no laggy switching out of the previous image.
The only thing to work out now is how to apply this function so when a user prematurely exits to the main menu, it still takes effect. perhaps if I fiddle with the exit button. Though that won't solve ALT+F4 exits.

Anyway, cheers! I'm happy enough with this.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Twitter

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

Re: renpy.randint not working as expected.

#4 Post by Imperf3kt » Fri Apr 14, 2017 8:34 pm

Small update:
I changed the behaviour so now the image changes every single time a user visits the main menu:

From script.rpy I removed:

Code: Select all

    $ persistent.main_menu_sprite += 1
    if persistent.main_menu_sprite >= 5:
        $ persistent.main_menu_sprite -= 4
And instead added this to the bottom of my main menu:

Code: Select all

    if persistent.main_menu_sprite == 1:
        imagebutton idle "gui/button/start.png" hover "gui/button/start.png" xpos 0 ypos 899 focus_mask True action ShowMenu("modesel"), SetField(persistent, "main_menu_sprite", 2)
    elif persistent.main_menu_sprite == 2:
        imagebutton idle "gui/button/start.png" hover "gui/button/start.png" xpos 0 ypos 899 focus_mask True action ShowMenu("modesel"), SetField(persistent, "main_menu_sprite", 3)
    elif persistent.main_menu_sprite == 3:
        imagebutton idle "gui/button/start.png" hover "gui/button/start.png" xpos 0 ypos 899 focus_mask True action ShowMenu("modesel"), SetField(persistent, "main_menu_sprite", 4)
    elif persistent.main_menu_sprite == 4:
        imagebutton idle "gui/button/start.png" hover "gui/button/start.png" xpos 0 ypos 899 focus_mask True action ShowMenu("modesel"), SetField(persistent, "main_menu_sprite", 1)

Just to clarify, my main menu is actually a menu, before my real main menu. It is just a welcome screen of sorts, with the text (as an image) overlayed on a base menu background plus the sprite(s) and a Start / Touch to Continue button.

The code above pretty much says (in human readable terms):
show this image and when this button is pressed, change it to this image.

I figured this may be useful to someone else, so I posted this extra information in order to help.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Twitter

Post Reply

Who is online

Users browsing this forum: Google [Bot], IaMe, Rainvillain, SwashbucklerXX