Accessing Array object in runtime

0 favourites
  • 4 posts
From the Asset Store
This is a single chapter from the "Construct Starter Kit Collection". It is the Student Workbook for its Workshop.
  • For reference, I am trying to create a simple slot machine in Construct 3 by utilizing an array to generate a 3x3x1 Array of integer values, each of which will be assigned their meaning elsewhere in the project. I do this using the Array object in runtime, and would like to access its values using javascript.

    When the user runs the slot machine, the following code should execute:

    for(let i=1; i<4; i++){ for(let j=1; j<4; j++){ runtime.objects.Array.At(i,j,1) = Math.random(1,5); } }

    Every time I run this code, the build fails, giving the error message "Assigning to rvalue 3:2".

    I'm sure this is a newbie mistake but if anyone can help me fix this code I would greatly appreciate.

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

    I think you want to use:

    runtime.objects.Array.getFirstInstance() to get the object instance and then use the method:

    setAt(val, x, y = 0, z = 0)

    Set an element in the array at the given X, Y and Z co-ordinates. val must be a number or string. For one or two dimensional arrays, the later parameters can be omitted as they default to 0.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thank you Mikal, so I now have the following code:

    for(let i=1; i<4; i++){ for(let j=1; j<4; j++){ runtime.objects.Array.getFirstInstance(); runtime.objects.Array.setAt(Math.random(1,5),i,j); } }

    This no longer fails to build but using debug layout I can see the values in the Array are never actually set. I've played around with the formatting and am still not sure how exactly to express this (again, newbie, sorry). What is the proper syntax?

  • You need to get the reference to the instance, then apply the methods to that instance for example (untested):

    // Get the first instance of the Array object
    let firstArray = runtime.objects.Array.getFirstInstance();
    for(let i=1; i<4; i++){ for(let j=1; j<4; j++){
    firstArray.setAt(Math.random(1,5),i,j);
    } }
    
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)