ondestroy events caller is not detected.

  • Hi,

    Please take a look at the code snippet below:


    <layer name="ly_kabkee" url="/assets/panda.png"

    enabled="false"

    edge="center"

    align="center"

    onleft="tween(rotate, 45,,, onright);"

    onright="tween(rotate, 0,,, onleft);"

    onloaded="onleft()"

    ondestroy="trace('bye for now 1.22.3'); onDestroyElement();"

    />


    <action name="onDestroyElement">

    trace(get(caller.name));

    trace(caller);

    stoptween(layer[get(name)].rotate);

    </action>


    This code is designed to make the layer rotate left and right repeatedly and call the onDestroyElement action when the layer is destroyed.


    The issue is that while the onDestroyElement action is properly triggered in Krpano 1.22.3, the caller information or properties like name and url seem to be unavailable when accessed within inline code.


    For example:

    ondestroy="trace('bye for now 1.22.3'); trace(name); trace(URL)"


    Both name and url return no values.

    That also effects the stoptween is not working in the onDestroyElement action.


    Additionally, when switching scenes, the following warning appears:

    WARNING: Unknown action: onleft

    This could have been prevented if I properly called the stoptween action before changing scenes.


    This seems to happen because the layer’s body element has already been removed.


    To address this, I’ve had to define an additional onremovepano action to clean things up. However, my expectation was that the ondestroy event would allow me to write less code by automatically handling cleanup. Unfortunately, it doesn’t seem to pass any caller information, making it less useful in this context.


    Am I using the ondestroy event incorrectly, or is there another way to approach this?

    Let me know if this works for you!

    Edited once, last by Kabkee (January 8, 2025 at 7:05 AM).

  • In the onDestroyElement action, the resulting path behaves globally.

    This indicates that the caller of the ondestroy event is not tied to the layer itself, which seems incorrect. Ideally, the caller of the ondestroy event should directly reference the layer being destroyed.

    This behavior appears to be a bug, and I believe it would benefit from clarification or confirmation from Klaus.

    It should function such that the caller of the ondestroy event is indeed the specific layer triggering the event.

  • To get a 'caller' object, the action need to be a localscope action.

    Code
    <action ... scope="local">
    ...
    </action>


    That’s correct. I made an oversight in not initially clarifying that the scope is local.

    However, even with that considered, this behavior is NOT functioning as expected.

    Consider the following example:

    <layer name="ly_kabkee" url="/assets/panda.png"
    onleft="tween(rotate, 45,,, onright);"
    onright="tween(rotate, 0,,, onleft);"
    onloaded="onleft()"
    ondestroy="trace('bye for now 1.22.3'); onDestroyElement();"
    />

    When the ondestroy event for the layer is triggered, attributes like layer.name or other properties are not accessible within the onDestroyElement action.

    This is the core issue I’m highlighting: these attributes should be accessible during the ondestroy event. Unfortunately, they are not.

    Additionally, even inline ondestroy commands fail to work as expected. Consider this example:

    <layer name="ly_kabkee" url="/assets/panda.png"
    onleft="tween(rotate, 45,,, onright);"
    onright="tween(rotate, 0,,, onleft);"
    onloaded="onleft()"
    ondestroy="trace('bye for now 1.22.3'); get(name)"
    />

    Here, using get(name) within the ondestroy event does not return the expected value. In this case, get(name) should return the layer name, "ly_kabkee", but it does not.

    Hypothesis:
    I suspect this might be happening because the layer is already destroyed before the ondestroy event is fired.

    Suggestion:
    Even if that’s the case, this behavior should be adjusted so that the ondestroy event is triggered before the layer is destroyed. This would allow developers to properly access the layer’s attributes during the ondestroy event, as expected.

  • Okay, I see the problem...

    The ondestroy itself is working, but during loading new panos (from the load request to until the xml parsing is done), the action processing is paused and calls during that time are queued and executed delayed.

    And then when the code from the ondestroy event gets called, the internal state is already at 'destroyed' and so it's code is skipped...

    I will think about solutions...

    As workaround, it would be possible to assign a Javascript function to the ondestroy event, this is not affected by the delayed action processing.

  • I’m really excited about the new reactive programming features in Krpano—great work! 😊
    I was especially looking forward to the ondestroy event for layers and hotspots, as it seems like a great fit for addressing certain challenges.

    For example, I often create tween animations with the donecall parameter for infinite loops. However, when changing scenes, warnings like “unknown action” appear because all layers or hotspots from the previous scene are destroyed. To avoid these warnings, I’ve been adding onremovepano actions to every scene that includes tween animations.

    If the new ondestroy event could serve as an alternative to my current solution, it would be a perfect fit given its name, “ONDESTROY.” It feels like this event could be the most intuitive and practical way to handle such scenarios.

    Here’s a simple example of what I mean:

    <layer url="abc.jpg"
    ongoup="tween(y, 10,,, ongodown() )"
    ongodown="tween(y, -10,,, ongoup() )"
    onloaded="ongoup();"
    ondestroy="stoptween(y)"
    />

    I’m really looking forward to seeing improvements to the ondestroy event in the near future.
    Also, could you share some guidance on the best practices for using ondestroy as it was intended? That would be incredibly helpful!

    This version reads more polished and positive while clearly expressing your points. Let me know if you’d like further adjustments! 😊

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!