no idea how to use this code from cookbook :?

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
glithch
Regular
Posts: 93
Joined: Sat Jul 23, 2016 5:32 am
Contact:

no idea how to use this code from cookbook :?

#1 Post by glithch »

ok so for my bf's birthday i decided to make him a short game about our relationship and put a dress up sequence at the beginning since hes so fashion.
i found an awesome code in the cookbook changed it up just the slightest and the problem is i cant figure out how to keep going.
heres the link to the code: viewtopic.php?f=51&t=14559
my problem is that i would like the sprite to be able to emote, and someone in the topic actually posted about how to do that but i have no idea how to read this.
pyopyon wrote:Yep! I asked this question earlier in another thread, so I'll leave it here.
qirien wrote:OK, this is actually simpler than I though, but involves using python, not Ren'Py. Unfortunately, we can't do what Asceai suggested because we are using a DynamicDisplayable, and im.FactorScale only works on images...

But, what you can do is add the following function in your code (I put it right before the draw_char):

Code: Select all

    def get_expression():
        if (pov_expr == "default"):
            return "default.png"
        else:
            return "%s.png"%pov_expr
Now we need to have your draw_char functions call this one, like this:

Code: Select all

    def draw_char_side(st, at): # same thing as above, just scaled and positioned for the sideimage; there's probably more elegant solution than this...

        return LiveComposite(
            (384, 674),
            (0, 375), im.FactorScale("base.png", .45, .45),
            (0, 375), im.FactorScale("skin%d.png"%skin, .45, .45),
            (0, 375), im.FactorScale(get_expression(), .45, .45),
            (0, 375), im.FactorScale("eyes%d.png"%eyes, .45, .45),
            (0, 375), im.FactorScale("other%d.png"%other, .45, .45),
            (0, 375), im.FactorScale("pants%d.png"%pants, .45, .45),
            (0, 375), im.FactorScale("shirt%d.png"%shirt, .45, .45),            
            (0, 375), im.FactorScale("jacket%d.png"%jacket, .45, .45),
            (0, 375), im.FactorScale("hair%d.png"%hair, .45, .45)
            ),.1
        
Now the only thing left to do is modify pov_expr whenever we want the character to change their expression. For example, in your code, you would put something like this:

Code: Select all

    # Expression One
    $ pov_expr = "frown"
    char "Seriously, I'm super dissatisfied right now!"
    char "Go get me chocolate!"

    # Expression Two
    $ pov_expr = "happy"
    char "Aww- you got me chocolate?"
    char "You're so nice!! I didn't expect it at all!"
    
    # Expression Three
    $ pov_expr = "blawk"    
    char "W-what do you mean you got it because I was cute?!"
    char "Don't joke so much!"
    
Basically, whatever filename you want it to use for the expression, set pov_expr to that without the ".png".
this is how i TRY to use the code and the expressions dont work

Code: Select all

init python:
    dressup_button_show = False
    hair, glasses, tie, shirt, pants = 1, 1, 1, 1, 1 # default dressup items
    hair_styles_num, glasses_styles_num, tie_styles_num, shirt_styles_num, pants_styles_num = 7, 4, 4, 3, 3 # number of styles (files) for each dressup item
    # define images as:
    # "base.png" for the base image
    # "hair1.png", "hair2.png", "hair3.png", ... - start with 1 and end with (hair_styles_num)
    # "glasses1.png", "glasses2.png", "glasses3.png", ... "tie1.png", "tie2.png", ... "shirt1.png", "shirt2.png", ... "shirt1.png", "pants2.png", ...
    def get_expression():
        if (pov_expr == "default"):
            return "default.png"
        else:
            return "%s.png"%pov_expr

    def draw_char(st, at): # combine the dressup items into one displayable
        return LiveComposite(
            (361, 702), # image size
            (0, 0), "base.png",
            (0, 0), "glasses%d.png"%glasses, # (0, 0) is the position of a dressup item. Because these images are saved with spacing around them, we don't need to position them.
            (0, 0), "hair%d.png"%hair,
            (0, 0), "shirt%d.png"%shirt,
            (0, 0), "tie%d.png"%tie,
            (0, 0), "pants%d.png"%pants
            ),.1

    def draw_char_side(st, at): # same thing as above, just scaled and positioned for the sideimage; there's probably more elegant solution than this...
        return LiveComposite(
            (361, 702),
            (10, 550), im.FactorScale("base.png", .45, .45),
            (0, 375), im.FactorScale(get_expression(), .45, .45),
            (10, 550), im.FactorScale("glasses%d.png"%glasses, .45, .45),
            (10, 550), im.FactorScale("hair%d.png"%hair, .45, .45),
            (10, 550), im.FactorScale("shirt%d.png"%shirt, .45, .45),
            (10, 550), im.FactorScale("tie%d.png"%tie, .45, .45),            
            (10, 550), im.FactorScale("pants%d.png"%pants, .45, .45)
            ),.1

init:
    image wojo = DynamicDisplayable(draw_char) # using DynamicDisplayable ensures that any changes are visible immedietly
    $ wojo = Character('Wojtek', color="#c8ffc8", window_left_padding=180)

label start:
   "It's a beautiful day when you finally wake up at almost 2PM."
   "The sun is peeking into your room and birds are signing."
   "Your plan for the day is meeting your girlfriend and actually spending time outside for once."
   "You better hurry as your head is a literal bird's nest and you should be meeting her in an hour."
   show wojo
   wojo "dang i gotta do something with my hair."
   show screen dressup_button
   $ dressup_button_show = True
label cont:
    wojo "La, la, la!"
    jump um

screen dressup_button: # a button to call the dressup game
    if dressup_button_show:
        vbox xalign 0.01 yalign 0.01:
            textbutton "Change look." action ui.callsinnewcontext("dressup")

label dressup:
    show wojo
    python:
        # display the arrows for changing the dress:
        y = 50
        ui.imagebutton("arrowL.png", "arrowL.png", clicked=ui.returns("hairL"), ypos=y, xpos=400)
        ui.imagebutton("arrowR.png", "arrowR.png", clicked=ui.returns("hairR"), ypos=y, xpos=800)
        y += 80
        ui.imagebutton("arrowL.png", "arrowL.png", clicked=ui.returns("glassesL"), ypos=y, xpos=400)
        ui.imagebutton("arrowR.png", "arrowR.png", clicked=ui.returns("glassesR"), ypos=y, xpos=800)
        y += 80
        ui.imagebutton("arrowL.png", "arrowL.png", clicked=ui.returns("tieL"), ypos=y, xpos=400)
        ui.imagebutton("arrowR.png", "arrowR.png", clicked=ui.returns("tieR"), ypos=y, xpos=800)
        y += 80
        ui.imagebutton("arrowL.png", "arrowL.png", clicked=ui.returns("shirtL"), ypos=y+80, xpos=400)
        ui.imagebutton("arrowR.png", "arrowR.png", clicked=ui.returns("shirtR"), ypos=y+80, xpos=800)
        y += 80
        ui.imagebutton("arrowL.png", "arrowL.png", clicked=ui.returns("pantsL"), ypos=y+160, xpos=400)
        ui.imagebutton("arrowR.png", "arrowR.png", clicked=ui.returns("pantsR"), ypos=y+160, xpos=800)
        ui.textbutton("Return", clicked=ui.returns("goback")) # image button version: ui.imagebutton("return.png", "return_hover.png", clicked=ui.returns("goback"), ypos=0, xpos=0)
        
    $ picked = ui.interact()
    # based on the selection, we increase or decrease the index of the appropriate dress up item
    if picked == "hairL":
        $ hair -= 1 # previous hair
    if picked == "hairR":
        $ hair += 1 # next hair
    if hair < 1: # making sure we don't get out of index range (index 0 is not allowed)
        $ hair = hair_styles_num
    if hair > hair_styles_num: # making sure we don't get out of index range (index musn't be bigger than hair_styles_num)
        $ hair = 1

    if picked == "glassesL":
        $ glasses -= 1
    if picked == "glassesR":
        $ glasses += 1
    if glasses < 1:
        $ glasses = glasses_styles_num
    if glasses > glasses_styles_num:
        $ glasses = 1

    if picked == "tieL":
        $ tie -= 1
    if picked == "tieR":
        $ tie += 1
    if tie < 1:
        $ tie = tie_styles_num
    if tie > tie_styles_num:
        $ tie = 1

    if picked == "shirtL":
        $ shirt -= 1
    if picked == "shirtR":
        $ shirt += 1
    if shirt < 1:
        $ shirt = shirt_styles_num
    if shirt > shirt_styles_num:
        $ shirt = 1

    if picked == "pantsL":
        $ pants -= 1
    if picked == "pantsR":
        $ pants += 1
    if pants < 1:
        $ pants = pants_styles_num
    if pants > pants_styles_num:
        $ pants = 1
        
    if picked == "goback":
        return    
    jump dressup # we don't want to return on every click, this jump loops until we click on "back" button
label um:
    "hope this works"
    $ pov_expr = "3"
    wojo "god darn it"
    $ pov_expr = "angry"
the faces i make for the expressions dont show up at all and i stay with a blank face.
those are the faces i tried making
Image

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: no idea how to use this code from cookbook :?

#2 Post by Remix »

Are you using the main (biggish) composite picture or the scaled down side image?

I ask because I notice the main one does not show expressions...

Try changing:
image wojo = DynamicDisplayable(draw_char) # using DynamicDisplayable ensures that any changes are visible immedietly
to
image wojo = DynamicDisplayable(draw_char_side) # using DynamicDisplayable ensures that any changes are visible immediately
as a quick test.

If that shows some expression, just swap back and tweak the draw_char displayable to include a line along the lines of:
(0, 0), get_expression(),

Hope that made sense.
Basically look through the draw_char definition and you will see which images it includes... smuggle the variable get_expression code into there.
Frameworks & Scriptlets:

glithch
Regular
Posts: 93
Joined: Sat Jul 23, 2016 5:32 am
Contact:

Re: no idea how to use this code from cookbook :?

#3 Post by glithch »

Remix wrote:Are you using the main (biggish) composite picture or the scaled down side image?

I ask because I notice the main one does not show expressions...

Try changing:
image wojo = DynamicDisplayable(draw_char) # using DynamicDisplayable ensures that any changes are visible immedietly
to
image wojo = DynamicDisplayable(draw_char_side) # using DynamicDisplayable ensures that any changes are visible immediately
as a quick test.

If that shows some expression, just swap back and tweak the draw_char displayable to include a line along the lines of:
(0, 0), get_expression(),

Hope that made sense.
Basically look through the draw_char definition and you will see which images it includes... smuggle the variable get_expression code into there.
oh yeah i think my problem was that i forgot i deleted the side image from the script :x i dont want to use it since the chara looks weird scaled like that. could i use expressions on the big sprite at all?

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: no idea how to use this code from cookbook :?

#4 Post by Remix »

Yes, just slip the (0, 0), get_expression(), bit into the big sprite code similar to how the scaled one does it... just without the im.FactorScale shrinky code...

Code: Select all

    def draw_char(st, at): # combine the dressup items into one displayable
        return LiveComposite(
            (361, 702), # image size
            (0, 0), "base.png",
            (0, 0), get_expression(), # Here should be good <-------
            (0, 0), "glasses%d.png"%glasses,
            (0, 0), "hair%d.png"%hair,
            (0, 0), "shirt%d.png"%shirt,
            (0, 0), "tie%d.png"%tie,
            (0, 0), "pants%d.png"%pants
            ),.1
Frameworks & Scriptlets:

glithch
Regular
Posts: 93
Joined: Sat Jul 23, 2016 5:32 am
Contact:

Re: no idea how to use this code from cookbook :?

#5 Post by glithch »

Remix wrote:Yes, just slip the (0, 0), get_expression(), bit into the big sprite code similar to how the scaled one does it... just without the im.FactorScale shrinky code...

Code: Select all

    def draw_char(st, at): # combine the dressup items into one displayable
        return LiveComposite(
            (361, 702), # image size
            (0, 0), "base.png",
            (0, 0), get_expression(), # Here should be good <-------
            (0, 0), "glasses%d.png"%glasses,
            (0, 0), "hair%d.png"%hair,
            (0, 0), "shirt%d.png"%shirt,
            (0, 0), "tie%d.png"%tie,
            (0, 0), "pants%d.png"%pants
            ),.1
now i get this

Code: Select all

While running game code:
  File "game/script.rpy", line 131, in script
    label um:
  File "game/script.rpy", line 19, in draw_char
    (0, 0), get_expression(),
  File "game/script.rpy", line 10, in get_expression
    if (pov_expr == "default"):
NameError: global name 'pov_expr' is not defined
:O

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: no idea how to use this code from cookbook :?

#6 Post by Remix »

I presume you have not set a default value for it anywhere...

Just put:

default pov_expr = "default"

Preferably near the start of the script, just make sure it is outside any python or label block/indentation
Frameworks & Scriptlets:

glithch
Regular
Posts: 93
Joined: Sat Jul 23, 2016 5:32 am
Contact:

Re: no idea how to use this code from cookbook :?

#7 Post by glithch »

Remix wrote:I presume you have not set a default value for it anywhere...

Just put:

default pov_expr = "default"

Preferably near the start of the script, just make sure it is outside any python or label block/indentation
i put it before def get expression and i get this

Code: Select all

I'm sorry, but errors were detected in your script. Please correct the
errors listed below, and try again.


File "game/script.rpy", line 9: invalid syntax
    default pov_expr = "default"
                    ^
    

Ren'Py Version: Ren'Py 6.99.12.1.2012

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: no idea how to use this code from cookbook :?

#8 Post by Remix »

Re-read what I wrote... especially the:

make sure it is outside any python or label block

bit

If confused, just put it on the first line of the file, un-indented, no spaces before the first letter, no tabs, no anything
Frameworks & Scriptlets:

glithch
Regular
Posts: 93
Joined: Sat Jul 23, 2016 5:32 am
Contact:

Re: no idea how to use this code from cookbook :?

#9 Post by glithch »

Remix wrote:Re-read what I wrote... especially the:

make sure it is outside any python or label block

bit

If confused, just put it on the first line of the file, un-indented, no spaces before the first letter, no tabs, no anything
ahhh so sorry, ive never put anything sooo outside of everything and i saw everything else defined at the begining so i thought that was what you meant :X thank you so much for your help! i know my problems were very nooby lol

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot]