Need help with the drag and drop code

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
jelly-crown
Newbie
Posts: 14
Joined: Tue Oct 12, 2010 6:50 pm
Projects: Nostalgia
Organization: Macchiato Games
Contact:

Need help with the drag and drop code

#1 Post by jelly-crown »

Would it be possible to make a pseudo-puzzle using the drag and drop code?
This is kind of what I'm wanting to do:

Lets say the puzzle piece (aka the drag) belongs in spot A (also a drag).
The player puts it on spot A, and the mini-game ends. Resume visual novel. Because they put the puzzle piece in the right place, jump to event A, and they're on the road to viridian city a good ending.
If the player drags it to spot B, the mini-game ends. Resume visual novel. Because they put the puzzle piece in the WRONG place, they'll get a bad ending.
Obviously the actual game won't be as straight-forward as this, but this is just an example.

So I have to ask, is this even possible, or am I just daydreaming?

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

Re: Need help with the drag and drop code

#2 Post by KimiYoriBaka »

that's quite possible. In fact, that's not really different than what's in the example in the documentation.

you just need to make a quick python function that tells what values to give for each ending and put it in the drag.

Code: Select all

init python:
    def piece_dragged(drags, drop):      #these are the required arguments for a dragged callback
        if not drop:
            return     #this is so it doesn't respond to the piece being dragged to a blank spot

        response = drop.drag_name

        return True

screen puzzle:
    add "puzzle_back.png"

    drag_group:
        drag:
            drag_name "piece"
            droppable False
            child "piece.png"
            xpos .2 ypos .5
            dragged piece_dragged    #this is where you put the function

        drag:
            drag_name "a"
            draggable False
            child "a.png"
            xpos .7 ypos .3

        drag:
            drag_name "b"
            draggable False
            child "b.png"
            xpos .7 ypos .6

label puzzle:
    "Now for this random puzzle that will determine your fate!"

    call screen puzzle

    if response == "a":
        jump good_end
    elif response == "b":
        jump bad_end

Soraminako
Veteran
Posts: 277
Joined: Sun Sep 04, 2011 1:36 am
Projects: A thingie without a title. With messy code.
Contact:

Re: Need help with the drag and drop code

#3 Post by Soraminako »

Sorry for reviving an older thread, but I was trying to use that code and noticed that it was having a problem with "response".

It's maybe because of a different version of ren'py or something like that, but when I try using that code I get the error that "response" wasn't defined. (Even though it's supposed to be.)

Is there something I could change to make ren'py recognize that "response" was already defined?

Also, it might also be because of different versions/language changes or something, but in the documentation and in the example in this thread, it says "drag_group", but at least under 6.12, that causes ren'py to crash.
It works if you write "draggroup" instead though. I thought I'd mention it here in case anyone searching through the threads comes across this one. (I spent a while earlier trying to figure out why that was happening, before I found another thread mentioning the "draggroup" thing.)
(I drew my avatar especially to express the scary feeling I get from the code as I type it... XD)

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

Re: Need help with the drag and drop code

#4 Post by KimiYoriBaka »

err...right. judging from the example given in the documentation, this might work:

Code: Select all

store.response = drop.drag_name
however, I'm not sure.

the "drag_group" label is what is used in the example in the documention, so I assumed that would work. The thing is, I've used the DragGroup class only once, and it was for something that went straight into python, I haven't tested the normal sl code for it. good to know.

Soraminako
Veteran
Posts: 277
Joined: Sun Sep 04, 2011 1:36 am
Projects: A thingie without a title. With messy code.
Contact:

Re: Need help with the drag and drop code

#5 Post by Soraminako »

Thank you so much!! :D :D

Just one thing I'm still stuck with though, is there a way to assign a store response not only to the place where the piece was dropped, but also to each different piece?

Like for example, if I have "piece 1" and "piece 2", and piece 1 is meant to go on "space 1" and so on, but the player tries to drop piece 1 on the spot meant for piece 2 instead, how do I make the code be able to tell that this is not how it was meant to be? ^^;

The way I have it working now, I can drop either of the pieces on the spot, and it will jump to the label I had set for that spot, regardless of which piece it was that we drop in it.
It records which pieces was dropped where and sends the result based on where they got dropped, but not based on both factors, which piece it was and where they got dropped.

Is there a way to make it record both which piece it was and where it was dropped, to have it react accordingly, so that if they drop the right piece it will jump to the proper label, but if they drop the wrong piece, it will tell them it was not the right one?

Thank you so much for your help!! m(_ _)m
(I drew my avatar especially to express the scary feeling I get from the code as I type it... XD)

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

Re: Need help with the drag and drop code

#6 Post by KimiYoriBaka »

yes, there is. you just use the other argument in the dragged callback, which is the list drags (it's sent as a list because multiple things can be dragged at a time if they're linked).

using the example above:

Code: Select all

init python:
    def piece_dragged(drags, drop):      #these are the required arguments for a dragged callback
        if not drop:
            return     #this is so it doesn't respond to the piece being dragged to a blank spot

        store.response = drop.drag_name
        store.response_piece = drags[0].drag_name

        return True
you just need to make sure that each thing has its own drag_name so you have something to check in your if conditions.

Soraminako
Veteran
Posts: 277
Joined: Sun Sep 04, 2011 1:36 am
Projects: A thingie without a title. With messy code.
Contact:

Re: Need help with the drag and drop code

#7 Post by Soraminako »

KimiYoriBaka wrote:yes, there is. you just use the other argument in the dragged callback, which is the list drags (it's sent as a list because multiple things can be dragged at a time if they're linked).
[...]
you just need to make sure that each thing has its own drag_name so you have something to check in your if conditions.
Awesome! Thank you!

I'm not 100% sure I understood this right, but if I want to make one response be dependent on the other, would I go about doing this?:

Code: Select all


    if response == "a":
        if response_piece == "01"
              jump good_end
    elif response == "b":
        jump bad_end

Or something like that?

Also, what does the number in the brackets in "store.response_piece = drags[0].drag_name" mean?

Again, thank you so much for your help!! It's truly extremely helpful, without it I would have been stuck on this for who knows how long.
(I drew my avatar especially to express the scary feeling I get from the code as I type it... XD)

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

Re: Need help with the drag and drop code

#8 Post by KimiYoriBaka »

Code: Select all

    if response == "a":
        if response_piece == "01"
              jump good_end
    elif response == "b":
        jump bad_end
almost. if the user places the wrong piece in "a", the program will go right past this. you need to add another else or elif statement to go with the second if.

also, when you see a variable with a number in brackets at the end, it refers to a specific element of a list. drags[0] is the first (and in this case, the only) element in the list "drags".

Soraminako
Veteran
Posts: 277
Joined: Sun Sep 04, 2011 1:36 am
Projects: A thingie without a title. With messy code.
Contact:

Re: Need help with the drag and drop code

#9 Post by Soraminako »

I think I see. At first my sleepy brain had a freeze and I thought you meant that only one if and one elif wouldn't work, period, anywhere, without additional elifs. XD (That tells you how much of a noob I am...)

Then I thought better and realized. Do you mean something to have the game know what to do in case the wrong piece is put in the square? Something like this?

Code: Select all

    if response_place == "a":
        if response_piece == "01"
              jump good_end
        elif response_piece == "02"
              jump stop_cheating
        elif response_piece == "03"
              jump stop_cheating
    elif response_place == "b":
        jump bad_end
Can I also invert them and have something like "response_place" being subordinate to "response_piece" instead of the opposite? Or will that cause problems with the screen language or something?



As for the list number also, would that mean that if I have several pieces to drag, it can't be "0" in those brackets? It would be the number of pieces instead?
Or does that mean that I only have put "1" and so on if I have one or more other puzzles/mini-games etc. later in the game? It's probably a dumb question, but I got pretty confused. ^^;


I have it currently this way, with "0" but with several pieces that can dragged onto various spots. Is it wrong? ^^;

Code: Select all

init python:

    def piece_dragged(drags, drop):

        if not drop:
            return

        store.piece = drags[0].drag_name
        store.square = drop.drag_name

        return True

screen epic_match_screen:

    add "board.png"
    draggroup:

        # Our pieces.
        drag:
            drag_name "piece 01"
            child "piece01.png"
            droppable False
            dragged piece_dragged
            xpos 254 ypos 345
        drag:
            drag_name "piece 02"
            child "piece02.png"
            droppable False
            dragged piece_dragged
            xpos 291 ypos 345
       #...and several more pieces, filling a whole area of a board.

        # The squares they can go to.
        drag:
            drag_name "a2"
            child "squares/a2.png"
            draggable False
            xpos 254 ypos 345
        drag:
            drag_name "b2"
            draggable False
            child "squares/b2.png"
            xpos 291 ypos 345
       #And many many more squares on the board
Thank you so much for all your help!!
(I drew my avatar especially to express the scary feeling I get from the code as I type it... XD)

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot]