How do I load just a JSON string into an array, or use Javascript to directly affect an array?

0 favourites
  • 7 posts
From the Asset Store
A collection of a mix of percussion and orchestral string loops, including 20 musical sketches.
  • I have a set of X an Y coordinates being returned from a javascript function in JSON format. Here's the format:

    [{"x":1,"y":0}, {"x":3.6385872025818373,"y":1.6200033965859757},{"x":0.9503872615007972,"y":1.0555119865570302},{"x":0.4389065636069681,"y":1.3508155051713286},{"x":-0.4163295056303866,"y":3.961110649613818},{"x":-0.4999999999999998,"y":0.8660254037844387},{"x":-3.2222576969514476,"y":2.3411072530278374},{"x":-1.3892938251077656,"y":0.29530351861429965},{"x":3.6385872025818435,"y":-1.6200033965859781}, {"x":1,"y":-2.4492935982947064e-16}]

    If I use JSON.parse the string then converts to:

    [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

    Here's my events:

    I'm trying to then either convert these to an array so I can draw those points on a Drawing Canvas, OR write Javascript to loop through the data and set up the drawing points/lines. I'm unable to get C3 to read either JSON or parsed JSON into an array using Load From JSON String.

    Nor can I get javascript to take control of the canvas. Here's the javascript I use to draw the points, it doesn't even do the alert:

    function drawPoints(jsonString) {
     let points = JSON.parse(jsonString);
     const canvas = runtime.objects.MyCanvas.getFirstInstance();
     canvas.ctx.beginPath();
    
     for (let i = 0; i < points.length; i++) {
     if (i === 0) {
     canvas.ctx.moveTo(points[i].x, points[i].y);
     } else {
     canvas.ctx.lineTo(points[i].x, points[i].y);
     }
     }
    	alert("Done");
     canvas.ctx.closePath();
     canvas.ctx.stroke();
    }
    

    I'd prefer to be able to just turn the X,Y coordinates in my json string into an array. I'm also not trying to save these points to a json to then reload through AJAX, as I already have the data stored in a string.

  • Are you sure GenPoints() returns a string? You cannot really put JSON data into a Construct variable. You can try

    	runtime.globalVars.jsonPoints = JSON.stringify(GenPoints(...));
    
  • Are you sure GenPoints() returns a string? You cannot really put JSON data into a Construct variable. You can try

    > 	runtime.globalVars.jsonPoints = JSON.stringify(GenPoints(...));
    

    As for the canvas, you have to use the proper scripting interface.

    https://www.construct.net/en/make-games/manuals/construct-3/scripting/scripting-reference/plugin-interfaces/drawing-canvas

    GenPoints returns exactly this below. I use stringify in my return on GenPoints.:

    [{"x":1,"y":0}, {"x":3.6385872025818373,"y":1.6200033965859757},{"x":0.9503872615007972,"y":1.0555119865570302},{"x":0.4389065636069681,"y":1.3508155051713286},{"x":-0.4163295056303866,"y":3.961110649613818},{"x":-0.4999999999999998,"y":0.8660254037844387},{"x":-3.2222576969514476,"y":2.3411072530278374},{"x":-1.3892938251077656,"y":0.29530351861429965},{"x":3.6385872025818435,"y":-1.6200033965859781}, {"x":1,"y":-2.4492935982947064e-16}]

    I'd rather save all these points to an array, maybe a dictionary? and then just do the canvas outside of Javascript. But I can't figure out how to get these x,y coordinates into an array

  • To be able to load a json string into the array object it needs to be in a specific format. You can see that format with array.asJSON.

    Something like this should work in converting your json string into a 1d construct array json.

    function convert(in){
     in = JSON.parse(in);
     let out = {c2array:true,size:[0,1,1],data:[]};
     for(let i=0; i<in.length; i++){
     out.data.push([[in[i].x]], [[in[i].y]]);
     out.size[0]+=2;
     }
     return JSON.stringify(out);
    }

    Then after loading that into the array object you can access the xy of the nth point with:

    X=array.at(n*2)

    Y=array.at(n*2+1)

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • To be able to load a json string into the array object it needs to be in a specific format. You can see that format with array.asJSON.

    Something like this should work in converting your json string into a 1d construct array json.

    function convert(in){
    in = JSON.parse(in);
    let out = {c2array:true,size:[0,1,1],data:[]};
    for(let i=0; i<in.length; i++){
    out.data.push([[in[i].x]], [[in[i].y]]);
    out.size[0]+=2;
    }
    return JSON.stringify(out);
    }

    Then after loading that into the array object you can access the xy of the nth point with:

    X=array.at(n*2)

    Y=array.at(n*2+1)

    I'm trying to skip the JSON completely, and just use JS to manipulate the array, but I can't for the life of me get it to touch my array, am I doing something wrong here? my JS seems to be disconnected completely from my C3 sheet, even though I was able to get the values into a JSON. And it's purpose is set to import for events (even though only of my JS can have that label at a time for some reason?):

    function genPointsTest(numPoints) {
     // Choose the array
     const pointsArray = runtime.objects.points_Array.getFirstInstance;
     pointsArray.clear();
     pointsArray.setSize(numPoints + 1, 2);
    
     for (let i = 0; i <= numPoints; i++) {
     let phi = 25;
     let r = phi + 25;
     let x = r * Math.cos(phi);
     let y = r * Math.sin(phi);
    
     // Add the points to the Array
     pointsArray.setAt(x, i, 0);
     pointsArray.setAt(y, i, 1);
     }
    }
    }
  • Probably check the browser's console to see if there are any errors. If that's good you could do some smaller tests with the array scripting api to see what works and what is not doing what you expect.

  • I ended up getting it to work by breaking out my into one line per math equasion in the event sheet. then set the array from the event sheet, pointing to the localVar i had setup.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)