Copy array object via JS

0 favourites
  • 3 posts
From the Asset Store
Hand-painted tiles, objects, animated objects, and background to build a colorful Mayan civilization environment.
  • I'll preface this by saying that I'm a neophyte to Construct and I'm just now getting into JS insertion within the event sheet.

    Within my game I have an Array object called "Solution". In my script I would like to make a copy of said array.

    To understand it I would like to do this (written in normal JS):

    let solution = [1, 2]
    let solutionCopy = [...solution]
    

    Giving a reading to the manual I tried to write some code, but it is reported in red, and therefore wrong:

    let solutionCopy = [...runtime.objects.Solution]
    

    Also subsequently, I can't access the length of an Array object, via JS code:

    for(let i = 0; i < runtime.objects.TempRow.lenght; i += 0) {
    

    I think I'm still unclear on how to properly access the data of an Array object via JS. Can anyone help me and give me some explanation?

    Thanks

  • It looks like you're mixing up object types and instances. runtime.objects.XYZ is always an IObjectClass, from which you can get an instance, e.g. via getFirstInstance(). That gives you an instance, which you can then call instance methods on, such as getAt() for IArrayInstance.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thank you for your response Ashley.

    So if I understand correctly the only way to copy an array object into an array is this?

    let solutionObj = runtime.objects.Solution.getFirstInstance()
    
    let tempArray = []
    
    for (let i=0; i < solutionObj.width; i++) {
    	tempArray.push(solutionObj.getAt(i, 0))
    }

    Or are there better ways to make a copy?

    Checking on the page you linked me to, it doesn't look like I can directly reference the array and copy it with a single string (example: let tempArray= [...solutionObj]), but I need to reference each cell at a time. Right?

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