[SOLVED] Using a screen to make an interactive map

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
ANGEL1234-1
Newbie
Posts: 3
Joined: Wed May 11, 2022 4:47 pm
Contact:

[SOLVED] Using a screen to make an interactive map

#1 Post by ANGEL1234-1 » Thu May 12, 2022 7:51 am

Hi, I need help with the implementation of this mechanic; I'm trying to make an interactive map where the player could move and speak with npc's, so I thought of using a screen with a viewport for the map and imagebuttons to interact with the characters.

This mechanic works fine, however since the screen is invoked with call it disappears when you start a dialogue. The way I found to deal with this was to call the same screen with show to use as background while the conversation is going and then go back to the screen with a call, however doing this means loosing the position of the player in the map, so I need a way to keep the coordinates of the player's location.

After reading through the documentation I thought of using two global variables and setting them to the x and yadjustment.value when initiating a conversation with an npc as this should set the coordinates of the player to the variables, that way I could then pass the variables as arguments to the screen and setting the xinitial yinitial to those values. This, however, doesn't seem to work unless the player press the wheel of the mouse to hide the screen, after which the coordinates of the moment when the player pressed the wheel are saved. Needless to say this isn't what I'm looking for, but I can't think of a way to make the system work the way I'd like to.

Does anyone know how could I fix this?

Here's the code:

(Calling the map)

Code: Select all

    $ npc = ["images/npc/n5_dojima/n5_0_0_0.png", "images/npc/n8_nanako/n8_0_0_0.png"]
    $ script = ["ns8_2_1", "ns8_2_2"]
    $ x_pos = [1350, 1450]
    $ y_pos = [3020, 2910]
    $ scroll_x = 750
    $ scroll_y = 2700

    call screen f008_002(npc, script, x_pos, y_pos, scroll_x, scroll_y, True)
(The code of the map)

Code: Select all

screen f008_002(npc, script, x_pos, y_pos, scroll_x, scroll_y, can_interact):

    zorder -100

    default xadj = ui.adjustment(scroll_x)
    default yadj = ui.adjustment(scroll_y)

    viewport id "f008_002":
        child_size(4999, 4320)
        draggable can_interact
        yinitial scroll_y
        xinitial scroll_x
        yadjustment yadj
        xadjustment xadj

        add "images/map/f008_002_inaba_south/f008_002.png"

        for i in range(len(npc)):

            imagebutton:
                idle npc[i] at half_size
                if can_interact:
                    action [SetVariable("scroll_y", yadj.value), SetVariable("scroll_x", xadj.value), Call(script[i])]
                xpos x_pos[i]
                ypos y_pos[i]
(Example of a conversation with a npc)

Code: Select all

label ns8_2_1:
    show screen f008_002(npc, script, x_pos, y_pos, scroll_x, scroll_y, False)
    show Dojima_1 at right
    "Dojima" "How're you feeling?"
    "Dojima" "Ready to get back in the car?"

    menu:
        "Dojima" "Ready to get back in the car?"
        "I'm ready":
            "Dojima" "All right, let's hit the road then."
            hide Dojima_1
            jump day_2011_4_11_2
        "Not yet...":
            "Dojima" "I see."
            "Dojima" "Well, I'll be waiting here."
            "Dojima" "You should take a walk around here."
            "Dojima" "Hopefully it'll make you feel better."
            hide Dojima_1
            call screen f008_002(npc, script, x_pos, y_pos, scroll_x, scroll_y, True)
Last edited by ANGEL1234-1 on Thu May 12, 2022 5:05 pm, edited 1 time in total.

rayminator
Miko-Class Veteran
Posts: 754
Joined: Fri Feb 09, 2018 12:05 am
Location: Canada
Contact:

Re: Using a screen to make an interactive map

#2 Post by rayminator » Thu May 12, 2022 8:14 am

you don't need to add this to the call
(npc, script, x_pos, y_pos, scroll_x, scroll_y, True)

and your zorder is at -100 so your screen will never be shown regardless you called it or show it

ANGEL1234-1
Newbie
Posts: 3
Joined: Wed May 11, 2022 4:47 pm
Contact:

Re: Using a screen to make an interactive map

#3 Post by ANGEL1234-1 » Thu May 12, 2022 10:20 am

I hide everything before calling the map and have tested it a few times and the screen shows, that's not an issue. The only problem is that scroll_x and scroll_y don't store the player's coordiantes in the viewport, so when I call the screen again the position is not the same that the one the player was before he started a conversation with the npc. Since the coordinates do get save in the variables when I hide the screen pressing the mouse wheel I'm guessing there's a way to force the coordinates to get save with this code or using something else, I just can't find how or what to do to get this to work.

User avatar
Alex
Lemma-Class Veteran
Posts: 2981
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Using a screen to make an interactive map

#4 Post by Alex » Thu May 12, 2022 11:25 am

ANGEL1234-1 wrote:
Thu May 12, 2022 10:20 am
...
Not tested, but try:
- move those lines outside the screen to make them global variables

Code: Select all

default xadj = ui.adjustment()
default yadj = ui.adjustment()
and use 'xadj' and 'yadj' instead of 'scroll_x' and 'scroll_y'
viewtopic.php?f=8&t=58441#p526802

- show 'f008_002' screen instead of call, to make it stay onscreen untill you hide it

- make variable 'can_interact' global one and don't pass it to the screen, so you could change it during dialog.

ANGEL1234-1
Newbie
Posts: 3
Joined: Wed May 11, 2022 4:47 pm
Contact:

Re: Using a screen to make an interactive map

#5 Post by ANGEL1234-1 » Thu May 12, 2022 5:01 pm

Thank you very much, I defined the default xadj and yadj as global variables and then pass the .values instead of the scroll_x and scroll_y and it now works perfectly. The code is also less bloated now.

Code: Select all

screen f008_002(npc, script, x_pos, y_pos, xadj, yadj, can_interact):

    zorder -100

    viewport id "f008_002":
        child_size(4999, 4320)
        draggable can_interact
        xadjustment xadj
        yadjustment yadj

        add "images/map/f008_002_inaba_south/f008_002.png"

        for i in range(len(npc)):

            imagebutton:
                idle npc[i] at half_size
                if can_interact:
                    action Call(script[i])
                xpos x_pos[i]
                ypos y_pos[i]

Post Reply

Who is online

Users browsing this forum: mold.FF