Arrays - Construct <--> Script

0 favourites
  • 5 posts
From the Asset Store
Casino? money? who knows? but the target is the same!
  • When picking an array and logging it, I'm not surprised it is an object, but I am surprised that I don't see the actual array of values anywhere on the object. Am I missing something?

    I am getting the instance like this: runtime.objects.Array.getFirstPickedInstance();

    Which is inside an event, and is finding the instance of the array I want.

    The main reason right now that I am wanting to work with the array in both Events and JavaScript is the built-in Save function. As if I only store the array in my script it won't be saved.

    I was hoping to grab the actual array so I could filter/map/reduce/etc integrating my events and scripts. I'm not wanting to loop through width and height and use the getAt() for each value. Seems to defeat the purpose (conciseness, readability, etc). I guess I could make a global function to translates arrays to/from Construct/JavaScript - but just surprised at this.

    Is JSON the same? Could use JSON instead of an array . . .

  • Don't know if this is of any help, but I made an example of Arrays here , It has an example of how to Add to a C3 Array in Javascript.

    https://www.construct.net/en/forum/construct-3/scripting-51/please-add-templates-145895/page-3?kws=sizcoz

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Don't know if this is of any help, but I made an example of Arrays here , It has an example of how to Add to a C3 Array in Javascript.

    https://www.construct.net/en/forum/construct-3/scripting-51/please-add-templates-145895/page-3?kws=sizcoz

    Hey sizcoz thanks for trying to help!

    I took a look at one of the projects you posted on that thread. It looked like arrays created in JavaScript. I'm looking to grab an Array, as in the Construct object added to the project, inside a script.

    So here is a screenshot as I grabbed an array and logged it:

    runtime.objects.Array.getFirstPickedInstance();
    

    You can see the width and height, that it is 4x5x1, but I don't see the actual data.

    The only method on the prototype is getAt(). It just seems weird that I'd have to loop to turn an array into an array.

    Anyway, the point of this comment - let me know if I was looking at the wrong part of that thread, ha. Thanks!

  • So, I mean I can make these two global functions:

    function parseArray(c3ArrayInstance){
    	const a = c3ArrayInstance;
    	return Array(a.width).fill(0).map((c, x) => {
    		return Array(a.height).fill(0).map((d, y) => a.getAt(x, y));
    	});
    }
    
    function setC3Array(c3ArrayInstance, arr){
    	let success = true;
    	try {
    		const w = arr.length;
    		let h = arr.sort((a, b) => a.length - b.length)[0].length;
    		c3ArrayInstance.setSize(w, h);
    		arr.forEach((x, xi) => {
    			x.forEach((y, yi) => {
    				c3ArrayInstance.setAt(y, xi, yi);
    			})
    		})
    	} catch(err){
    		console.log('Errror on setC3Array function', err);
    		success = false;
    	}
    	return success;
    }

    They seem to be working fine. Like I said, it would make sense to me that there would be a getArray() and setArray() method on the prototype.

    Maybe it is because it is unlikely that users that are using Scripting in C3 will also use the C3 Array plugin. Like I mentioned before, I am looking to do this because I read the C3 Arrays will be included in the Save/Load plugin but arrays in scripts would not be.

  • Tested the above functions with this code:

    const a = runtime.objects.Array.getFirstPickedInstance();
    const b = parseArray(a);
    console.log(b);
    b[1][0] = 'test text';
    const success = setC3Array(a, b);
    console.log("success: ", success);

    Only thing odd was that the console.log(b) already had the 'test text' . . .

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