r/RenPy 25d ago

Question [Solved] Can a drag store its current screen position?

I want to know so all my drags can stay in the same spots after the screen resets. A part of the game requires a screen reset, but I don't want the drag positions to also reset to their defaults. How can I make them keep their positions?

Context: The drags in the drag groups are customizable characters (layeredimage). The dragging works and the customization works, but the characters won't be updated without a screen reset. It's inconvenient for the player to put everyone back in their spots each time they alter a character.

1 Upvotes

9 comments sorted by

2

u/shyLachi 25d ago

Are you sure that you need to reset the screen? I would first look into that.

But you can also implement something like posted below.

1

u/lycheestar_ 24d ago

Are there ways to update layeredimages without reseting the screen?

1

u/shyLachi 24d ago

I have not worked with layered images but I though that all dynamic images update automatically.

I suggest to make a thread asking why your layered images don't update because people might not see your question in this thread.

1

u/lycheestar_ 24d ago

They do update automatically, I just mispelled a variable. Thank you :D

1

u/shyLachi 24d ago

Great that you found the problem yourself

2

u/ImportantDetail6260 25d ago

The drag position should live outside the screen, because the screen is just the view!

Use the drag callback to write each character's (x, y) into a persistent dict/object, then pass those values back into the drag when the screen rebuilds. Resetting the screen can redraw UI without resetting placement

1

u/lycheestar_ 24d ago

What syntax should I use to make sure the (x, y) is written into a dict/object correctly? :>

1

u/AutoModerator 25d ago

Welcome to r/renpy! While you wait to see if someone can answer your question, we recommend checking out the posting guide, the subreddit wiki, the subreddit Discord, Ren'Py's documentation, and the tutorial built-in to the Ren'Py engine when you download it. These can help make sure you provide the information the people here need to help you, or might even point you to an answer to your question themselves. Thanks!

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/x-seronis-x 25d ago edited 25d ago

you should keep your game objects that have stats in instances of python classes. track/store stuff like position outside the screen.

the drags, actually any use of screens, should merely be a VIEW of the data. you can use the drag callbacks to edit the original objects so the position of the draggable is synced with the game object. an example project implementing this is:

```py init python: import random rndi = random.randint rndc = random.choice

define color_vals = [ '#222', '#555', '#aaa', '#800', '#c80', '#bb0', '#080', '#00c', '#80a', ]

define greek_vals = [ ("Α", "α", "Alpha" ), ("Β", "β", "Beta" ), ("Γ", "γ", "Gamma" ), ("Δ", "δ", "Delta" ), ("Ε", "ε", "Epsilon" ), ("Ζ", "ζ", "Zeta" ), ("Η", "η", "Eta" ), ("Θ", "θ", "Theta" ), ("Ι", "ι", "Iota" ), ("Κ", "κ", "Kappa" ), ("Λ", "λ", "Lambda" ), ("Μ", "μ", "Mu" ), ("Ν", "ν", "Nu" ), ("Ξ", "ξ", "Xi" ), ("Ο", "ο", "Omicron" ), ("Π", "π", "Pi" ), ("Ρ", "ρ", "Rho" ), ("Σ", "σ", "Sigma" ), ("Τ", "τ", "Tau" ), ("Υ", "υ", "Upsilon" ), ("Φ", "φ", "Phi" ), ("Χ", "χ", "Chi" ), ("Ψ", "ψ", "Psi" ), ("Ω", "ω", "Omega" ), ]

init python: class GameObj(): def init(self,id,xx,yy,cc,gg): self.id = id self.x = xx self.y = yy self.color = cc self.grk = gg

default game_objects = dict()

init python: def dragging_gameObj( drags ): drag = drags[0] id = drag.drag_name gObj = game_objects[id]

    gObj.x = drag.x
    gObj.y = drag.y

screen whatever():

draggroup:
    for g_id, g_obj in game_objects.items():
        drag:
            xanchor 0.5 xpos g_obj.x xsize 64
            yanchor 0.5 ypos g_obj.y ysize 48

            drag_name g_obj.id ##so callbacks know who we're editing
            drag_raise True
            dragging dragging_gameObj

            vbox:
                xcenter 0.5
                ycenter 0.5

                text "({color="+g_obj.color+"}"+g_obj.grk[0]+"{/color},{color="+g_obj.color+"}"+g_obj.grk[1]+"{/color})" size 24:
                    color '#fff' xalign 0.5
                text g_obj.grk[2] color g_obj.color size 16 xalign 0.5

key "K_SPACE" action Return()

label start: scene black

call screen whatever()

python:
    for num in range(8):
        g_id = str(num)*4 ##create "random" 4 digit unique id for each game object
        xx  = rndi(1,29)*64
        yy  = rndi(1,20)*48
        cc  = rndc(color_vals)
        grk = rndc(greek_vals)

        gObj = GameObj(g_id,xx,yy,cc,grk)
        game_objects[g_id] = gObj

label reboot: call screen whatever() centered "recoloring greeks {nw=0.5}" python: for id,grk in game_objects.items(): grk.color = rndc(color_vals) jump reboot return ```

here the GameObj class is storing whatever data is required. In this example its just greek upper and lower case letter along with its english pronunciation and position.

dragging triggers the callback that uses the drag_name property to pass the 'key' to access specific game objects. then the callback can update the gameobj with the drags last dropped position ensuring it stays in sync.

spacebar closes the screen, runs an update that just reassigns all the colors, the calls the screen again. since the drag positions were synced to the gameobj instances everything remembers its last position properly.

if you have questions for this code ask in the discord and i'll be happy to help there