Passing an array to a js library

0 favourites
  • 3 posts
From the Asset Store
Strange Game Ambient Loops 2 library is a meticulously designed & game ready ambience loop library.
  • This is my first foray into utilizing a third party library - github.com/gorhill/Javascript-Voronoi/blob/master/rhill-voronoi-core.js

    The project file is here - dropbox.com/s/dr6abkphr8k4egd/Noise%20graph.c3p

    I have a Construct array of size n,2,1 where x and y coordinates are recorded at y=0 and y=1 respectively, while the x index is each set of coordinates.

    The script requires input in the following format - [{x:300,y:300}, {x:100,y:100}, {x:200,y:500}, {x:250,y:450}, {x:600,y:150}] - which I believe is a JSON array of objects (I'm also not particularly familiar with JSON).

    I've got another possibly unrelated issue where in the project the contents of the Construct array can be utilized fine in the project and when viewed in the debug preview, but when setting text to Array.asJSON the output is empty of contents.

    How should I best proceed to format and pass the data in the Construct array to the script function? I'm not against storing the coordinates in a different manner if it would simplify interfacing with the script, I'm just most familiar with using Construct arrays.

  • Might not be the best if you need to rebuild this object frequently, but simply iterating through your x-axis should be quite easy to get the data needed by your library.

    const arrayInstance = runtime.objects.Array.getFirstInstance();
    let arrayData = [];
    
    for(let i=0; i < arrayInstance.width; i++){
    	arrayData.push({x: arrayInstance.getAt(i, 0),
    					y: arrayInstance.getAt(i, 1)});
    }

    As a side note, the debug text box should have its BBCode functionality deactivated, otherwise it will create weird anomalies trying to display your JSON.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thanks for the reply, seems simple enough. I see where to find the array interface in the scripting documentation now too, I guess I just wasn't familiar enough with that section of the manual.

    I also tried putting it together using the JSON object with -> JSON: Push back "{x:"&array.at(loopindex,0)&",y:"array.at(loopindex,1)&"}", but that resulted in each element being enclosed in double quotes, which I guess indicates a string rather than two separate json objects in that array cell. I could strip them out in the resulting json output with a replace, but that felt pretty clunky and wrong.

    About the BBcode property... ugh. Thanks. Figured the data was fine but it was driving me nuts why it wouldn't display.

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