For loop in XML coding

  • Hi, Klaus!

    Can you make for(var i:int = 0; i < count; i++) {...} loop action in XML like in ActionScript?

    It's possible?


  • It's possible?

    It would be great if Klaus could add looping constructs to the krpano language. In the meantime, the krpano's language is flexible enough to write your own for-loop action:


    Here is how you would call the above for-loop action. You would have to use krpano action syntax for the initialization, loop condition, and incrementing as well as the loop actions:

    Code
    <action name="for_test1">
    					for (set(a,0),a LT 5,inc(a),
    						trace("a=",get(a));
    					);
    			</action>


    Multiple statements can be put into the initalization, increment and body sections:

    Code
    <action name="for_test2">
    				for (trace(initializing);set(a,0),a LT 5,trace(incrementing);inc(a),
    					trace("a=",get(a));
    					trace("looping");
    				);
    			</action>


    You can nest for loops as well. Note that if you loop over a large number of iterations you will run into krpano's actions overflow limitation (more info here). You can work around this with a wait(0} statement:

    Code
    <action name="for_test3">
    			for(set(a,-360),a LE 360,inc(a,30),
    				for(set(b,-360),b LE 360,inc(b,30),
    					trace("looping a=",get(a)," b=",get(b));
    				);
    				wait(0);   <!--BUGFIX appears to be necessary to prevent action overflow -->
    			);
    		</action>


    hope this helps

    steve

  • Steve,

    Thanks very much for this information.

    I tried to implement your for loop in my project, but I got stuck on a limitation of the "if" condition. In your example, you write "a LT 5". This works fine.

    But I need something like "a LT get(enum[panos].item.count)", and it won't try to resolve this, but uses get(enum[panos].item.count as a string instead, so numerical 0, so the "if" always results in false...

    Is there any way to enhance your "for" action so that it could work with variables in the "if" condition?

    Thanks in advance!

    UPDATE: I found a solution!!!

    The solution is to split the if condition into three parts, then one can safely use get() instead of constants when calling the "for" action:

    <action name="for"> <!--initialization.str, condition.var1, condition, condition.var2, increment.str, body.str-->
    push(action[_for_action].content);
    set(action[_for_action].content,"%1;");
    _for_action();
    set(action[_for_action].content,"if(%2 %3 %4, %6; %5; _for_action(););");
    _for_action();
    pop(action[_for_action].content);
    </action>

    <enum name="panos">
    <item name="Pano001" morestuff="more stuff" />
    <item name="Pano002" morestuff="even more stuff" />
    <item name="Pano003" morestuff="blahblah" />
    </enum>

    <action name="for_test1">
    for (set(a,0), a, LT, get(enum[panos].item.count), inc(a),
    trace("a=",get(a)); trace(enum[panos].item.name);
    );
    </action>

    Maybe there is a more elegant solution?

    UPDATE 2: the elegant solution *smile*

    The solution is to write "a LT enum[panos].item.count" instead of "a LT get(enum[panos].item.count)". Then all what I wrote here is not required... *g*

    - Ronny D'Hoore

    Edited 2 times, last by rdhoore108 (September 12, 2010 at 11:57 AM).

  • Hi Ronny,

    But I need something like "a LT get(enum[panos].item.count)", and it won't try to resolve this, but uses get(enum[panos].item.count as a string instead, so numerical 0, so the "if" always results in false...

    As I know, the use of get(....) inside the condition of an IF action is not possible (the get() is not resolved and it becomes a string).
    I think that the correct way to do would be:

    Code
    if( a LT enum[panos].item.count , trueaction , falseaction );

    As a resum, I think we can say something like:
    if( variable_A , trueaction , falseaction );
    means: if the value of variable_A == true , do trueaction , else do falseaction .
    if( variable_A OPERATOR value , trueaction , falseaction );
    means: if the value of variable_A OPERATOR value ( value is a number or a string ) , do trueaction , else do falseaction .
    if( variable_A OPERATOR variable_B , trueaction , falseaction );
    means: if the value of variable_A OPERATOR the value of variable_B , do trueaction , else do falseaction .

    Hope thi help...

    SAlut.

  • A for-loop? From wikipedia: A for loop is a programming language statement which allows code to be repeatedly executed.

    It could for example be used to iterate over all plugin or hotspot nodes to set a property (eg making all hotspots have alpha=10). Or to iterate over all scenes. Or, you could use it to create (or remove) exactly 10 plugins.

    Yes, there are hoops one can jump through currently to do the same thing without this plugin, but this is a lot easier and more readable. It is such a basic programming construct that it should have really been part of krpano already (and the open-source(!) implementation is tiny and efficient).

Participate now!

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