Operations on variables passed from js

  • Hello, I have a doubt since I need to do some simple operations on a variable that is passed from the js, ( viewer.addVariable("serial",1);)

    I can get that variable by using the get action. So for example
    showtext(get(serial),infostyle); effectively shows the 1 value on screen. Now what I want is to add a value to the serial variable...it could be done by using the add action
    add(get(serial),1);
    However it would not change the actual get(serial) value how could that?? The confusion is because the add function works over the variable that is passed as the first argument ( like by reference in programming languages). But by doing the last action nothing is changed, what I mean is that if I use the following code:

    add(get(serial),1);
    showtext(get(serial),infostyle);

    1 it's shown on screen, and nothing changes that... is there something like local variables in krpano??? or how could I add a value to a global variable and then retrieve the changed value?.
    Thanks a lot.

  • Zitat

    add(get(serial),1);
    However it would not change the actual get(serial) value how could that?? The confusion is because the add function works over the variable that is passed as the first argument ( like by reference in programming languages). But by doing the last action nothing is changed, what I mean is that if I use the following code:

    OK

    viewer.addVariable("serial",1);
    this would be nothing more than a string equaling 1

    unless you do

    var serial = 1;
    in your js

    viewer.addVariable("serial",serial);
    to pass a real js variable into krpano.

    if want to change the value of serial of your js you could do
    js(setval(serial,2));

    js only calls a function so we need one setup for us

    javascript function
    function setval(var) {
    serial = var;
    krpano.set("serial", serial);
    }

    and our js function updates the krpano value.

    I have no idea what you are trying to do. So there are probably easier methods. But at least you can cross talk from krpano, to js and back again.

    see here for more details.
    https://krpano.com/docu/js/

  • Hi,

    the get() is wrong here:

    add(get(serial),1);

    the add() action needs a variable name to know where to store the result,
    and when using get(var) then only the content of the variable will be passed to the action,
    (btw - the add() action is automatically getting the content from given variables)

    e.g. when serial=1 then the code line from above will become to:

    Code
    add(1,1)


    and means:

    Code
    1 = 1 + 1


    which doesn't makes sense of course

    correct would be just:

    Code
    add(serial,1)


    and means:

    Code
    serial = get(serial) + 1
    Code
    serial = 1 + 1

    e.g. you could also use the 3-parameter syntax:

    Code
    add(serial, get(serial), 1);


    or

    Code
    add(serial, serial, 1);


    which will finally become

    Code
    add(serial, 1, 1);


    and means:

    Code
    serial = 1 + 1

    btw - here a very good and very well written tutorial from Pinsane/Steve about the krpano action:
    A Tutorial on Krpano Action Arguments (pt 1)

    best regards,
    Klaus

Jetzt mitmachen!

Sie haben noch kein Benutzerkonto auf unserer Seite? Registrieren Sie sich kostenlos und nehmen Sie an unserer Community teil!