help with screen to displays random messages

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
jadeon12
Newbie
Posts: 24
Joined: Sat Mar 05, 2016 6:17 am
Contact:

help with screen to displays random messages

#1 Post by jadeon12 »

Hello Fellow Renpy users,

I am working on this game where I have a screen that opens to display certain random messages, its a Krystal ball whom the player can take it out of its pocket peer in it and get a random message.

Here is part of the code:

Code: Select all


label ballgaze:

$ ball = renpy.random.randint(1, 4)
if ball == 1:
    $messa = "Today its going to Rain heavily., but stay focused on your purpose" 
if ball == 2:
    $messa = "What does not kill you will make you stronger." 
if ball == 3:
    $messa = "Your destiny is in your hands." 
if ball == 4:
    $messa = "Remember the reason you are here!!"



screen kriball_button:
    imagebutton:
            idle "appear_idle.png"
            hover "appear_hover.png"
            action  [Show ("kriball_screen "), Hide("kriball_button")] align (.40,.90)

screen kriball: 
    modal True 
    window:
        vbox:   
            label "Krystal Ball"           
            text " Digging in your pocket you take out the Krystal ball and peer inside for a message:"
            text ""
            text " [messa] "

This code works and displays a random message when the screen is opened, however if i close and reopen the screen it will keep giving me the same message instead of giving a random one.

How can i make it that the screen will display another random message after i close and reopen it again?

I understand that somehow the message ballgaze "function" should run each time the screen is opened in order to generate a new one.

But I cannot figure out how to get it done, I hope someone can give me a hand with this.

User avatar
papiersam
Veteran
Posts: 231
Joined: Fri Aug 12, 2016 2:24 pm
Completed: Gem Hunt Beta, 1/Probably, Animunch
Projects: The Panda Who Dreamed
Contact:

Re: help with screen to displays random messages

#2 Post by papiersam »

You could use a python function instead. Possibly something like this:

Code: Select all

init python:
    
    import random #need the module
    
    def ballgaze(): #the function
        ball = random.randint(1, 4)
        if ball == 1:
            return "Today its going to Rain heavily., but stay focused on your purpose" 
        if ball == 2:
            return "What does not kill you will make you stronger." 
        if ball == 3:
            return "Your destiny is in your hands." 
        if ball == 4:
            return "Remember the reason you are here!!"
            
label start:
    
    "Test"
    
    $messa = ballgaze()
    
    "[messa]"
    
    "Now we can call the screen:"
    
    call screen kriball_button


screen kriball_button:
    imagebutton:
            idle "appear_idle.png"
            hover "appear_hover.png"
            action  [Show ("kriball_screen "), Hide("kriball_button")] align (.40,.90)

screen kriball_screen: 
    modal True 
    window:
        vbox:   
            label "Krystal Ball"           
            text " Digging in your pocket you take out the Krystal ball and peer inside for a message:"
            text ""
            text ballgaze() #call the function

jadeon12
Newbie
Posts: 24
Joined: Sat Mar 05, 2016 6:17 am
Contact:

Re: help with screen to displays random messages

#3 Post by jadeon12 »

r_sami wrote:You could use a python function instead. Possibly something like this:

Code: Select all

init python:
    
    import random #need the module
    
    def ballgaze(): #the function
        ball = random.randint(1, 4)
        if ball == 1:
            return "Today its going to Rain heavily., but stay focused on your purpose" 
        if ball == 2:
            return "What does not kill you will make you stronger." 
        if ball == 3:
            return "Your destiny is in your hands." 
        if ball == 4:
            return "Remember the reason you are here!!"
            
label start:
    
    "Test"
    
    $messa = ballgaze()
    
    "[messa]"
    
    "Now we can call the screen:"
    
    call screen kriball_button


screen kriball_button:
    imagebutton:
            idle "appear_idle.png"
            hover "appear_hover.png"
            action  [Show ("kriball_screen "), Hide("kriball_button")] align (.40,.90)

screen kriball_screen: 
    modal True 
    window:
        vbox:   
            label "Krystal Ball"           
            text " Digging in your pocket you take out the Krystal ball and peer inside for a message:"
            text ""
            text ballgaze() #call the function
Hello r_sami,

Thank you for your reply, i tried the above function, although the screen opens, the message still does not update(does not change) after one closes and opens the screen by pressing the imagebutton.

it gives me the same message,

I used both call dcreen or show screen but its not working

User avatar
gas
Miko-Class Veteran
Posts: 842
Joined: Mon Jan 26, 2009 7:21 pm
Contact:

Re: help with screen to displays random messages

#4 Post by gas »

(import random??)

Another practical solution...

Code: Select all

default tenses=["You're having a bad day","I think someone will help you in a forum","Someone will write a code for you"]

label start:
     show screen kriball_button
     "Did you like horoscopes?"

screen kriball_button:
    imagebutton:
            idle "appear_idle.png"
            hover "appear_hover.png"
            action  [Show ("kriball_screen"), Hide("kriball_button")] align (.40,.90)

screen kriball:
    modal True
    $ myfate=renpy.random.choice(tenses) #pick a random tense from the list "tenses"
    window:
        vbox:   
            label "Krystal Ball"           
            text " Digging in your pocket you take out the Krystal ball and peer inside for a message:"
            text ""
            text " [myfate] "

so you can just add new tenses and everything's fine.
(Anyway you need a button to hide the kriball screen, somehow. Don't forget to add it!)
If you want to debate on a reply I gave to your posts, please QUOTE ME or i'll not be notified about. << now red so probably you'll see it.

10 ? "RENPY"
20 GOTO 10

RUN

User avatar
papiersam
Veteran
Posts: 231
Joined: Fri Aug 12, 2016 2:24 pm
Completed: Gem Hunt Beta, 1/Probably, Animunch
Projects: The Panda Who Dreamed
Contact:

Re: help with screen to displays random messages

#5 Post by papiersam »

(import random??)
Ah, my bad. Should look like this, then:

Code: Select all

    from random import randint
    
    def ballgaze():
        ball = randint(1, 4)
        if ball == 1:
            return "Today its going to Rain heavily., but stay focused on your purpose" 
        if ball == 2:
            return "What does not kill you will make you stronger." 
        if ball == 3:
            return "Your destiny is in your hands." 
        if ball == 4:
            return "Remember the reason you are here!!"

User avatar
Divona
Miko-Class Veteran
Posts: 678
Joined: Sun Jun 05, 2016 8:29 pm
Completed: The Falconers: Moonlight
Organization: Bionic Penguin
itch: bionicpenguin
Contact:

Re: help with screen to displays random messages

#6 Post by Divona »

r_sami wrote:
(import random??)
Ah, my bad. Should look like this, then:
Could just use "renpy.random" instead of import, no?

Code: Select all

    def ballgaze():
        ball = renpy.random.randint(1, 4)
        if ball == 1:
            return "Today its going to Rain heavily., but stay focused on your purpose" 
        if ball == 2:
            return "What does not kill you will make you stronger." 
        if ball == 3:
            return "Your destiny is in your hands." 
        if ball == 4:
            return "Remember the reason you are here!!"
Completed:
Image

jadeon12
Newbie
Posts: 24
Joined: Sat Mar 05, 2016 6:17 am
Contact:

Re: help with screen to displays random messages

#7 Post by jadeon12 »

Thank you very much all of you,

The correction of R_sami is working, and Gas method also, need to try Givona's yet.

However I tried to make a similar function without random for another feature of the glass ball, where i want it to change color and size according to certain events like what the player ate or what his mood are as he opens the screen:

I have the variable mood that is set to 1 for green.
If the player eats a chili it should change to red, and the screen will have [messa] substituted for that value, however its not working, when i open and close the screen, the value does not change accordingly it stays at green.


I hope you can give me a hand friends.

Code: Select all

 

init python:
     
    def ballgaze():
        global mood
        if mood == 1:
            return "green"
        if mood == 2:
            return "blue"
        if mood == 3:
            return "red"
        if mood == 4:
            return "yellow"
           
label start:
   
    "Test"
    $ mood = 1
    $messa = ballgaze()
   
    "[messa]"
   
   
    show screen kriball_button

label colortest:
menu:
      "eat a chili":
          $mood = 3
          "you ate a fiery chili"
          jump colortest
      "eat a mango":
          $mood = 4
          "you ate a sweet mango"
          jump colortest
      "eat blueberries":
          $mood = 2
          "you ate a cooling berrie"
          jump colortest
          


screen kriball_button:
    imagebutton:
            idle "appear_idle.png"
            hover "appear_hover.png"
            action  [Show ("kriball_screen "), Hide("kriball_button")] align (.40,.90)

screen kriball_screen:
    modal True
    window:
        vbox:   
            label "Krystal Ball"           
            text " Digging in your pocket you take out the Krystal ball and peer inside for a message:"
            text ""
            text "the ball is giving a [messa]ish pulsing glow"
            
    imagebutton:
            idle "closeappi_idle.png"
            hover "closeappi_hover.png"
            action [ Hide("kriball_screen"), Show("kriball_button")] align (.40,.90) 

User avatar
gas
Miko-Class Veteran
Posts: 842
Joined: Mon Jan 26, 2009 7:21 pm
Contact:

Re: help with screen to displays random messages

#8 Post by gas »

Ok let's simplify everything.
You want for the sphere to say something based on the mood value.

If you set mood=1 in a label, renpy put at 1 each time he reach again that label.
So, define it at first OUTSIDE the normal course of events.
Then use a list to link the various conditions to the variable. Is called INDEXING.
in python, each element of a list can be retrieved by referring his index number, counting FROM 0 ONWARD.

Code: Select all

default mood=0
default shade=["green","red","blue"]  # so, mood 0 = green, mood 1= red, mood 2= blue
label start:
    show screen kriball
label feast:
    menu:
        "Eat mango!":
            $ mood=0
            "You ate a juicy mango!"
        "Eat chili!":
            $ mood=1
            "You ate a fiery chili pepper!"
        "Eat alien food":
            $ mood=2
            "Something strange happened..."
    jump feast
screen kriball_button:
    imagebutton:
            idle "appear_idle.png"
            hover "appear_hover.png"
            action  [Show ("kriball_screen "), Hide("kriball_button")] align (.40,.90)

screen kriball_screen:
    modal True
    $ yourcolor=shade[mood]
    window:
        vbox:   
            label "Krystal Ball"           
            text " Digging in your pocket you take out the Krystal ball and peer inside for a message:"
            text ""
            text "the ball is giving a [yourcolor]ish pulsing glow"

With this logic it's very easy to accomplish what you want for.
Change the mood (the index) and retrieve the string.

If you find some other difficulty, ask for! That kriball seems fun!
If you want to debate on a reply I gave to your posts, please QUOTE ME or i'll not be notified about. << now red so probably you'll see it.

10 ? "RENPY"
20 GOTO 10

RUN

jadeon12
Newbie
Posts: 24
Joined: Sat Mar 05, 2016 6:17 am
Contact:

Re: help with screen to displays random messages

#9 Post by jadeon12 »

gas wrote:Ok let's simplify everything.
You want for the sphere to say something based on the mood value.

If you set mood=1 in a label, renpy put at 1 each time he reach again that label.
So, define it at first OUTSIDE the normal course of events.
Then use a list to link the various conditions to the variable. Is called INDEXING.
in python, each element of a list can be retrieved by referring his index number, counting FROM 0 ONWARD.

Code: Select all

default mood=0
default shade=["green","red","blue"]  # so, mood 0 = green, mood 1= red, mood 2= blue
label start:
    show screen kriball
label feast:
    menu:
        "Eat mango!":
            $ mood=0
            "You ate a juicy mango!"
        "Eat chili!":
            $ mood=1
            "You ate a fiery chili pepper!"
        "Eat alien food":
            $ mood=2
            "Something strange happened..."
    jump feast
screen kriball_button:
    imagebutton:
            idle "appear_idle.png"
            hover "appear_hover.png"
            action  [Show ("kriball_screen "), Hide("kriball_button")] align (.40,.90)

screen kriball_screen:
    modal True
    $ yourcolor=shade[mood]
    window:
        vbox:   
            label "Krystal Ball"           
            text " Digging in your pocket you take out the Krystal ball and peer inside for a message:"
            text ""
            text "the ball is giving a [yourcolor]ish pulsing glow"

With this logic it's very easy to accomplish what you want for.
Change the mood (the index) and retrieve the string.

If you find some other difficulty, ask for! That kriball seems fun!
Yes, I understood the coding, it and its working!

Thank you very much Gas!

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot]