Trying to populate a C3 array with a JS array failing

0 favourites
  • 2 posts
From the Asset Store
Advanced inventory mechanics for your RPG game (Array-based). Take Items, split them, pick up them, read the description
  • Hi!

    I'm trying to get into scripting, but can't figure out what I'm doing wrong here. My goal is to populate a C3 Array object using a javascript array.

    Here is my script:

    //creating a JS array by splitting a string:
    const arrayForSplitText = localVars.randomylChosenScrollText.split('.');
    
    //accessing a C3 array object:
    const a runtime.objects.ArrayFromConstruct.getFirstPickedInstance();
    
    //This console log works as expected:
    console.log(JSON.stringify(arrayForSplitText));
    
    //Function someone here on the forums built for loading a C3 array with a JS array:
    function setC3Array(c3ArrayInstance, arr){
    
    	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);
    
    	}
    
    }
    
    //call the function:
    setC3Array(a, arrayForSplitText)
    
    //What I get is:
    Errror on setC3Array function TypeError: x.forEach is not a function
    

    Tagged:

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Okay I simplified the code and for some reason this version works just fine:

    const arrayForSplitText = localVars.randomylChosenScrollText.split((/(?<=[.;:])/gi));
    const a = runtime.objects.ArrayFromConstruct.getFirstPickedInstance();
    
    console.log(JSON.stringify(arrayForSplitText));
    
    a.setSize(10, 1);
    
    for (let i=0; i<arrayForSplitText.length; i++ ){
    	a.setAt(arrayForSplitText[i], i);
    
    }
    
    
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)