Python/imagebuttons for interactive characters/objects.

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
Janto
Newbie
Posts: 21
Joined: Wed Apr 11, 2012 10:48 am
Projects: Fire in the Night
Location: Florence, Italy
Contact:

Python/imagebuttons for interactive characters/objects.

#1 Post by Janto » Mon Feb 04, 2013 4:07 pm

Hi all, I'm working on a few short tests / demos of various Ren'Py elements that I'd like to use to make a more interactive VN, and I've begun to dig into screens and screen language. This is a bit of a complex request, so I'll try and make it as straightforward as possible.

I'm interested in having a system like the Professor Layton games, where the player can wander from location to location and click on characters to trigger a conversation with them. Because I want characters to be able to move from one location to another, and either overlap or have foreground objects overlap them, I think imagebuttons are the best (or only) solution. I have the basic code working after much crashing (as in, Ren'Py stopped responding when calling a label and had to be shut down) but I think it's time to look for expertise in learning to improve things.

Basically, I wonder what people suggest for setting up a bunch of imagebuttons that share a lot of similar properties, especially for characters which could have different expressions, different positions on different scenes, etc. Has anyone done something similar?

My crash, for the record. Here's the code I've used:

Code: Select all

screen button_1:
    modal False
    zorder 2    
    imagebutton:
        xpos 20 ypos 430
        auto "button_1_%s.png"
        action [SetVariable("button_1_press", True), Hide("button_1"), Jump("button_1_result")] 
        
screen button_2: 
    modal False
    zorder 0
    imagebutton:
        xpos 40 ypos 300
        auto "button_2_%s.png"
        action [SetVariable("button_2_press", True), Hide("button_2"), Jump("button_2_result")]
        
label image_button_exp:
    
label room:
    show imagemap ground
    
    if not button_1_press:
        show screen button_1
    if not button_2_press:
        show screen button_2  
        
    # whatever else happens in this room
    
    $ renpy.pause()
    jump room

label button_1_result:    
    e "You pressed button 1!"
    return
    
label button_2_result:
    e "You pressed button 2!"
    return
This seemed to be the critical part:

Code: Select all

 $ renpy.pause()
Without that, the call to image_button_exp just killed the game dead.

User avatar
KimiYoriBaka
Miko-Class Veteran
Posts: 636
Joined: Thu May 14, 2009 8:15 pm
Projects: Castle of Arhannia
Contact:

Re: Python/imagebuttons for interactive characters/objects.

#2 Post by KimiYoriBaka » Mon Feb 04, 2013 7:20 pm

Has anyone done something similar?
I really really wish someone would invent a system for checking new posts in question sections of forums that can find questions like this and intervene with a list of posts before the question gets posted. I've noticed it's really hard to find the answers through searching even for the most covered topics.

and yes, a couple people, including me, have done this. if you're capable of working with lists, I would suggest you use one screen that reads your imagebuttons from a list. that way you don't have to deal with having lots and lots of variations crowding your code.

how it would be done depends on your game, but the idea is something like this:

Code: Select all

screen buttons:
    modal False
    for b in buttonlist:
        imagebutton:
            xpos b[0] ypos b[1]
            auto (b[2] + "_%s.png")
            action b[3]

label start:
    buttonlist = [[20, 430, "button_1", [SetVariable("button_1_press", True), Hide("button_1"), Jump("button_1_result")]],
               [40, 300, "button_2", [SetVariable("button_2_press", True), Hide("button_2"), Jump("button_2_result")]]]
    
also, the reason your game is having a problem is because you used show screen instead of call screen. this is fine (I prefer doing it that way myself) but you need to know that show screen doesn't pause the game. it's just meant for keeping the buttons on screen for as long as necessary. if you don't have either a say statement (aka dialogue) or a pause, it will keep going until it reaches one. in the case of your program, not having the pause meant that it only processed the jump which formed an infinite loop.

good luck with your testing and feel free to ask about whatever comes up.

Janto
Newbie
Posts: 21
Joined: Wed Apr 11, 2012 10:48 am
Projects: Fire in the Night
Location: Florence, Italy
Contact:

Re: Python/imagebuttons for interactive characters/objects.

#3 Post by Janto » Tue Feb 05, 2013 6:31 am

Thanks Kimi, I'll experiment with this, or probably get my coding partner to experiment with it.

(Yes, some sort of smart filtering for similar topics while writing would be nice, but part of it is always probably going to be a problem of new users not having the terminology down yet.)

Janto
Newbie
Posts: 21
Joined: Wed Apr 11, 2012 10:48 am
Projects: Fire in the Night
Location: Florence, Italy
Contact:

Re: Python/imagebuttons for interactive characters/objects.

#4 Post by Janto » Fri Feb 08, 2013 7:44 am

Just a quick note/query for anyone else following this topic. The code Kimi gave me works fine, except the list itself has to be put into a python block, which makes sense, but isn't obvious from the example. (Or I'm just that naive.)

So:

Code: Select all

label start:
    buttonlist = [[20, 430, "button_1", [SetVariable("button_1_press", True), Hide("button_1"), Jump("button_1_result")]],
               [40, 300, "button_2", [SetVariable("button_2_press", True), Hide("button_2"), Jump("button_2_result")]]]
Has to be:

Code: Select all

init python:
    buttonlist = [[20, 430, "button_1", [SetVariable("button_1_press", True), Hide("button_1"), Jump("button_1_result")]],
                  [40, 300, "button_2", [SetVariable("button_2_press", True), Hide("button_2"), Jump("button_2_result")]]]
I'll post up the full sample scene here with its code once it's done, just so people can see it in action.

User avatar
saguaro
Miko-Class Veteran
Posts: 560
Joined: Sun Feb 12, 2012 9:17 am
Completed: Locked-In, Sunrise, The Censor
Organization: Lucky Special Games
itch: saguarofoo
Location: USA
Contact:

Re: Python/imagebuttons for interactive characters/objects.

#5 Post by saguaro » Fri Feb 08, 2013 9:10 am

You can put it in a label, you just have to put a $ (and make sure buttonlist is defined before the screen is called).

Code: Select all

label start:
    $ buttonlist = [[20, 430, "button_1", [SetVariable("button_1_press", True), Hide("button_1"), Jump("button_1_result")]],
                  [40, 300, "button_2", [SetVariable("button_2_press", True), Hide("button_2"), Jump("button_2_result")]]]

Janto
Newbie
Posts: 21
Joined: Wed Apr 11, 2012 10:48 am
Projects: Fire in the Night
Location: Florence, Italy
Contact:

Re: Python/imagebuttons for interactive characters/objects.

#6 Post by Janto » Fri Feb 08, 2013 12:01 pm

Of course, I was silly in saying it HAD to be a python block, didn't experiment with just marking it with an $ for a single line of python. (Thanks for clarifying)

Post Reply

Who is online

Users browsing this forum: Bing [Bot]