Javascript Arrays not properly initialized

0 favourites
  • 2 posts
From the Asset Store
2D fighting template based in the game that defined the fighting games genre.
  • Hello I've written the following script:

    	var careerIndices = [];
    
    var randomCareers = [];
    var randomCareersForPortraits = [];
    var randomCareersForCareerImages = [];
    
    var portraitIndices = [];
    var careerImagesIndices = [];
    
    
    
    function intializeCareerIndices (runtime) {
    
    	for (var i = 0; i < runtime.globalVars.gNumberOfCareers; i++) {
    		careerIndices.push(i);
    	}
    
    	for (var i = 0; i < runtime.globalVars.gNumberOfCharacters; i++){
    		
    		var index = Math.floor(Math.random() * careerIndices.length);
    		
    		var roll = careerIndices.splice(index,1);
    		
    		randomCareers.push (roll [0]);
    	}
    	
    	randomCareersForPortraits = randomCareers;
    	randomCareersForCareerImages = randomCareers;
    	
    	console.log (careerIndices);
    }
    
    
    function initializeImagesIndices (runtime) {
    
    
    	for (var i = 0; i < runtime.globalVars.gNumberOfCharacters; i++){
    		
    		var index = Math.floor(Math.random() * randomCareersForPortraits.length);
    		
    		var roll = randomCareersForPortraits.splice(index,1);
    		
    		portraitIndices.push (roll [0]);
    		
    		index = Math.floor(Math.random() * randomCareersForCareerImages.length);
    		
    		roll = randomCareersForCareerImages.splice(index,1);
    		
    		careerImagesIndices.push (roll [0]);
    	}
    }
    

    The NumberOfCareers is 15 but the careerIndices array has a length of 11 for some reason even if I explicitly define it as careerIndex = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14]. Is this a bug or am I doing something wrong?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Hello,

    when do you call your "intializeCareerIndices()" function?

    I created a global variable "Test=15" in an event sheet and call this code and it returns 15 as it should do:

    async function OnBeforeProjectStart(runtime)
    {
    	
    	runtime.addEventListener("tick", () => Tick(runtime));
    	
    	var careerIndices = [];
    	for (var i = 0; i < runtime.globalVars.Test; i++) {
    		careerIndices.push(i);
    	}
    	console.log(careerIndices.length);
    }
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)