You are not logged in.

Paranorma

Beginner

  • "Paranorma" started this thread

Posts: 20

Location: Russia, St.Petersburg

  • Send private message

1

Tuesday, August 17th 2010, 3:16pm

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?

Source code

1
2
3
4
5
6
7
8
9
10
11
12
 <plugin name="box"
 		...
         onloaded="action(setupitems);"
 		...
         />
 	 
 <action name="setupitems">
 	removeall();
 	for{var i:int = 0;  i < count; i++) {
 		plugin[box].addItem(name(i), action(action,i); );
 	} 
 </action> 


It's possible?

2

Tuesday, August 17th 2010, 5:04pm

RE: For loop in XML coding


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:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	<!--	_for_action
		private function to execute for loop body action -->
	<action name="_for_action">
		trace("Error in for loop: push/pop stack corrupted"); <!-- this line should never execute -->
	</action>
	<!--	for <initialize.str> <condition.str> <increment.str> <body.str>
		for loop function in the C style which repeatedly iterates the action as long as the condition is true-->
	<action name="for"> <!--initialization.str,condition.str,increment.str,body.str-->
		push(action[_for_action].content);
		set(action[_for_action].content,"%1;");
		_for_action();
		set(action[_for_action].content,"if(%2,%4;%3;_for_action(););");
		_for_action();
		pop(action[_for_action].content);
	</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:

Source code

1
2
3
4
5
			<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:

Source code

1
2
3
4
5
6
			<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:

Source code

1
2
3
4
5
6
7
8
		<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

rdhoore108

Trainee

Posts: 89

Location: Belgium

Occupation: Systems and Network Administrator

  • Send private message

3

Saturday, September 11th 2010, 8:44pm

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

This post has been edited 2 times, last edit by "rdhoore108" (Sep 12th 2010, 11:57am)


michel

Professional

Posts: 1,153

Location: ANDORRA

Occupation: TV

  • Send private message

4

Sunday, September 12th 2010, 12:43pm

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:

Source code

1
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.

rdhoore108

Trainee

Posts: 89

Location: Belgium

Occupation: Systems and Network Administrator

  • Send private message

5

Sunday, September 12th 2010, 1:03pm

Thanks, Michel.

Actually I came to the same conclusion, and updated my post accordingly, as "the elegant solution", about 45 min. before you wrote your reply.

Still I am sorry to have taken your time. Thanks again.
- Ronny D'Hoore

michel

Professional

Posts: 1,153

Location: ANDORRA

Occupation: TV

  • Send private message

6

Sunday, September 12th 2010, 1:08pm

Hi Ronny,

It takes me about 1h to write my answer *g* *g* *g* *thumbdown* ... that explain *squint* ....

SAlut.

7

Friday, April 29th 2011, 1:32pm

I've created a plugin (flash & html5) which implements an elegant for-loop:
Misc plugin; for loops and more
Eg this code traces numbers from -2 to 6, in increments of .5 (ie: -2, -1.5, -1,... 5.5, 6)

Source code

1
2
3
plugin[misc].forloop(i, -2, 6, .5,
          trace(i);
        );


I would not be opposed to having this functionality assimilated in krpano itself. Klaus?

VN2011

Professional

Posts: 1,336

Location: Duluth MN

  • Send private message

8

Friday, April 29th 2011, 2:54pm

what would something like this be used for ?

9

Saturday, April 30th 2011, 9:29am

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).

Zephyr

Professional

Posts: 994

Location: Netherlands

Occupation: Web developer

  • Send private message

10

Wednesday, June 8th 2011, 7:33pm

Ahoeben, your wish came true! Look at the newest beta 14 rc3 :)
3d childrens game using krpano Game
Minify your xml with my krpano XML Compressor
Krpano XML Parsing Error Checklist
Available for hire (krpano xml, javascript, actionscript, flash, php and ipad plugins).

Similar threads