Forum

unload function

26 February 2015 19:57
Hi,
I am trying to re-use the load/unload functions like in your cartoon interior example, in order to load/unload additional information into my Scene (like text with arrows… dimensions, etc… this loaded scene may contain several Objects).
I have coded the load button and function and it works as expected, but I have problems with the unload function…
How do I get the correct id for the objects of the latest .json file I have loaded?? This id is a number as explained in the API, and I don't know how to get theses… Do I have to unload every single Object that is loaded as part of this .json file? can I unload all of them at once, keeping only the objets from the startup file?
Thanks for your help!

David
David Duperron
STAT Marine - www.stat-marine.com
26 February 2015 20:17
There must be a way to store the id of the latest loaded data somewhere (the returns of the "load" function that I used by clicking on the "load additional content" button?), but I am really not expert enough in javascript to sort it out…
David Duperron
STAT Marine - www.stat-marine.com
26 February 2015 20:24
Hi!
Indeed, the loaded data ID is returned by the load() method. You can push it in some array, initialized as "global" (module's internal) variable, and later use the values from that array in your unload() functions.
var _ids = [];


_ids.push(data.load("scene1"))
_ids.push(data.load("scene2"))
_ids.push(data.load("scene3"))


for (var ii = 0; ii < _ids.length; ii++)
    data.unload(_ids[ii]);
26 February 2015 20:40
Well… that's where my skills are a little too weak…
here is the code for the buttons I want to use:

// init show/hide info layers button
function init_show_hide() {

var load_add_scene = document.getElementById("load_add_button");
var unload_add_scene = document.getElementById("unload_add_button");  
    
// this function loads an external .json file into the scene
load_add_scene.addEventListener("click", function(e) {
        m_data.load("Scene_Demo_Arrows.json", null, null, false, false);
    });
// this function SHOULD unload the external .json file from the scene this is where I need help... I would like to store the id of the previously loaded scene, and re-use this ID here.
unload_add_scene.addEventListener("click", function(e) {
        m_data.unload();
    });
}


The solution must be very simple but I really have to learn more about javascript coding…
David Duperron
STAT Marine - www.stat-marine.com
26 February 2015 20:53
Try something like that:

function init_show_hide() {
    
    var load_add_scene = document.getElementById("load_add_button");
    var unload_add_scene = document.getElementById("unload_add_button");  
    
    // will keep loaded scene ID here
    var arrows_scene_id = 0;
    
    load_add_scene.addEventListener("click", function(e) {
        arrows_scene_id = m_data.load("Scene_Demo_Arrows.json", null, null, false, false);
    });

    unload_add_scene.addEventListener("click", function(e) {
        // check that the scene is loaded (0 unloads all scenes)
        if (arrows_scene_id != 0)
            m_data.unload(arrows_scene_id);
    });
}
26 February 2015 21:06
Thanks a lot!!
I was close to that on my side, I'm getting better…
However, there is still a small issue… if a click again on the same "load" button with my additional scene already loaded, the unload button does not work anymore…
I can find a way to hide the load button once it is loaded and prevent the user to click again! But there must be another "javascript" reason for that…
David Duperron
STAT Marine - www.stat-marine.com
26 February 2015 21:29
Ok, you need additional check before load method and reset ID after unload:

load_add_scene.addEventListener("click", function(e) {
    // if not loaded load it now
    if (arrows_scene_id == 0)
        arrows_scene_id = m_data.load("Scene_Demo_Arrows.json", null, null, false, false);
});

unload_add_scene.addEventListener("click", function(e) {
    // check that the scene is loaded (0 unloads all scenes)
    if (arrows_scene_id != 0) {
        m_data.unload(arrows_scene_id);
        arrows_scene_id = 0;
    }
});
26 February 2015 22:15
Ah! I did not think about this… I have put in place the solution I was planning: hiding/showing the interface buttons according to the status of the loaded file… I must say I like it also. This avoids having too many useless buttons on the interface. Perhaps a mix of the two solutions would be the best solution, the "cleaner" one!
Thanks very much for your help as usual!
David Duperron
STAT Marine - www.stat-marine.com
 
Please register or log in to leave a reply.