[Solved!] Name Generator generating twice?

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
peach_light
Newbie
Posts: 3
Joined: Wed Apr 10, 2024 2:45 pm
Contact:

[Solved!] Name Generator generating twice?

#1 Post by peach_light »

Hello wonderful friends of the LemmaSoft Forum!!
I come bearing a request for assistance regarding my code.
The name gen I have works great! Managed to get it to fill the input field, but after jumping to the next scene, it generates a whole new name >:(
Here's a preview of the code in action:

https://www.youtube.com/watch?v=S5igAxiaDZ4

As well as the code itself:

Code: Select all

define mc = DynamicCharacter("mc_name")

init python:
    class GENERATOR:
        def randommc(self):
            if random_mc:
                mc = renpy.random.choice(names)
                store.mc_name = mc
   
init:
    $ random_mc = False
    $ names = ["Eisley","Romi","Arianwen","Elvira","Belphoebe","Etol","Aralueni","Morag","Ottoline","Velnor","Euphoria","Envy","Naiad","Hauntice","Malice","Aquina","Sprite","Revelation","Ethereal","Priam"]
    $ mc_name = ""




transform dice:
    zoom 0.1





label start:
python:
    gen = GENERATOR()
        

$ random_mc = False
screen mc_naming:
    hbox:
        align(0.5,0.5)
        text "Character name:" size 26 color "#000000" yalign 0.5 outlines[(absolute(2),"#ffffff", 0,0)]
        frame:
            background "#ffffff"
            yalign 0.5
            input value VariableInputValue("mc_name") minwidth 150 length 24
            

    textbutton "Start" xalign 0.3 yalign 0.5 action Jump("greeting")   
    imagebutton idle "gui/buttons/dice.png" hover "gui/buttons/dice_hover.png" at dice: 
        align (0.9,0.1) 
        action [SetVariable("random_mc",True)]
         

    if random_mc:
        $ gen.randommc()
        
    if mc_name == "":
        $ mc_name = "Greg"
         






call screen mc_naming  



label greeting:
    

    
    mc "hello!"
    mc "my name is [mc_name]"
    return


    
    
return


All i'm asking is for the randomized name in the field to continue into the actual game instead of having to pray it generates the same name twice...

Thank you in advance <3




Solved by Philiat! TYSM <3
philat wrote: Thu Apr 11, 2024 2:48 am The whole thing seems wildly overengineered, tbh.

Code: Select all

define mc = DynamicCharacter("mc_name")
default names = ["Eisley","Romi","Arianwen","Elvira","Belphoebe","Etol","Aralueni","Morag","Ottoline","Velnor","Euphoria","Envy","Naiad","Hauntice","Malice","Aquina","Sprite","Revelation","Ethereal","Priam"]
default mc_name = ""

init python:
    def generate_random_mc():
        store.mc_name = renpy.random.choice(names)

screen mc_naming:
    vbox:
        hbox:
            text "Character name:" 
            input value VariableInputValue("mc_name") minwidth 150 length 24

        textbutton "Start" action Return() # due to label flow, this falls through to label greeting anyway
        textbutton "Generate" action [SetVariable("mc_name", renpy.random.choice(names))] # given that your function only does one thing, simply using SetVariable directly seems far more efficient
        textbutton "Generate (Function)" action Function(generate_random_mc) # example of using a function if you intend to extend the function further (I really don't see the point of the generator class)

label start:
    call screen mc_naming  

label greeting:
    mc "hello!"
    mc "my name is [mc_name]"
    return
Last edited by peach_light on Thu Apr 11, 2024 1:42 pm, edited 2 times in total.

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2410
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Name Generator generating twice?

#2 Post by Ocelot »

Screens should not have any side effects in their code. Screen code is run multiple times during prediction and every time screen refreshes (basically, when anything happens) and can be executed with any parameters, including those you never tried to pass.

In short, move random name generation and empty name replacement outside of the screen, after you call it.
Even better, instead of SetVariable("random_mc",True), use Function(gen.randommc)
< < insert Rick Cook quote here > >

User avatar
peach_light
Newbie
Posts: 3
Joined: Wed Apr 10, 2024 2:45 pm
Contact:

Re: Name Generator generating twice?

#3 Post by peach_light »

Ocelot wrote: Wed Apr 10, 2024 4:42 pm Screens should not have any side effects in their code. Screen code is run multiple times during prediction and every time screen refreshes (basically, when anything happens) and can be executed with any parameters, including those you never tried to pass.

In short, move random name generation and empty name replacement outside of the screen, after you call it.
Even better, instead of SetVariable("random_mc",True), use Function(gen.randommc)

Unfortunately, doing this has caused the generator to not really do anything at all? Hitting the randomize(dice) button results in nothing happening either on the screen or once the game jumps to greeting.

Only if I put the gen.randommc in the greeting label does it generate anything, but only after the start button is pressed.

What I was intending was to hopefully have the generator provide a name from the screen like in the video I linked and perpetuate that into the game.

also for some reason Function(gen.randommc) doesn't really function as it should. I initially had the code using the function string and it didn't really do anything :\

Thus far, I have a super chaotic workaround that looks like

Code: Select all

screen mc_naming:
    hbox:
        align(0.5,0.5)
        text "Character name:" size 26 color "#000000" yalign 0.5 outlines[(absolute(2),"#ffffff", 0,0)]
        frame:
            background "#ffffff"
            yalign 0.5
            input value VariableInputValue("mc_name") minwidth 150 length 24
            

    textbutton "Start" xalign 0.3 yalign 0.5 action Jump("greeting")    
    imagebutton idle "gui/buttons/dice.png" hover "gui/buttons/dice_hover.png" unhovered SetVariable("random_mc",False) at dice: 
        align (0.9,0.1) 
        action SetVariable("random_mc",True)
        
        
         
##just in case unhover is being silly
    textbutton "Confirm!" xalign 0.3 yalign 0.3 action SetVariable("random_mc",False)
    if random_mc:
        $ gen.randommc()
hopefully theres an easy and not confusing way that is invisible to my noob brain lol

tysm :)
[/color]

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

Re: Name Generator generating twice?

#4 Post by Imperf3kt »

Looking at your original code, you have if randommc, a variable which starts as false in your code. This is what seems to be used to check whether to generate a name or not in your class.

Then in your script, you check if this variable is populated, and run the same code again
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

philat
Eileen-Class Veteran
Posts: 1920
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Name Generator generating twice?

#5 Post by philat »

The whole thing seems wildly overengineered, tbh.

Code: Select all

define mc = DynamicCharacter("mc_name")
default names = ["Eisley","Romi","Arianwen","Elvira","Belphoebe","Etol","Aralueni","Morag","Ottoline","Velnor","Euphoria","Envy","Naiad","Hauntice","Malice","Aquina","Sprite","Revelation","Ethereal","Priam"]
default mc_name = ""

init python:
    def generate_random_mc():
        store.mc_name = renpy.random.choice(names)

screen mc_naming:
    vbox:
        hbox:
            text "Character name:" 
            input value VariableInputValue("mc_name") minwidth 150 length 24

        textbutton "Start" action Return() # due to label flow, this falls through to label greeting anyway
        textbutton "Generate" action [SetVariable("mc_name", renpy.random.choice(names))] # given that your function only does one thing, simply using SetVariable directly seems far more efficient
        textbutton "Generate (Function)" action Function(generate_random_mc) # example of using a function if you intend to extend the function further (I really don't see the point of the generator class)

label start:
    call screen mc_naming  

label greeting:
    mc "hello!"
    mc "my name is [mc_name]"
    return

User avatar
peach_light
Newbie
Posts: 3
Joined: Wed Apr 10, 2024 2:45 pm
Contact:

Re: Name Generator generating twice?

#6 Post by peach_light »

philat wrote: Thu Apr 11, 2024 2:48 am The whole thing seems wildly overengineered, tbh.

Code: Select all

define mc = DynamicCharacter("mc_name")
default names = ["Eisley","Romi","Arianwen","Elvira","Belphoebe","Etol","Aralueni","Morag","Ottoline","Velnor","Euphoria","Envy","Naiad","Hauntice","Malice","Aquina","Sprite","Revelation","Ethereal","Priam"]
default mc_name = ""

init python:
    def generate_random_mc():
        store.mc_name = renpy.random.choice(names)

screen mc_naming:
    vbox:
        hbox:
            text "Character name:" 
            input value VariableInputValue("mc_name") minwidth 150 length 24

        textbutton "Start" action Return() # due to label flow, this falls through to label greeting anyway
        textbutton "Generate" action [SetVariable("mc_name", renpy.random.choice(names))] # given that your function only does one thing, simply using SetVariable directly seems far more efficient
        textbutton "Generate (Function)" action Function(generate_random_mc) # example of using a function if you intend to extend the function further (I really don't see the point of the generator class)

label start:
    call screen mc_naming  

label greeting:
    mc "hello!"
    mc "my name is [mc_name]"
    return

This has genuinely saved me !!! Thank you!!!!!!
Give it to the newbie to overcomplicate something so simple ahaha :lol:
Thank you again !!!!

Post Reply

Who is online

Users browsing this forum: Bing [Bot]