Drag item won't consistently drop in its assigned spot

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
solarisdreams
Newbie
Posts: 3
Joined: Fri Mar 01, 2024 3:47 pm
Contact:

Drag item won't consistently drop in its assigned spot

#1 Post by solarisdreams »

Hello, I'm pretty new to Renpy and have been struggling with creating a drag and drop minigame. Also I'm new to posting here so sorry if the format isn't the best.

But I have it set up where 12 pieces spawn on the left and the player must drag a specific 8 of them over to the frame on the left. Every piece snaps into place except for the piece that is circled in blue on the solution screenshot. I have only gotten it to snap once and go onto the novel but can't seem to replicate what I did.

The corresponding image file name is piece-2 which I'm assuming means it's not a list or range issue because it is in the middle of the list. If anyone has any ideas for what might be causing this I would greatly appreciate.

Here is my code so far:

Code: Select all

init python:
    def piece_drop(dropped_on, dragged_piece):

        global finished_pieces

        if dragged_piece[0].drag_name == dropped_on.drag_name:
            dragged_piece[0].snap(dropped_on.x, dropped_on.y)
            dragged_piece[0].draggable = False
            finished_pieces += 1

            if finished_pieces == solution_pieces:
                renpy.jump("poster_complete")

# define certain global variables that will keep of track of things like number of pieces, the final locations, how many are fixed, etc.

default poster_pieces = 12
default solution_pieces = 8
default poster_size = (668, 688)
default piece_coordinates = [(1224,377),(1237,369),(1526,380),(1544,392),(1553,716),(1533,704),(1206,702),(1243,701)]
default intitial_piece_coordinates = [(579,214),(272,537),(254,866),(596,554),(605,203),(249,545),(572,882),(271,214),(262,890),(253,190),(563,536),(603,887)]
default finished_pieces = 0

screen poster:
    image "bg wood.png"

    draggroup:
        # draggable pieces
        for i in range(poster_pieces):
            drag:
                drag_name i 
                pos intitial_piece_coordinates[i]
                anchor(0.5, 0.5)
                focus_mask True
                drag_raise True
                image "Pieces/piece-%s.png" % (i + 1)

        # snappable spots
        for i in range(solution_pieces):
            drag:
                drag_name i 
                draggable False
                droppable True
                dropped piece_drop
                pos piece_coordinates[i]
                anchor(0.5, 0.5)
                focus_mask True
                image "Pieces/piece-%s.png" % (i + 1) alpha 0.0
and then later in the script to call it

Code: Select all

    call screen poster

    label poster_complete:
        scene bg test alley
        o "This has to be the location."
Attachments
code_solution.png
(1.27 MiB) Not downloaded yet
code_setup.png
(1.33 MiB) Not downloaded yet
code_part3.png
(130.16 KiB) Not downloaded yet
code_part2.png
(136.91 KiB) Not downloaded yet
code_part1.png
(147.97 KiB) Not downloaded yet
Last edited by solarisdreams on Tue Mar 05, 2024 6:13 am, edited 1 time in total.

jeffster
Veteran
Posts: 421
Joined: Wed Feb 03, 2021 9:55 pm
Contact:

Re: Drag item won't consistently drop in its assigned spot

#2 Post by jeffster »

solarisdreams wrote: Fri Mar 01, 2024 4:00 pm sorry if the format isn't the best.
It would be great if you could edit your post and put the code in tags, starting with

Code: Select all

[code]
and ending with:
[/code]

Then it would be easier to read.

User avatar
m_from_space
Eileen-Class Veteran
Posts: 1000
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: Drag item won't consistently drop in its assigned spot

#3 Post by m_from_space »

solarisdreams wrote: Fri Mar 01, 2024 4:00 pm

Code: Select all

    draggroup:
        # draggable pieces
        for i in range(poster_pieces):
            drag:
                drag_name i 
                pos intitial_piece_coordinates[i]
                anchor(0.5, 0.5)
                focus_mask True
                drag_raise True
                image "Pieces/piece-%s.png" % (i + 1)

        # snappable spots
        for i in range(solution_pieces):
            drag:
                drag_name i 
                draggable False
                droppable True
                dropped piece_drop
                pos piece_coordinates[i]
                anchor(0.5, 0.5)
                focus_mask True
                image "Pieces/piece-%s.png" % (i + 1) alpha 0.0
Not sure what the real issue is, so I am just throwing stuff in here:

Some of your drags that are draggable and drags that are droppable, share the same drag_name property (just using "i" here). I am not sure this doesn't cause any issues (maybe it doesn't, who knows). Can you create unique names for them (maybe just an offset of +12 for the solutions)? Because I can't make out why this specific piece-2 should have a problem. But maybe it doesn't snap, because the drag_names don't match? To test that you could put a renpy.notify() into the piece_drop function that outputs both drag_names when it is called.

Also just using numbers as names maybe can be a problem. Because in Python the number 1 also evaluates to True, while other number don't. And for piece-2 the integer i is equal to 1. But then again you comparing two numbers.

Maybe it doesn't snap, because the droppable is overlapped by another image and the event therefore never called? Can you set alpha of your snappable spots to something greater to see them? You're also using focus_mask, but set all alpha to 0, no clue what this is doing actually. For a draggable, this would mean you cannot drag it.

By the way, you're using the "image" keyword as part of the drag, that's not correct. I see many people doing that inside screens. The "image" statement is for creating images, not for showing them. Use "add" instead!

solarisdreams
Newbie
Posts: 3
Joined: Fri Mar 01, 2024 3:47 pm
Contact:

Re: Drag item won't consistently drop in its assigned spot

#4 Post by solarisdreams »

Some of the other pieces that work fine are also overlapping. The piece in question is barely if at all overlapping with other pieces.

The part of the code using focus_mask and setting all to alpha to 0 is basically the final solution spots hidden so that the player can't see or move them. When one of the draggable pieces is dropped over the corresponding hidden piece it's supposed to snap and add to the completed piece variable in order to move the game forward.

I tried changing the names so that it would start at 0, so piece-0 instead of piece-1, and it doesn't seem to be because of the numbers since the issue didn't move to a different piece. It's still only that piece acting up.

I also tried the notify check line but for some reason it's not even running on that piece. So it looks like the piece_drop function isn't running on that piece, but I'm not sure why it's not.

User avatar
m_from_space
Eileen-Class Veteran
Posts: 1000
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: Drag item won't consistently drop in its assigned spot

#5 Post by m_from_space »

solarisdreams wrote: Tue Mar 05, 2024 6:39 am I also tried the notify check line but for some reason it's not even running on that piece. So it looks like the piece_drop function isn't running on that piece, but I'm not sure why it's not.
Yeah that was my assumption. So try making the drop pieces visible so you can be sure they are in the right spot to begin with. Maybe for that piece the "drag" is out of place or something.

solarisdreams
Newbie
Posts: 3
Joined: Fri Mar 01, 2024 3:47 pm
Contact:

Re: Drag item won't consistently drop in its assigned spot

#6 Post by solarisdreams »

I have set the solution pieces visible and they all are in the right place.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], DewyNebula, Google [Bot], NoFanru