Sie sind nicht angemeldet.

1

Montag, 16. August 2021, 18:00

how to set global.variable with dynamically created name

Hi, let's say we define a global variable:

Quellcode

1
2
3
<action name="def_global_var">
    def(global.variable_to_count,  int, 0);
</action>

which we want to set by picking with dynamically created name:

Quellcode

1
2
3
4
5
6
7
8
9
<action name="set_global_var">
    txtadd(dynamic_var_name, 'variable_to', '_count');
    
    <!-- what's next, how to set var -->

    set(global.get(dynamic_var_name), get(something.matter));          <!-- NOT work -->
    set(global[get(dynamic_var_name], get(something.matter));          <!-- NOT work -->
    set(global[get(dynamic_var_name].value, get(something.matter));    <!-- NOT work -->
</action>

The question is, which syntax should be in order to set the global value?

By the way, which way to define a global.variable outside of the action tag.

Thx,

Beiträge: 1 117

Wohnort: Poland, Europe

Beruf: krpano developer : virtual tours : the cms4vr owner

  • Nachricht senden

2

Montag, 16. August 2021, 18:10

The intention is not clear from the code but have you tried it?


Quellcode

1
copy(dynamic_var_name, something.matter);




Piotr
Your own professional, online cloud tool for creating virtual tours - www.cms4vr.com

facebook page :: youtube :: wiki.cms4vr.com

cms4vr team *thumbsup*

3

Montag, 16. August 2021, 18:48

set(global.dynamic_var_name, "myvarname");
set(global.source_variable, "hello");

copy(get(global.dynamic_var_name), global.source_variable);

trace(myvarname); // --> hello
trace(get(global.source_variable)); // --> hello

note:
something.matter is not a simple variable name
its an object "something" with a property "matter"
would be possible, but

4

Dienstag, 17. August 2021, 11:15

Thank you pacerywirtualne and indexofrefraction.

Zitat

The intention is not clear from the code

The point is to set global variable with accessing to it through the generated name.

Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<action name="def_global_var">
    def(global.variable_to_count,  int, 0);
</action>

<action name="set_global_var">
    txtadd(dynamic_var_name, 'variable_to', %1);

<!-- let's say %1 = '_count' in this example
so the dynamic_var_name = 'variable_to_count' -->

<!-- great we have the  global variable name as generated variable
how to access the global variable through the generated variable

what's next, how to set global.variable_to_count with dynamic_var_name -->

    set(global.get(dynamic_var_name), get(whatever));          <!-- NOT work -->
    set(global[get(dynamic_var_name], get(whatever));          <!-- NOT work -->
    set(global[get(dynamic_var_name].value, get(whatever));    <!-- NOT work -->
</action>


something.matter as well as whatever it's just for example )

Beiträge: 1 117

Wohnort: Poland, Europe

Beruf: krpano developer : virtual tours : the cms4vr owner

  • Nachricht senden

5

Dienstag, 17. August 2021, 12:21

This?

Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<action autorun="onstart">
	set(var_int,0);

	for(set(i,0), i LT 3, inc(i), 
        txtadd(dynamic_var_name, 'variable_to', get(i));
        // and now
        calc(dynamic_var_name, 'The variable no '+i);
	); 

	<!-- checker -->
	for(set(i,0), i LT 3, inc(i), 
        trace('Index : ',get(i),' Text: ',calc('variable_to'+i));
	); 
</action>


Trace:


INFO: Index : 0 Text: variable_to0
INFO: Index : 1 Text: variable_to1
INFO: Index : 2 Text: variable_to2

Piotr
Your own professional, online cloud tool for creating virtual tours - www.cms4vr.com

facebook page :: youtube :: wiki.cms4vr.com

cms4vr team *thumbsup*

6

Dienstag, 17. August 2021, 13:10

Zitat

This?

Don't think so)

spacerywirtualne and indexofrefraction thanks to you a solution founded.
Not the most graceful, but works:

Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<action name="def_global_var">
    def(global.variable_to_count,  int, 0);
</action>

<action name="set_global_var">
    txtadd(dynamic_var_name, 'global.variable_to', %1);

<!-- let's say %1 = '_count' in this example
so the dynamic_var_name = 'global.variable_to_count' -->

<!-- great we have the global variable name as generated variable -->

    trace(get(dynamic_var_name));        <!-- INFO: 0 -->

    set(current_count, get(dynamic_var_name));
    inc(current_count);
    set(get(dynamic_var_name), get(current_count));

    trace(get(dynamic_var_name));        <!-- INFO: 1 -->
</action>


Still hope that someone will find the better syntax's solution.

Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von »zabelin« (17. August 2021, 16:21) aus folgendem Grund: just looks like code may be simpler


Beiträge: 1 117

Wohnort: Poland, Europe

Beruf: krpano developer : virtual tours : the cms4vr owner

  • Nachricht senden

7

Dienstag, 17. August 2021, 15:03

Hi
Don't think so)
This was a working example of how to create variables dynamically and assign specific values to them. Now it's up to you to take it yourself and rewrite it your way.


*thumbup*
Your own professional, online cloud tool for creating virtual tours - www.cms4vr.com

facebook page :: youtube :: wiki.cms4vr.com

cms4vr team *thumbsup*

8

Dienstag, 17. August 2021, 15:04

Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
<!-- i think you need action.scope defined, otherwise global.var might not work -->

<action name="def_global_var" scope="local"> 
    def(global.variable_to_count,  int, 0);
</action>

<action name="set_global_var" scope="local" args="name"> 
    calc(local.dynamic_var_name, 'global.variable_to' + name);
    trace(dynamic_var_name,'=',get(dynamic_var_name));  
    inc(get(dynamic_var_name));
    trace(dynamic_var_name,'=',get(dynamic_var_name));  
</action>