[solved] transform moving when an item from the same list is removed

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
Kia
Eileen-Class Veteran
Posts: 1011
Joined: Fri Aug 01, 2014 7:49 am
Deviantart: KiaAzad
Discord: Kia#6810
Contact:

[solved] transform moving when an item from the same list is removed

#1 Post by Kia » Sun Apr 03, 2022 12:44 pm

One of the things that have been annoying me for a long time is: Often I loop over a list to create something residing on a transform, when I remove an item from the list, others move. It's hard to describe, you can try this mock-up to see what I mean.

Code: Select all

default all_buttons = []
init python:
    def add_button(l):
        l.append(renpy.random.randint(-800, 800))
    def remove_button(l, i):
        l.remove(i)
transform test_transform(x):
    ease .4 xoffset x
screen transform_test(l= all_buttons):
    for i in l:
        button:
            xysize 100,100 align .5,.5 background "#fff9"
            text "Remove"
            at test_transform(i)
            action Function(remove_button,l, i)

    button:
        xysize 100,100 background "#fff9"
        text "add"
        action Function(add_button, l)
I'm curious if there's a simple way around it?
Last edited by Kia on Sun Apr 03, 2022 3:02 pm, edited 1 time in total.

User avatar
Ocelot
Eileen-Class Veteran
Posts: 1883
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: transform moving when an item from the same list is removed

#2 Post by Ocelot » Sun Apr 03, 2022 1:00 pm

Well, it is logical. First it was button [0] at -400 and button [1] at 600, and after deletion of one button you have only button [0] at 600. You need to animate change from -400 to 600.
What is the desired behavior? If it is to make buttons stay at their original position: do not delete buttons. Simply mark them as deleted in the list, and do not show deleted entries. In add_button scan list and replace first button marked as deleted (or add to the end if no deleted button exist)
< < insert Rick Cook quote here > >

User avatar
Kia
Eileen-Class Veteran
Posts: 1011
Joined: Fri Aug 01, 2014 7:49 am
Deviantart: KiaAzad
Discord: Kia#6810
Contact:

Re: transform moving when an item from the same list is removed

#3 Post by Kia » Sun Apr 03, 2022 1:43 pm

Hiding them is what I'm doing as a solution, but in most cases, I'll be running different operations over the list and not removing them can cause the list grow too big and slow down the game over a long time.
I'm changing their numbers to move them, having an ease function makes the movement smooth. but does introduce the problem on removing items from the list.
I can imagine defining them as objects and the transform that moves them around as a part of it, then I can add that transform to the screen maybe? I've seen something like that don in CDDs

User avatar
Ocelot
Eileen-Class Veteran
Posts: 1883
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: transform moving when an item from the same list is removed

#4 Post by Ocelot » Sun Apr 03, 2022 2:02 pm

Reusing deleted items will not cause list to grow more than maximum active objects at the same time (in fact, it is also how filesystems operate, this is why it is sometimes possible to restore deleted ifiles). "Never delete hot object" is a common optimisation in gamedev.

You can check edited code and see, that list size never grows larger than maximum amount of buttons displayed at the same time:

Code: Select all

default all_buttons = []
init python:
    class ButtonHolder:
        def __init__(self, position, active=True):
            self.pos = position
            self.active = active

    def add_button(l):
        newpos = renpy.random.randint(-800, 800)
        for i in l:
            if not i.active:
                i.pos = newpos
                i.active = True
                break
        else:
            l.append(ButtonHolder(newpos))

    def remove_button(l, i):
        idx = l.index(i)
        l[idx].active = False

transform test_transform(x):
    ease .4 xoffset x

screen transform_test(l= all_buttons):
    for i in l:
        if i.active:
            button:
                xysize 100,100 align .5,.5 background "#fff9"
                text "Remove"
                at test_transform(i.pos)
                action Function(remove_button,l, i)
    button:
        xysize 100,100 background "#fff9"
        text "add"
        action Function(add_button, l)
< < insert Rick Cook quote here > >

User avatar
Kia
Eileen-Class Veteran
Posts: 1011
Joined: Fri Aug 01, 2014 7:49 am
Deviantart: KiaAzad
Discord: Kia#6810
Contact:

Re: transform moving when an item from the same list is removed

#5 Post by Kia » Sun Apr 03, 2022 3:01 pm

That's a pretty nifty workaround, I was messing with replacing them with None and all, causing more problem instead of solving any.
Thank you.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], span4ev