getting an attribute that has part of its name stored in a variable

  • Hello!
    I'm sorry to bother you with what I suppose is something everyone should know… but how do I access an attribute has its name stored in another variable?


    Suppose I have a xml element with the descriptions of my three hotspots nm1, nm2 and nm3, to be shown when I hover over them.


    <description
    nm1="kitchen"
    nm2="bedroom"
    nm3="living room"...>

    On the hotspot definition, I call the "find description()" with the onhover.

    <hotspot name="nm1", onhover="find_description()" ...>

    If I define the action find_description as such:

    <action name="find_description">
    txtadd(hovertext,"description.", get(name()); <!-- get(name) gives me the name of the hotspot I hover, i.e. nm1 -->
    trace(name);
    trace(hovertext);
    trace(get(hovertext));
    showtext(get(hovertext));
    </action>

    trace(name) gives me the hotspot name (i.e. nm1), which is ok.
    Trace (hovertext) gives me "desription.nm1".
    Trace(get(hovertext)) gives me "kitchen" and that is ok.
    But showtext doesn't work like trace, showtext(get(hovertext)) still shows me "description.nm1".

    Is there any straightforward way to solve the problem? The only way I could think of was quite clumsy, to redefine the action to call another action act2:

    <action name="find_description"> txtadd(hovertext,"description.", get(name()); act2(get(hovertext)) </action>
    <action name="act2">
    showtext(get(%1)) </action>

    This, of course, substituted desription.nm1 into %1, transforming get(%1) into get(description.nm1), which, obviously, showed "kitchen". It made it possible to apply get twice. Unfortunately, using just showtext(get(get(hovertext)) didn't work, it showed Null.
    But it doubled the number of neccesary actions, it makes the code very unreadable.

    Thank you!

  • txtadd(hovertext,"description.", get(name());

    try this (remove brackets)

    txtadd(hovertext,'description.', get(name));

    or

    <hotspot name="nm1", onhover="find_description(get(name))" ...>

    <action name="find_description">

    showtext(get(description.%1));

    </action>

    Piotr

  • Thanks for your reply, Piotr!

    I'm sorry, I already used the name without brackets. I just wrote them here by mistake.

    The second idea works, thank you!

    Still, why do showtext() and trace() work so differently? Why does one show kitchen and the other remains at description.nm1?

    and why wouldn't

    <hotspot name="nm1", onhover="showtext(get(description.get(name)))" ...> work?
    Why does a variable have to be passed to a function just to replace the %1 with get(name) afterwards instead of working directly? Is it because krpano can't automatically find the value of the variable in the internal bracket before going on to the external ones?

    I read the tutorial about how variables work in functions, but I can't find a answer there.

  • Quote

    <hotspot name="nm1", onhover="showtext(get(description.get(name)))" ...> work?

    It will not work, see here

    You can do it in a different way:

    Code
    <description name="nm1" val="kitchen" />
    <description name="nm2" val="bedroom" />
    <description name="nm3" val="living room" />

    and new code:

    Code
    <hotspot name="nm1" onhover="showtext(get(description[get(name)].val));"/>


    If you want to trace a variable you need to use the get method

    trace(description.nm1); -> INFO: description.nm1 (string)
    trace(get(description.nm1)); -> INFO: kitchen (viriable)

    Quote

    Why does a variable have to be passed to a function just to replace the
    %1 with get(name) afterwards instead of working directly? Is it because
    krpano can't automatically find the value of the variable in the
    internal bracket before going on to the external ones?

    You must follow the general rules of XML syntax and KRpano documentation here . *thumbup*

    Piotr

  • Ok, I think I get it. You are really resourceful, thank you!
    But sometimes I would still like to use the txtadd method.

    For example, when klicking on the nm1 hotspot, I would like to play a sound from http://randomadrress.com/folder/kitchen.mp3,
    which would probably need me to do some kind of txtadd(var, 'http://randomadrress.com/folder/', get(description.get(nm1)), 'mp3'). Of course there are workarounds, but I can't find a straightforward way to do it (to make a code that could easily be understood and modified afterwards, if needed).

    p.s.: even with the txtadd it seems to be working now. The trick is, again, to send the name as %1 as the argument of the variable in the first place. Thanks! If you ever come to the slovenian sea-side, I owe you a beer.


    Edited 2 times, last by Remi (February 23, 2016 at 7:09 PM).

Participate now!

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