You are not logged in.

Dear visitor, welcome to krpano.com Forum. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

herrpedro

Trainee

  • "herrpedro" started this thread

Posts: 143

Location: Lisbon

Occupation: Programmer/analyst

  • Send private message

1

Saturday, November 2nd 2019, 10:57pm

returning a value from a javascript action

Hi all

so far all examples i got was to call functions and then change something in krpano plugins or positions (there is one example in the skin file
but i did not find any to "return a value"


Source code

1
2
3
4
5
6
7
<action name="register_javascript_actions2" type="Javascript" autorun="preinit">
  krpano.actions.getlanguagecontent = function( labelId , type , outresult)
    {
		args[2]='ola';
		outresult = 'ola';
}
</action>


when i call

Source code

1
getlanguagecontent(get(hotspot[%1].hsname) , 'auditorio' , outputfromfunction);


i was hoping to get 'ola' in the outputfromfunction variable bit it stays null all the time
tried with args and setting the variable directly and i always get null
i see the function running because i added some "console.log" and it displays

thank you for the help

herrpedro

Trainee

  • "herrpedro" started this thread

Posts: 143

Location: Lisbon

Occupation: Programmer/analyst

  • Send private message

2

Saturday, November 2nd 2019, 11:10pm

i keep digging
added a console log to see the "args" array and i get
at 0 the "envelope" action name so.. i guess it is not this way
there is no point in setting args[3] to return the output since i'm not getting the 2 input arguments

["register_javascript_actions2"]
0: "register_javascript_actions2"

3

Sunday, November 3rd 2019, 9:11am

the args array is used in type=Javascript actions.
your register_javascript_actions2() is type=Javascript,
but krpano.actions.getlanguagecontent() is working as a normal js function:

Source code

1
2
3
4
5
6
7
8
9
10
<action name="register_javascript_actions" type="Javascript" autorun="preinit">
	krpano.actions.examplefunc = function(dest, a, b) {
		krpano.set(dest, a * b);
	};
</action>

<action autorun="onstart">
	examplefunc(test, 5, 2);
	trace(test); // 10
</action>


important:
if you create functions like that, you must check/verify the variable type of the input variables.
ie. if you would create an add function and do a+b you have to ensure that a and b are numbers first!

and... its not a must, but in krpano xml actions the return value target is normally the first argument.
i.e. https://krpano.com/docu/actions/#mul

*smile*

This post has been edited 5 times, last edit by "indexofrefraction" (Nov 3rd 2019, 8:54pm)


herrpedro

Trainee

  • "herrpedro" started this thread

Posts: 143

Location: Lisbon

Occupation: Programmer/analyst

  • Send private message

4

Sunday, November 3rd 2019, 1:06pm

hi,

thank you for your reply.

That wont do (i think) in my case because of atomic execution. All the function will execute synchronous but i think that the get variable in krpano variable, after, the execution of the jsfunction, can get a different value if some executions occur at the same time.

I turned into a jsget with 2 "envelope" actions since i can pass %1, %2, etc to jsget but i could not manage to pass a variable, neither by name, neither with get but %3 was fine

i had a problem while ago with detecting if the variable was defines and i was hoping to solve it today with a js action.

i was hoping to get this code inside my xml but i couldn't... may be later i'll understand the connections with parameters bewteen actions and javascript actions and plain javascript
from time to time i try and i always end by solving it but not in the way which i think is the correct

thanks again