Page 1 of 1

[solved] Using variables to alter character sprite

Posted: Mon Jan 07, 2013 12:35 am
by jayel
Question One: Can I use a variable to switch between character images? In this instance, I want the string in the variable hero_color to be used to switch the color of the charabase sprite. So if hero_color = "pink", then I want "show charabase hero_color" to be the same as "show charabase pink".

In the example below, which doesn't work but might illustrate my faulty method, I get the non-fatal error

Code: Select all

Image 'charabase' can't take parameters 'white'. (Perhaps you got the name wrong?)

I appreciate Ren'py's polite manner but it does little to soothe my frustration. :)


Script

Code: Select all

# You can place the script of your game in this file.
init:
    $ hero_color = "white"

# Declare images below this line, using the image statement.

image charabase = "CharaBase.png"
image charabase white = "CharaBase.png"
image charabase violet = im.Recolor("CharaBase.png", 183, 79, 207)
image charabase indigo = im.Recolor("CharaBase.png", 115, 79, 207)
image charabase pink = im.Recolor("CharaBase.png", 207, 79, 128)

# Declare characters used by this game.
define t = Character('tutor', color="#c8ffc8")


# The game starts here.
label start:

show charabase [hero_color] at truecenter

t "This is the base character sprite."
t "Currently the base color is [hero_color]."
t "Press the buttons at right to change the skin color."
    
call screen hero_custom:

return

Question the Second: I want to alter the variable hero_color using buttons and have the sprite update immediately. (I've borrowed the main menu code for now). Can I a) call my charabase sprite on my hero_custom screen or b) have it updated in the background behind the screen?


Screens

Code: Select all

##############################################################################
# Hero Chara Customization
#
# Screen that's used to display and customize the Hero Character

screen hero_custom:

    # This ensures that any other menu screen is replaced.  
    # Which I don't want - I want the previous BG to be seen (and affected)
    # So commented out.
    ## tag menu

    # The background of the main menu.  Commented out to be transparent
    ##window:
    ##    style "mm_root"

    # The main menu buttons.
    frame:
        style_group "mm"
        xalign .98
        yalign .98

        has vbox

        textbutton _("Pink") action SetVariable("hero_color", "pink")
        textbutton _("Violet") action SetVariable("hero_color", "violet")
        textbutton _("Indigo") action SetVariable("hero_color", "indigo")
        textbutton _("Done") action Return
        
        
init -2 python:

    # Make all the main menu buttons be the same size.
    style.mm_button.size_group = "mm"

Re: Using variables to alter character sprite?

Posted: Mon Jan 07, 2013 4:13 am
by Ayutac
Question the first: I think this is what you are looking for.
Question the second: b), as you can see in the link above
Sorry for that, I can't read.

Okay, instead of

Code: Select all

show charabase [hero_color] at truecenter
you could use

Code: Select all

$ renpy.show("charabase " + hero_color, at_list=[truecenter])
The reason your code doesn't work is that in Ren'Py language the image to show is not interpreted as string but as variable, thus you can't use "[hero_color]". But with Python you can build your variable together.

For the second question: I don't know, for now you can try since question 1 works. But if it wouldn't, you could try redrawing with renpy.redraw and renpy.curry. I'll will try this out in some minutes, just let me do my laundry quick.

EDIT: With this post we find the idea to do a proper redraw from a screen (or calling a function in general), but as I said, I can't read. So, you actually want to change the image but keep it in its position and all. I quote from here
If an image with the same image tag is already showing on the layer, the new image replaces it.
So, in theory calling

Code: Select all

$ renpy.show("charabase " + hero_color)
would do the trick, now we just have to get this into the screen actions. To do more than one action, you write

Code: Select all

action [method1, method2, ..., lastMethod]
so you give 'action' a list.


But as I know me and this board there is probably an easier way to achieve all this <_<

EDIT: I knew it T_T

Re: Using variables to alter character sprite?

Posted: Mon Jan 07, 2013 4:53 am
by AxemRed
ConditionSwitch should solve both problems.

Re: Using variables to alter character sprite?

Posted: Mon Jan 07, 2013 12:13 pm
by jayel
Thank you both! :D I'll work with ConditionSwitch tonight when I get home from work. And I'll play around with the renpy.show/action code too, because there are never too many workarounds. I appreciate the step-by-step, Ayutac!

[Solved] Using variables to alter character sprite

Posted: Tue Jan 08, 2013 3:22 am
by jayel
WORKING EXAMPLE

Purpose: To recolor a sprite based on a variable determined by the User. This code could be used in dress-up games or to customize a hero's hair/skin/eye color.

The base sprite used in testing was black and white. More complex looks could be obtained by coloring the base and the lineart separately - lineart could be colored a different but complimentary color or the lineart could be lightened if the chosen base color is very dark.


SCRIPT

Code: Select all

init:
    image charabase color = ConditionSwitch (
        "hero_color == 'white'", "CharaBase.png",
        "hero_color == 'pink'", im.Recolor("CharaBase.png", 207, 79, 128),
        "hero_color == 'violet'", im.Recolor("CharaBase.png", 183, 79, 207),
        "hero_color == 'indigo'", im.Recolor("CharaBase.png", 115, 79, 207),
                )
    $ hero_color = "white"

# Declare images below this line, using the image statement.
image charabase = "CharaBase.png"

# Declare characters used by this game.
define t = Character('tutor', color="#c8ffc8")


# The game starts here.
label start:

# Showing the default sprite and displaying the default value of the variable "hero_color".
t "This is the base character sprite."
t "Currently the base color is [hero_color]."
t "Press the buttons at right to change the skin color."

# Calling the screen code for the customization options
call screen hero_custom:

# Displaying the current value of the variable "hero_color".
t "The color is currently [hero_color]."

return
SCREENS

Code: Select all

##############################################################################
# Chara Customization
#
# Screen that's used to display and customize the Hero Character

screen hero_custom:

    # This ensures that any other menu screen is replaced.  
    # Which I don't want - I want the previous BG to be seen (and affected)
    # So commented out.
    ## tag menu

    # The background of the main menu.  Commented out to be transparent
    ##window:
    ##    style "mm_root"

    # The buttons, borrowed from Main Menu default script.
    frame:
        style_group "mm"
        xalign .98
        yalign .98

        has vbox

        textbutton _("Pink") action SetVariable("hero_color", "pink")
        textbutton _("Violet") action SetVariable("hero_color", "violet")
        textbutton _("Indigo") action SetVariable("hero_color", "indigo")
        textbutton _("Done") action Return
        
        
init -2 python:

    # Make all the main menu buttons be the same size.
    style.mm_button.size_group = "mm"