Imagebutton custom path

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.
Message
Author
User avatar
Luxliev
Veteran
Posts: 242
Joined: Sat Feb 07, 2015 11:01 am
Soundcloud: Luxliev
Contact:

Imagebutton custom path

#1 Post by Luxliev »

Can anyone tell me if it's possible to add string variable as imagebutton path? What I have in mind:

Code: Select all

init:
    $ imagepath = "image.png"

label start:
    screen tempscreen:
        vbox:
            textbutton "temp button" action [Show("temp")]
            imagebutton "[imagepath]" action [Show("temp")]
Version above gives me following error:
File "game/script.rpy", line 11: expected a keyword argument, colon, or end of line.

EDIT: Update example code for clarity.
Last edited by Luxliev on Thu Sep 03, 2015 10:17 am, edited 3 times in total.
Newest classical cover: Advance Wars - Sami Theme: https://www.youtube.com/watch?v=657Jt7hJRVc

Forum with my music: http://luxliev.proboards.com/

User avatar
mobychan
Veteran
Posts: 275
Joined: Fri Apr 24, 2015 6:31 am
Projects: The Chosen - Sakura Pink & Gentian Blue
Organization: Foresoft
Location: Germany
Contact:

Re: Imagebutton custom path

#2 Post by mobychan »

I'm not sure, but I believe text- and imagebuttons don't work in a label

User avatar
Luxliev
Veteran
Posts: 242
Joined: Sat Feb 07, 2015 11:01 am
Soundcloud: Luxliev
Contact:

Re: Imagebutton custom path

#3 Post by Luxliev »

Oh that's just example don't worry about it I just wanted to show you what I want to achieve.
Newest classical cover: Advance Wars - Sami Theme: https://www.youtube.com/watch?v=657Jt7hJRVc

Forum with my music: http://luxliev.proboards.com/

User avatar
mobychan
Veteran
Posts: 275
Joined: Fri Apr 24, 2015 6:31 am
Projects: The Chosen - Sakura Pink & Gentian Blue
Organization: Foresoft
Location: Germany
Contact:

Re: Imagebutton custom path

#4 Post by mobychan »

it's still a screen under a label ;)

imagebutton needs idle and hover images:

Code: Select all

imagebutton:
    idle imagepath
    hover imagepath2
    action [Show("temp")]
you can write it in one line as well:

Code: Select all

imagebutton idle imagepath hover imagepath2 action [Show("temp")]
Looking into the documentation might help as well

User avatar
Luxliev
Veteran
Posts: 242
Joined: Sat Feb 07, 2015 11:01 am
Soundcloud: Luxliev
Contact:

Re: Imagebutton custom path

#5 Post by Luxliev »

Thanks! Works exactly as I want. This part of the code:

Code: Select all

imagebutton:
    idle imagepath
    hover imagepath2
    action [Show("temp")]
Give me expected statement error. But second example you gave works as it should.

Solved.
Newest classical cover: Advance Wars - Sami Theme: https://www.youtube.com/watch?v=657Jt7hJRVc

Forum with my music: http://luxliev.proboards.com/

User avatar
Luxliev
Veteran
Posts: 242
Joined: Sat Feb 07, 2015 11:01 am
Soundcloud: Luxliev
Contact:

Re: Imagebutton custom path

#6 Post by Luxliev »

EDIT: I've got new problem. When I use it in function it doesn't work. Code:

Code: Select all

init python:
    class tempclass:
        def __init__(self, name, portrait_idle, portrait_hover):
            self.n = name
            self.awpi = portrait_idle
            self.awph = portrait_hover

    path = None

    chosenbutton = None
    
    def load_images():
        button1i = p01g01
        button1h = p01g01

    path = tempclass("name", "gui/aw/awp/temp_awp_hover.png", "gui/aw/awp/temp_awp_idle.png")

    p01g01 = path

screen top_gui:
        hbox:
            screen temp_button:
                textbutton "Temp" action [Function(load_images), Show("temp_screen"), Hide("textbutton")] align (1.0,0.0)
    
screen army_screen:    
    add "gui/temp.png" align (0.75,0.125)
    modal True
    hbox align (1.0,0.0):
        textbutton "Close Window" action [ Hide("temp_screen"), Show("top_gui")]    
    vbox:
        align (0.0125,0.35)
        imagebutton idle button1i hover button1h action [SetVariable("chosenbutton", 1), Show("temptempscreen")]
NameError: name 'button1i' is not defined
Newest classical cover: Advance Wars - Sami Theme: https://www.youtube.com/watch?v=657Jt7hJRVc

Forum with my music: http://luxliev.proboards.com/

User avatar
orz
Regular
Posts: 126
Joined: Tue Apr 21, 2015 10:19 pm
Contact:

Re: Imagebutton custom path

#7 Post by orz »

Code: Select all

    def load_images():
        button1i = p01g01
        button1h = p01g01
I'm pretty sure this code isn't doing anything.

When you write a function, it creates its own scope, so to speak. So even if the function knew what p01g01 was, everything outside the function won't know what button1i is.

User avatar
Luxliev
Veteran
Posts: 242
Joined: Sat Feb 07, 2015 11:01 am
Soundcloud: Luxliev
Contact:

Re: Imagebutton custom path

#8 Post by Luxliev »

Oh right this is class should I copycopy it?
Newest classical cover: Advance Wars - Sami Theme: https://www.youtube.com/watch?v=657Jt7hJRVc

Forum with my music: http://luxliev.proboards.com/

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Imagebutton custom path

#9 Post by trooper6 »

Luxliev wrote:EDIT: I've got new problem. When I use it in function it doesn't work. Code:

Code: Select all

init python:
    class tempclass:
        def __init__(self, name, portrait_idle, portrait_hover):
            self.n = name
            self.awpi = portrait_idle
            self.awph = portrait_hover

    path = None

    chosenbutton = None
    
    def load_images():
        button1i = p01g01
        button1h = p01g01

    path = tempclass("name", "gui/aw/awp/temp_awp_hover.png", "gui/aw/awp/temp_awp_idle.png")

    p01g01 = path

screen top_gui:
        hbox:
            screen temp_button:
                textbutton "Temp" action [Function(load_images), Show("temp_screen"), Hide("textbutton")] align (1.0,0.0)
    
screen army_screen:    
    add "gui/temp.png" align (0.75,0.125)
    modal True
    hbox align (1.0,0.0):
        textbutton "Close Window" action [ Hide("temp_screen"), Show("top_gui")]    
    vbox:
        align (0.0125,0.35)
        imagebutton idle button1i hover button1h action [SetVariable("chosenbutton", 1), Show("temptempscreen")]
NameError: name 'button1i' is not defined
There is a lot of stuff going on in this code of yours. Not good stuff.
Don't put screens in a screen.
Also, let us back way up and away from the custom path question.
To the beginning.

What exactly to you want your screen to do? Because I bet there is a way to do it that has nothing to do with custom paths.
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

User avatar
orz
Regular
Posts: 126
Joined: Tue Apr 21, 2015 10:19 pm
Contact:

Re: Imagebutton custom path

#10 Post by orz »

Oh right this is class should I copycopy it?
That function isn't even in the class. And if it was, if you were wanting to use it like a class method or something like that, it'd require "self" as a parameter.

Also, what trooper6 said is correct - I'm pretty sure there's a much easier way of doing what you're trying to do.

User avatar
Luxliev
Veteran
Posts: 242
Joined: Sat Feb 07, 2015 11:01 am
Soundcloud: Luxliev
Contact:

Re: Imagebutton custom path

#11 Post by Luxliev »

trooper6 wrote:What exactly to you want your screen to do? Because I bet there is a way to do it that has nothing to do with custom paths.
Let's start with explaining why I did what I did.

Code: Select all

class tempclass:
        def __init__(self, name, portrait_idle, portrait_hover):
            self.n = name
            self.awpi = portrait_idle
            self.awph = portrait_hover
This class is actually more complicated. There is pretty much all info about player character in it and 2 of those lines highlited are responsible for portrait image path in game folder. I'll explain why.

Code: Select all

screen top_gui:
        hbox:
            screen temp_button:
                textbutton "Temp" action [Function(load_images), Show("temp_screen"), Hide("textbutton")] align (1.0,0.0)
Temp button is one of the buttons used to open menus. This one opens character screen. Here is also reason why I want to use custom paths for images. I want to create imagebutton with character portrait that then open one more screen with info about him. Because characters will come and go I want to always update current roster with load images function. Currently (36)

Code: Select all

    vbox:
        align (0.0125,0.35)
        imagebutton idle button1i hover button1h action [SetVariable("chosenbutton", 1), Show("temptempscreen")]
There will be 6 character slots at any time that's why I have placeholder variables that work as shortcut to specific character that is currently in the team.

Code: Select all

p01g01 = path
p01g02 = path
etc. p01 stands for player 1 and g01 is number of character (currently g06 highest). In this way any character that wil be linked to specific player will be seen when he clicks character button.
Newest classical cover: Advance Wars - Sami Theme: https://www.youtube.com/watch?v=657Jt7hJRVc

Forum with my music: http://luxliev.proboards.com/

User avatar
orz
Regular
Posts: 126
Joined: Tue Apr 21, 2015 10:19 pm
Contact:

Re: Imagebutton custom path

#12 Post by orz »

I think he was asking more in general terms.

Edit: Either way, here's a problem in the coding logic you have.

You instanced a class, and stored that instance in a variable.

Later, you try adding that variable (containing the instanced class) to two other variables.

You then try using those two other variables as they are for the "idle" and "hover". Which would point at the instanced class, not the fields on the class which actually contains the path to the image you wanted.

User avatar
Luxliev
Veteran
Posts: 242
Joined: Sat Feb 07, 2015 11:01 am
Soundcloud: Luxliev
Contact:

Re: Imagebutton custom path

#13 Post by Luxliev »

Thanks for detailed information. How can I fix the code to make it do what I want? I actually found similiar issue I had in other part of the code and bypassed it by using copy.copy

Code: Select all

init python:
    import copy
init:
    p01g01 = copy.copy(path)
Is this good idea or I should resolve this issue in some other way.
Newest classical cover: Advance Wars - Sami Theme: https://www.youtube.com/watch?v=657Jt7hJRVc

Forum with my music: http://luxliev.proboards.com/

User avatar
orz
Regular
Posts: 126
Joined: Tue Apr 21, 2015 10:19 pm
Contact:

Re: Imagebutton custom path

#14 Post by orz »

You should resolve the issue in some other way.

Also, using "temp" so many times as a name is very unreadable for anyone else -- it's hard to figure out the intent behind what you're trying to do with your code. It's fine for very temporary testing and when you're the only one reading it, but good variable and class naming will go a long way in expressing what's supposed to happen with your code.

Here's your same code with pointless stuff taken out and spelling errors fixed (the screen was named army_screen, and you were trying to call temp_screen). And you were passing the idle picture to your class in the positional slot as the hover, and vice versa.

Code: Select all

init python:
    class tempclass:
        def __init__(self, name, portrait_idle, portrait_hover):
            self.n = name
            self.awpi = portrait_idle
            self.awph = portrait_hover

    chosenbutton = None    
    path = tempclass("name", "gui/aw/awp/temp_awp_idle.png", "gui/aw/awp/temp_awp_hover.png")

    p01g01 = path

screen top_gui:
    textbutton "Temp" action [Show("army_screen"), Hide("top_gui")] align (1.0,0.0)
    
screen army_screen:    
    add "gui/temp.png" align (0.75,0.125)
    modal True
    hbox align (1.0,0.0):
        textbutton "Close Window" action [Hide("army_screen"), Show("top_gui")]    
    vbox:
        align (0.0125,0.35)
        imagebutton idle p01g01.awpi hover p01g01.awph action [SetVariable("chosenbutton", 1), Show("temptempscreen")]
Note, "imagebutton idle p01g01.awpi hover p01g01.awph".

It's more than fine to use a class's fields there, as long as the field, when evaluated, results in A) a displayable, or B) a string containing a path to an image.

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Imagebutton custom path

#15 Post by trooper6 »

Honestly, I would not do anything that you are currently doing at all. Your code is all over the place.

I have created a test project that does/has the following:
A screen on the top with three buttons: "Inventory", "Character", "Spells"
*Inventory and Spells don't do anything, but they are there to make the top screen look nice.
*The Character button pulls up a screen which lists image buttons for all the characters in the list it is passed (in this case the curr_roster of characters).
*Clicking on a character image button opens up another screen with info on the the character that is passed to that screen when you press the button.

This set up requires the Char class to include all of its image paths and info and whatever else is associated with that particular character.
This set up requires a list with all of the current character, which are appended or deleted as needed.

This is tested and works:

Code: Select all

# You can place the script of your game in this file.

# Declare images below this line, using the image statement.
# eg. image eileen happy = "eileen_happy.png"

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


init -1 python:
    class Char():
        def __init__(self, name, but_idle, but_hover, port, info):
            self.n = name
            self.awpi = but_idle
            self.awph = but_hover
            self.port = port
            self.info = info

screen top_gui():
    frame:
        hbox:
            textbutton "Inventory" action NullAction()
            textbutton "Characters" activate_sound 'click.wav' action If(renpy.get_screen("char_roster"), Hide("char_roster"), Show("char_roster", list=curr_roster))
            textbutton "Spells" action NullAction()
        
screen char_roster(list):
    frame:
        align (0.0, 0.5)
        xysize (400, 300)
        vbox:
            text "Character Roster"
            text "-----"
            spacing 10
            for c in list:
                imagebutton:
                    idle c.awpi
                    hover c.awph
                    focus_mask True 
                    activate_sound 'click.wav'
                    action If(renpy.get_screen("info_screen"), Hide("info_screen"), Show("info_screen", who=c))
        
screen info_screen(who):
    frame align (1.0, 0.5):
        vbox:
            text "[who.n]"
            add who.port
            text "-----"
            text "[who.info]"

default curr_roster = []
default d = Char("Diana", "dianab_idle", "dianab_hover", "dianaport", "Diana is the science officer.")
default l = Char("Lydia", "lydiab_idle", "lydiab_hover", "lydiaport", "Lydia is the security officer.")
default j = Char("Juliet", "julietb_idle", "julietb_hover", "julietport", "Juliet leada the resistance.")

# The game starts here.
label start:
    scene black
    show screen top_gui()
    "Rignt now, no one should be in the roster."
    $curr_roster.append(d)
    "Diana should be in the list."
    $curr_roster.append(l)
    "Diana and Lydia should be in the list."
    $curr_roster.append(j)
    "Diana, Lydia, and Juliet should be in the list."
    
    
    
I built a distribution of this project in case you want to run it and see what it is like in practice. The placement/style of the screens could, of course, be made nicer...I just put this together quickly.
Attachments
CurrentScratch-1.0-all.zip
(26.38 MiB) Downloaded 27 times
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

Post Reply

Who is online

Users browsing this forum: Ocelot