Map navigation with imagebuttons (I need a suggestion)

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
tcassat
Regular
Posts: 33
Joined: Tue Oct 03, 2017 7:32 pm
Contact:

Map navigation with imagebuttons (I need a suggestion)

#1 Post by tcassat »

Hello! Firstly, sorry for the question not being so forthright, but if someone with programming experience can help me, I'll appreciate it.

Well, in my game the player can walk around the house from imagebuttons, simplified example below:

Code: Select all

label map_house_exterior:
    scene house_exterior
    call screen map_house_exterior_buttons

screen map_house_exterior_buttons:
    # front door
    imagebutton:
        xpos 660
        ypos 650
        style "alpha_buttons"
        idle "map/house_exterior_zdoor_idle.png"
        hover "map/house_exterior_zdoor_hover.png"
        action Jump("map_house_dining")
        tooltip "{size=24}Enter your home.{/size}"

    $ tooltip = GetTooltip()

    if GetTooltip() is not None:
        text GetTooltip() at pos_tooltips
So basically the player clicks on the door, or other elements, and is directed to another label similar to this one. Each label represents a room in the house. The original code is more complex, but that's the general idea.

My problem: although this works perfectly for walking around the house, things start to get complex when I start adding events. For example, let's say at one point, the door is locked, or the door leads to an event X now, but after another condition, to an event Y. This will force me to make this code increasingly complex, eg:

Code: Select all

label map_house_exterior:
    scene house_exterior
    call screen map_house_exterior_buttons

screen map_house_exterior_buttons:
    if not X:
        # front door
        imagebutton:
            xpos 660
            ypos 650
            style "alpha_buttons"
            idle "map/house_exterior_zdoor_idle.png"
            hover "map/house_exterior_zdoor_hover.png"
            action Jump("map_house_dining")
            tooltip "{size=24}Enter your home.{/size}"
    if X and not Y:
        # front door is locked
        imagebutton:
            xpos 660
            ypos 650
            style "alpha_buttons"
            idle "map/house_exterior_zdoor_idle.png"
            hover "map/house_exterior_zdoor_hover.png"
            action Jump("map_house_dining_event_locked")
            tooltip "{size=24}Enter your home.{/size}"
    if Y and not Z:
        # front door send to event 1
        imagebutton:
            xpos 660
            ypos 650
            style "alpha_buttons"
            idle "map/house_exterior_zdoor_idle.png"
            hover "map/house_exterior_zdoor_hover.png"
            action Jump("map_house_dining_event1")
            tooltip "{size=24}Enter your home.{/size}"

    $ tooltip = GetTooltip()

    if GetTooltip() is not None:
        text GetTooltip() at pos_tooltips
Summary: The code to walk the map incorporates several "if" and getting stupidly large (considering there are several rooms and several things to interact in each room). As I have no experience as a programmer, I suspect that I am doing something wrong. Could anyone make any suggestions as to how I can keep it as clean as possible while still being able to check to see if the player has events or something?

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: Map navigation with imagebuttons (I need a suggestion)

#2 Post by kivik »

So this will be a key tip for everybody new to programming:

Separate logic from Interface. In Renpy terms, it kinda means separate label / functions from screens - though that's not 100% accurate.

To over simplify it, your interface / Screens should only do 2 things: displaying information based on your variables, and pass input to the logic code / labels and functions.

The thing that is confusing, is that your interface / screen itself can have logics: if x show y else show z - in your example, if you want your door to look different based on whether it's locked or not, you can use if statements to test it and show the right image. However since you're using the same image, I'm going to skip this part.


So the flow of your code should be something like this:

1 - call screen
2 - screen takes user input
3 - you return the user input to the label you were on
4 - based on the user input, AND the X, Y, Z conditions, you decide where to jump.

So:

Code: Select all

label map_house_exterior:
    scene house_exterior
    call screen map_house_exterior_buttons
    if _return == "exterior_door":
        if not X:
            jump map_house_dining
        if X and Y:
            jump map_house_dining_event_locked
        if Y and not Z: # or just use else if there're no other possibilities
            jump map_house_dining_event1
    elif _return == "road":
        # road stuff etc.

# your screen's single door button:
action Return("exterior_door")
Whilst your code works when events / additional conditions are not in place, you were able to directly jump from label to label and essentially made the buttons your menu choices. However you've rightly noticed that it gets rather complicated if you want to mix additional logic in.


Now I won't recommend this, but you can also tweak your version of the screen code, by only changing the action statement based on conditions:

Code: Select all

        imagebutton:
            xpos 660
            ypos 650
            style "alpha_buttons"
            idle "map/house_exterior_zdoor_idle.png"
            hover "map/house_exterior_zdoor_hover.png"
            if not X:
                action Jump("map_house_dining")
            elif X and not Y:
                action Jump("map_house_dining_event_locked")
            else:
                action Jump("map_house_dining_event1")
            tooltip "{size=24}Enter your home.{/size}"

tcassat
Regular
Posts: 33
Joined: Tue Oct 03, 2017 7:32 pm
Contact:

Re: Map navigation with imagebuttons (I need a suggestion)

#3 Post by tcassat »

Thanks! I'll follow your main suggestion of separating the logic from the interface.

EDIT: Ok, worked perfectly

Post Reply

Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot]