Help with drag image on screen and drop into container

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
User avatar
RogueArtist
Newbie
Posts: 4
Joined: Wed Feb 24, 2021 3:01 pm
Contact:

Help with drag image on screen and drop into container

#1 Post by RogueArtist » Fri Feb 26, 2021 11:27 am

Hello,

How can I have a backpack container on screen at all times, not loading it with each screen change?

And then I want to have the player be able to drag and drop an image from the scene onto the backpack, which adds it to an inventory list that the engine will reference later when such items are needed to complete quests.

This is my first foray into coding like this, please be patient. The last time I coded anything was back when what they now call "legacy" web-coding was new lol. So some stuff looks familiar but the more complex things I just need pointed in the right direction. It will make sense to me once I am shown the way.

thank you very kindly,
-Jason

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

Re: Help with drag image on screen and drop into container

#2 Post by Alex » Fri Feb 26, 2021 1:45 pm

RogueArtist wrote:
Fri Feb 26, 2021 11:27 am
...And then I want to have the player be able to drag and drop an image from the scene onto the backpack, which adds it to an inventory list that the engine will reference later when such items are needed to complete quests. ...
There might be other solutions, but you could try to make such a system.
A screen, with a draggroup that has a backpack droppable and a number of draggables that will be created using a list of their descriptions. When item dropped onto backpack it should be added in the inventory list and removed from the list of item descriptions. If you place this backpack over the normal inventory button it'll look like you drop items inside the inventory.
Then you'll be able to show this screen using different item descriptions.

Check sample with detectives - https://www.renpy.org/doc/html/drag_drop.html#examples
Also, this might help
viewtopic.php?f=8&t=53537&p=503938#p503894
viewtopic.php?f=8&t=61134#p538636

User avatar
_ticlock_
Veteran
Posts: 391
Joined: Mon Oct 26, 2020 5:41 pm
Contact:

Re: Help with drag image on screen and drop into container

#3 Post by _ticlock_ » Fri Feb 26, 2021 4:34 pm

Hi, RogueArtist,

For drag and drop you need to have both drag and drop in the same draggroup. Thus, it should be inside one screen.

Alex has provided nice examples. Here is another example of what you can do(You can copy-paste the code to a new project to see how it works):

1) You can have one inventory screen and in each "scene" pass the "items" to the screen. For example, you can pass all available items to the list scene_items, which is used by inventory screen:

Code: Select all

init python:
    def item_dragged(drags, drop):
        if drop:
            if drop.drag_name =="backpack":
                for i, item in enumerate(store.scene_items):
                    if item == drags[0].drag_name:
                        inventory_items.append(store.scene_items.pop(i))
            return True
        else:
            return

default scene_items_scene_001 = [Item('key1','key1', 500,500), Item('key2','key2', 800,500)]
default scene_items_scene_002 = [Item('key3','key3', 100,500), Item('key4','key4', 800,100)]
default scene_items = []
default inventory_items = []

screen inventory_screen():
    draggroup:
        drag:
            drag_name 'backpack'
            child 'backpack'
            draggable False
            xpos 0 ypos 0
        for item in scene_items:
            drag:
                drag_name item
                child item.image
                droppable False
                dragged item_dragged
                xpos item.scene_pos_x ypos item.scene_pos_y
Function item_dragged process if the item is dropped in the backpack. If yes, it transfer item to the inventory_items

This example assumes that you have defined some Item class, for example like this:

Code: Select all

init python:
    class Item():
        def __init__(self, name, image, scene_pos_x, scene_pos_y):
            self.name = name
            self.image = image
            self.scene_pos_x = scene_pos_x
            self.scene_pos_y = scene_pos_y
Let's add images that represent our items(gray squares with text):

Code: Select all

init python:
    def get_text_image(text,size):
        return LiveComposite(
            size,
            (0,0), Solid("#888", xysize=size),
            (0,0), Text(text))
image backpack = get_text_image('backpack', (150,150))
image key1 = get_text_image('key1', (50,50))
image key2 = get_text_image('key2', (50,50))
image key3 = get_text_image('key3', (50,50))
image key4 = get_text_image('key4', (50,50))
Then in script:

Code: Select all

label start:
    show screen inventory_screen()
    "No items available. Let's go to another scene"
    jump scene_001

label scene_001:
    $ scene_items = scene_items_scene_001
    "Now, you can try to put key1 or key 2 in the backpack"
    "Let's go to another image, that does not have any items"
    $ scene_items = []
    "No items available. Let's go to another scene 2"
    $ scene_items = scene_items_scene_002
    "Now, you can try to put key3 or key 4 in the backpack"
    "Let's jump back to label scene_001."
    "Notice, that items that are in the backback are no longer in the scene_items_scene_001"
    jump scene_001

User avatar
RogueArtist
Newbie
Posts: 4
Joined: Wed Feb 24, 2021 3:01 pm
Contact:

Re: Help with drag image on screen and drop into container

#4 Post by RogueArtist » Fri Feb 26, 2021 6:56 pm

Oh! Well by golly, between both of your examples, I think I'm starting to see how that works. Cool 😊 thank you. Now I'll play with those codes and learn.

User avatar
RogueArtist
Newbie
Posts: 4
Joined: Wed Feb 24, 2021 3:01 pm
Contact:

Re: Help with drag image on screen and drop into container

#5 Post by RogueArtist » Fri Feb 26, 2021 7:12 pm

Ok I got stuck...

From this example:

show screen inventory_screen()
"No items available. Let's go to another scene"
jump scene_001


I get this error:

File "game/script.rpy", line 52: u'show' is not a keyword argument or valid child for the screen statement.
show screen inventory_screen()

User avatar
hell_oh_world
Miko-Class Veteran
Posts: 777
Joined: Fri Jul 12, 2019 5:21 am
Projects: The Button Man
Organization: NILA
Github: hell-oh-world
Location: Philippines
Contact:

Re: Help with drag image on screen and drop into container

#6 Post by hell_oh_world » Fri Feb 26, 2021 7:53 pm

the codes are done inside a `label` not inside a `screen`.

User avatar
RogueArtist
Newbie
Posts: 4
Joined: Wed Feb 24, 2021 3:01 pm
Contact:

Re: Help with drag image on screen and drop into container

#7 Post by RogueArtist » Fri Feb 26, 2021 9:34 pm

Oh good, I understand labels at least 😁

Post Reply

Who is online

Users browsing this forum: Bing [Bot]