How to manipulating Arrays in Construct 3 Using JavaScript?

0 favourites
  • 12 posts
From the Asset Store
Casino? money? who knows? but the target is the same!
  • To manipulate global variables from the Construct 3 event sheet using JavaScript, we do

    // Fetched the data from the Construct 3 global variable to JavaScript
    var jsVariable = runtime.globalVars.c3Variable;
    
    // Store the data in the Construct 3 global variable from JavaScript
    runtime.globalVars.c3Variable = jsVariable;
    
    

    Similarly, What is the direct way to manipulate Arrays in Construct 3 Using JavaScript? What expression I should use?

    Let's say I have an array (JSON array converted into the native array) in Construct 3.

    I want to use JavaScript to add elements to the array, remove elements, or do other things with the array.

  • Thanks for your response, I visited that page already, but I didn't understand it, so I asked the question. I'm getting on colsole

    a reference error: My_Array is not defined

    // My_Array is an array from c3 event (converted from JSON array),I put it on start of the layout, after using 1sec wait.
    
    var valueAt00 = My_Array.getAt(0, 0);
    
    console.log("Value at (0, 0):", valueAt00);
    
  • a reference error: My_Array is not defined

    You need to define your array in Javascript.

    You can have multiple instances of the same array object, so you have to specify which one. construct.net/en/make-games/manuals/construct-3/scripting/scripting-reference/object-interfaces/iinstance

    const JS_myArray = runtime.objects.My_Array.getFirstInstance();
    var valueAt00 = JS_myArray.getAt(0, 0);
    

    There are a bunch of JavaScript examples/tutorials built in, you should look through them.

    Also if you're a beginner, my recommendation is to never use the wait action, ever. It can be a shortcut for certain things when you know what you're doing, but it is never necessary and it can break or otherwise make your project behave outside your expectations in a hundred different ways. If you insist on using wait, just remember that it only delays actions, and does not stop or pause the event sheet from running.

  • Thanks oosyrag It worked.

    Also if you're a beginner, my recommendation is to never use the wait action, ever.

    Please correct me if I'm mistaken, I used wait() for a specific reason. Here in my case, the C3 array is converted from a JSON array, If I don't include wait here, the array always shows 0. However, when I do include wait, the array provides the correct output.

    Or I should use here something else like "wait for signal" or "wait for the previous action to complete"?

  • You should put that script right after the array load action in event 3, because that is when it will be available. Wait is not necessary. In the theoretical event the disk is under load and it takes more than 0.1 seconds for ajax to read the json, your event as written would also break, since the array would not have been loaded at that point 0.1 seconds after the layout starts. If you put the script in event 3, which is triggered by the data becoming available, it will always work because event 3 will run when the data is available.

  • But in some cases, the array is stored in a separate event, and we need to call it from a different event sheet. In such situations, what is the recommended approach?

  • I do not understand your situation. Arrays are global. It doesn't matter what event sheet you refer to it from.

  • If I may, could you please assist me with this?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I do not understand your situation. Arrays are global. It doesn't matter what event sheet you refer to it from.

    Is it mandatory to place any action that depends on or fetches data from an array after this particular action?

    + AJAX: On "My_Array" completed

    -> My_Array: Load from JSON string AJAX.LastData

    If the answer is yes, then I've been mistaken all along, because I consistently store my arrays in a separate event sheet named "Array," without mixing them with any other code. :/

  • Yes, if you intend on running said action that depends on or fetches data from the array as soon as possible after loading the array JSON data into memory.

    Practically though, no user should be able to click on something or do an action faster than your device can load the array on start of layout.

    If you want to be really proper about it, start with your buttons or user interface disabled, and add an action to enable them in that same event with on ajax finished and array loaded. This guarantees the user can't do anything until at least the array is ready. For automated tasks and functions, yes they should be called in that event as well, so they run whenver the data is available.

    Separate event sheets or includes has nothing to do with anything, you can do that or not, up to you.

    Edit: Event sheets don't store arrays...? You're definitely misunderstanding something.

  • Thank you for the detailed explanation! :) Now, I understand.

    Edit: Event sheets don't store arrays...? You're definitely misunderstanding something.

    😅 No, what I meant to say was about the Array Code, as shown below.

    + System: On start of layout

    -> AJAX: Request MY_JSONArray.json (tag "My_Array")

    + AJAX: On "My_Array" completed

    -> My_Array: Load from JSON string AJAX.LastData

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