Forum

Logic node to JS

12 March 2018 21:48
Hi again,
thanks for the help with my previous project, i am now trying to work with the js callback function. I have a scene where i want to active a js callback to change the camera, however i keep getting the error that b4w cannot find the module "logic_nodes".
I'm not sure what i'm doing wrong, but would really appreciate the help! thank you
13 March 2018 21:12
Well, from what I can tell, you put
var m_logic = require("logic_nodes");
into code.js, but you need to properly load the logic_nodes module before "requiring" it, so your main file (the file that loads code.js) should contain something like this:
<script type="text/javascript" src="path_to_the_logic_nodes_file/logic_nodes.js"></script>
You need to provide the correct path to the logic_nodes.js file.

Also, it's difficult to help seeing just one file, it would be easier if you link to your project or attach it.
13 March 2018 21:13
I solved this issue! However i have a problem with this now, the console is constantly spamming 0 til button press, i'd really appreciate some help with this part, pretty sure it's something up with my nodes. i've attached the deploy files, the .blend and my JS.
Thanks guys!
p.s thanks for your response blend4life
13 March 2018 22:36
Hi again,
thanks for the help with my previous project, i am now trying to work with the js callback function. I have a scene where i want to active a js callback to change the camera, however i keep getting the error that b4w cannot find the module "logic_nodes".
I'm not sure what i'm doing wrong, but would really appreciate the help! thank you

You are using ES6 import. This means that you should not use require in this file at all. Actually this is not a rule for all js-applications, but specifically for Blend4Web, because b4w has its own require which can collide with other require from CommonJS. In the future we will drop require approach.
You can find logic_nodes and other modules in b4w variable:

var m_logic = b4w.logic_nodes;
Alexander (Blend4Web Team)
twitter
13 March 2018 22:52
I solved this issue! However i have a problem with this now, the console is constantly spamming 0 til button press, i'd really appreciate some help with this part, pretty sure it's something up with my nodes. i've attached the deploy files, the .blend and my JS.

All is corrrect:
Look at you Switch node: R1 equals to 0 until you clicked; while no click Switch node reroutes to the callback that prints R1, which value is 0.

What do you expect?
Alexander (Blend4Web Team)
twitter
13 March 2018 23:09
Ahh you are right, i am sorry, one last thing, i have changed from requires to the approach you have suggested, how do i go about registering the module?
previously i had used "b4w.require("webtest").init();" what should i use now?
thanks so much you guys, i really appreciate all the help!
16 March 2018 10:27
See simple_app.js in SDK:
"use strict"

import b4w from "blend4web";

// import modules used by the app
var m_app       = b4w.app;
var m_cfg       = b4w.config;
var m_data      = b4w.data;
var m_preloader = b4w.preloader;
var m_ver       = b4w.version;
var m_anim      = b4w.animation;
var m_cont      = b4w.container;
var m_mouse     = b4w.mouse;
var m_scenes    = b4w.scenes;

var _previous_selected_obj = null;

// detect application mode
var DEBUG = (m_ver.type() == "DEBUG");

// automatically detect assets path
var APP_ASSETS_PATH = m_cfg.get_assets_path("simple_app");

/**
 * export the method to initialize the app (called at the bottom of this file)
 */
function init() {
    m_app.init({
        canvas_container_id: "main_canvas_container",
        callback: init_cb,
        show_fps: DEBUG,
        console_verbose: DEBUG,
        autoresize: true
    });
}

/**
 * callback executed when the app is initialized 
 */
function init_cb(canvas_elem, success) {

    if (!success) {
        console.log("b4w init failure");
        return;
    }

    m_preloader.create_preloader();

    // ignore right-click on the canvas element
    canvas_elem.oncontextmenu = function(e) {
        e.preventDefault();
        e.stopPropagation();
        return false;
    };

    load();
}

/**
 * load the scene data
 */
function load() {
    m_data.load(APP_ASSETS_PATH + "simple_app.json", load_cb, preloader_cb);
}

/**
 * update the app's preloader
 */
function preloader_cb(percentage) {
    m_preloader.update_preloader(percentage);
}

/**
 * callback executed when the scene data is loaded
 */
function load_cb(data_id, success) {

    if (!success) {
        console.log("b4w load failure");
        return;
    }

    m_app.enable_camera_controls();

    // place your code here
    var canvas_elem = m_cont.get_canvas();
    canvas_elem.addEventListener("mousedown", main_canvas_click, false);
    canvas_elem.addEventListener("touchstart", main_canvas_click, false);
}

function main_canvas_click(e) {
    if (e.preventDefault)
        e.preventDefault();

    var x = m_mouse.get_coords_x(e);
    var y = m_mouse.get_coords_y(e);

    var obj = m_scenes.pick_object(x, y);

    if (obj) {
        if (_previous_selected_obj) {
            m_anim.stop(_previous_selected_obj);
            m_anim.set_frame(_previous_selected_obj, 0);
        }
        _previous_selected_obj = obj;

        m_anim.apply_def(obj);
        m_anim.play(obj);
    }
}

init();

If you want to use any function/variable from this module in another module, just do:
export var var_blabla = 123;
export function hello() {
    console.log("blabla!")
}
And then import in another module:
import * as m_blabla from "./blabla.js";
console.log(m_blabla.var_blabla)
m_blabla.hello();

There are many forms of using import.
Alexander (Blend4Web Team)
twitter
23 March 2018 19:56
Thank you very much!
 
Please register or log in to leave a reply.