Forum

Pass a Variable from JS to LogicTree via EntryPoint ?

11 November 2018 16:08
Hello
I would like to know if there is a method to pass a variable from the Javascript to the LogicTree via a Run From Script EntryPoint node ?
I'm working with students in Art and they are not very friendly with Javascript programming, sometimes some functions are easier to set in LogicTree for them.
Thanks for your reply and by the way congratulations for the development of blend4web :)
Christophe
11 November 2018 21:12
I would like to know if there is a method to pass a variable from the Javascript to the LogicTree via a Run From Script EntryPoint node ?
The JS_Callback Node is the only node that can pass variables back and forth between Logic Trees and Javascript, namely with its InParams and OutParams, where InParams means variables that Javascript takes in from the Logic Tree, and OutParams means variables that Javascript passes out to the Logic Tree.

  • InParams = passing variables from Tree to Javascript; InParams 0, 1, 2 defined in the node will be passed to arguments[0][0], arguments[0][1], arguments[0][2], and so on in Javascript.

  • OutParams = passing variables from Javascript to Tree; values set in arguments[1][0], arguments[1][1], arguments[1][2] in Javascript will be passed to OutParams 0, 1, 2 in the node.

  • So, yes, you can pass variables from Javascript to an Entry Point by putting a JS_Callback Node behind the Entry Point. On the Javascript side, you set your variables and then call m_logic.run_entrypoint(). This will run the Entry Point which leads to the JS_Callback Node which pulls in the variables' values from Javascript through its OutParams.

    Example Tree:

    Example Code:
    // I need to pre-define the variables I want to pass outside function scope:
    
    var my_first_variable, my_second_variable, my_third_variable;
    
    function load_cb(data_id, success) {
    
      if (!success) {
          console.log("b4w load failure");
          return;
      }
    
      m_app.enable_camera_controls();
    
      // place your code here
    
      // register the callback:
      
      m_logic.append_custom_callback('my_js_callback_ID', my_js_callback_function);
    
      // set my variables to the values I want to pass:
    
      my_first_variable = 33;
      my_second_variable = "Blend4Web";
      my_third_variable = 1.2345;
    
      // run the entry point:
    
      m_logic.run_entrypoint("Scene", "variable_passing_example");
    }
    
    function my_js_callback_function() {
    
      // This function will pass the variables to the Logic Tree's "OutParams". 
      // This is done by simply putting the variables in arguments[1][0], arguments[1][1],
      // arguments[1][2] and so on for the first, second, third variable to be passed.
    
      arguments[1][0] = my_first_variable;
      arguments[1][1] = my_second_variable;
      arguments[1][2] = my_third_variable;
        
      // Do NOT use a RETURN statement in B4W JS_callback functions! Only use arguments[1] to pass data.
    }
    

    If you run this example, the console should put out

    var 1 {"my_first_variable":33}
    var 2 {"my_second_variable":"Blend4Web"}
    var 3 {"my_third_variable":1.2345}

    proving that the Logic Tree indeed received the variable data from Javascript.

    Note:

    - The variable names in the Tree / Javascript do not need to have the same names, I just did that for sake of clarity.

    - Every Entry Point starts its own logic thread, and you cannot interconnect nodes between threads, but you can use variables to communicate between them, if you set the variables to "GLOBAL" (as seen in the example). Global means sharing variables between different logic threads (started by different Entry Points).
    13 November 2018 15:35
    Thank you very much for this detailed answer.
    I just tried and it works very well
    13 November 2018 21:35
    I gave a detailed answer because the thing with the params can be confusing.

    The terms "In" and "Out" must be seen from the perspective of Javascript, not the Tree. The naming is kind of stupid since the JS_Callback Node says "InParams" where it puts out variables to Javascript and it says "OutParams" where it takes in variables from Javascript.

    The developers should have better called these params "Pass To Javascript" and "Get From Javascript" to make it unmistakably clear what goes in which direction.
    14 November 2018 11:23
    You are right
    it's these two terms "in and out parameters" that put me in doubt.
    it's much clearer now
    Thanks again
     
    Please register or log in to leave a reply.