access array from script

0 favourites
  • 11 posts
From the Asset Store
Here is the wonderful & amazing script for your website or fun with maths.
  • Please someone help, as this issue is driving me crazy for days in a row.

    I am very new to scripting, but been able to make it right with the help of gpt, but now, even gpt is tired of this issue and cannot resolve it further.

    I need to update a textbox called textDebug with the first value of the array called NEWScheduleArray. Whatever I (and gpt) do it always gives the same result "scheduleArray.get is not a function"

    right now, after 1000 changes, the code goes like this:

    // Access the NEWScheduleArray instance

    const scheduleArray = runtime.objects.NEWScheduleArray.getFirstInstance();

    // Retrieve the value at position [0, 0] from the array

    const valueAt00 = scheduleArray.get(0, 0); // Correct method to use

    // Access the textDebug Text object instance

    const textDebugObject = runtime.objects.textDebug.getFirstInstance();

    // Update the text of textDebug with the retrieved value

    textDebugObject.text = valueAt00.toString(); // Convert to string if necessary

    What is wrong and how to capture the value 0,0 inside the array?

    Many thanks in advance

  • const valueAt00 = scheduleArray.get(0, 0); // Correct method to use

    It's the incorrect method. The correct method is scheduleArray.getAt(0,0)

    In such cases I always recommend to consult the manual over gpt, because gpt will not be familiar with the API of Construct.

    construct.net/en/make-games/manuals/construct-3/scripting/scripting-reference/plugin-interfaces/array

  • Older thread, but I have a similar issue, except I am already using getAt.

    My array is called ARRAY_TotalDLs. It has been filled with JSON data beforehand using the inbuilt Load. The array is global.

    I want to access it at specific numbers, using the variables j and i.

    So I do:

    Array_TotalDLs.getAt(j,i);

    I get a "ReferenceError: ARRAY_TotalDLs is not defined".

    I have checked the spelling numerous times, but with no luck, including copy pasting the name directly to ensure no errors. The Array is created from the beginning, so it most definitely exists. I have console logs that ensure that I enter the code block and that the JSON data is correct, and Load seems to succeed.

    What am I doing wrong?

  • It's not possible to tell without seeing all your code. It means that variable is undefined though. You need to declare variables before using them - Construct does not declare anything for you (other than occasionally passing 'runtime' and 'localVars'). If you are getting stuck on things like variable declarations I would recommend going through the tutorial course Learn JavaScript in Construct which covers that.

  • Firstly, thank you for the answer! I am definitely still dealing with this issue.

    Essentially I use javascript and fetch functions to communicate with a server to receive JSON data and then load it into the array object, but now need to console log the entire array (and will need to access specific points of it later anyway) to check if all data went in correctly.

    My code in that code block (within an event sheet) looks like this:

    console.log("Entered Printing function.");

    let stringtemp = "";

    for (let i = 0; i< 26; i++) // 9 elements, index goes to 8.

    {

    stringtemp = "";

    for (let j = 0; j< 19; j++) // 20 elements, index goes to 19

    {

    // Why does this function call not work?

    stringtemp = stringtemp + "<" + ARRAY_TotalDLs.getAt(j, i) + "> ";

    }

    console.log(stringtemp);

    }

    I have already gone through the javascript Construct tutorial, as well as read the Array API documentation. I find them to not be precise/detailed enough on this, personally (Mostly lacking practical examples).

    Does this mean I cannot access the array that I created using Constructs inbuilt features? As in my understanding, if I declare a new array within javascript, it will be an empty new array, but I need the information I loaded into the array object beforehand.

  • Where is there a variable that declares ARRAY_TotalDLs?

    There doesn't seem to be one. That's why it's not working.

  • I can declare an empty array called the same as the array object, but upon declaration, it will be empty, as it was just made and is in my understanding not connected to the array object into which I loaded in my data.

    Is it then not possible to retrieve the data of the array object from javascript? Or is there a special declaration that will connect the newly declared array with the array object I already have?

  • If you intend to refer to a Construct object, you need to use runtime.objects.ObjectName. This is covered in part 11 of the 'Learn JavaScript in Construct' tutorial series.

  • Having adjusted my code to this:

    console.log("Entered Printing function.");

    let stringtemp = "";

    for (let i = 0; i< 26; i++) // 9 elements, index goes to 8.

    {

    stringtemp = "";

    for (let j = 0; j< 19; j++) // 20 elements, index goes to 19

    {

    stringtemp = stringtemp + "<" + runtime.objects.ARRAY_TotalDLs.getAt(j, i) + ">";

    }

    console.log(stringtemp);

    }

    I now receive a "TypeError: runtime.objects.ARRAY_TotalDLs.getAt is not a function".

  • That's because you're calling getAt() on the object type, not the instance. I'd suggest re-reading part 11 of the tutorial series - it covers all this.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Got it, I think I found the section of that part that I was looking for - I wasn't aware Array objects also worked with instances this way. Thank you for the answers!

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