Updating global variables inside draw() function

I cannot update global variables in the Draw() function and I think that is because it would be problematic to deal with a variable that gets updated x times per second (I suppose?), sorry but I’m totally new to p5js. How can I update these variables during the running of my program since any attempt to update them inside the draw() has so far been useless? Thanks a lot

Edit: Here’s the source code

//WARNING: SHITCODE AHEAD!!!
//////////////////////////////////////////////////////////////////////
var myMatrix = [];
var humanStats = []
var humanDiseases = [ “none”,“influenza”,“tuberculosis”,“bronchitis”,“typhus”,“salmonella”];
var humanDiseasesE = [ “none”,“cysticFibrosisE”,“duchenneMuscularDystrophyE”,“hypertensionE”,“arthritisE”, “diabetesE” ]
var humanDiseasesResistance = [ “none”,“influenza”,“tuberculosis”,“bronchitis”,“typhus”,“salmonella”,“cysticFibrosisE”,“duchenneMuscularDystrophyE”,“hypertensionE”,“arthritisE”, “diabetesE”];
var humanEnvironmentResistance = [ “none”,“cold”,“food”,“danger”];
var childContainer = [];
var childN = 0;
var sideLength = 100;
var checkStartButton = false;
var horizontalPositionArray= [ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240, 250, 260, 270, 280, 290, 300, 310, 320, 330, 340, 350, 360, 370, 380, 390, 400, 410, 420, 430, 440, 450, 460, 470, 480, 490, 500, 510, 520, 530, 540, 550, 560, 570, 580, 590, 600, 610, 620, 630, 640, 650, 660, 670, 680, 690, 700, 710, 720, 730, 740, 750, 760, 770, 780, 790, 800, 810, 820, 830, 840, 850, 860, 870, 880, 890, 900, 910, 920, 930, 940, 950, 960, 970, 980, 990 ];
var verticalPositionArray= [ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240, 250, 260, 270, 280, 290, 300, 310, 320, 330, 340, 350, 360, 370, 380, 390, 400, 410, 420, 430, 440, 450, 460, 470, 480, 490, 500, 510, 520, 530, 540, 550, 560, 570, 580, 590, 600, 610, 620, 630, 640, 650, 660, 670, 680, 690, 700, 710, 720, 730, 740, 750, 760, 770, 780, 790, 800, 810, 820, 830, 840, 850, 860, 870, 880, 890, 900, 910, 920, 930, 940, 950, 960, 970, 980, 990 ];
var randomSpawnX = undefined;
var randomSpawnY = undefined;

function createMatrix(sideLength){

for (var i = 0; i < sideLength; i++) {

myMatrix[i] = [];

for (var j = 0; j < sideLength; j++) {

    myMatrix[i][j] = 0;

	}
}

}

function randomFromInterval( min, max ){

var random = Math.random() * ( max - min + 1 ) + min;

if ( random > max ){

random = max;

}

return random;
}

function randomBoolean(){

return Math.random() >= 0.5;
}

function initializeRandomStats( sideLength ){

for ( var i = 0; i < sideLength; i++ ){

humanStats[i]= { id: i, xPos: 0, yPos: 0, strength: randomFromInterval(1,10), agility : randomFromInterval(1,10), sight: randomFromInterval(1,10), hearing: randomFromInterval(1,10), intelligence: randomFromInterval(1,10), disease: Math.floor(randomFromInterval(0,5)), diseaseE: Math.floor(randomFromInterval(0,5)), diseaseResistance: Math.floor(randomFromInterval(0,10)), environmentResistance: Math.floor(randomFromInterval(0,3)), aggressiveness: randomBoolean };

}
}

function inheritedDiseaseE( id1, id2 ){

if ( humanStats[id1].diseaseE == humanStats[id2].diseaseE ){

return humanStats[id1].diseaseE;

}

if ( humanStats[id1].diseaseE == 0 ){

return humanStats[id2].diseaseE;

}

if ( humanStats[id2].diseaseE == 0 ){

return humanStats[id1].diseaseE;

}

if ( randomBoolean == true ){

return humanStats[id1].diseaseE;

} else {

return humanStats[id2].diseaseE;

}
}

function inheritedDiseaseResistance( id1, id2 ){

if ( humanStats[id1].diseaseResistance == humanStats[id2].diseaseResistance ){

return humanStats[id1].diseaseResistance;

}

if ( humanStats[id1].diseaseResistance == 0 ){

return humanStats[id2].diseaseResistance;

}

if ( humanStats[id2].diseaseResistance == 0 ){

return humanStats[id1].diseaseResistance;

}

if ( randomBoolean == true ){

return humanStats[id1].diseaseResistance;

} else {

return humanStats[id2].diseaseResistance;

}
}

function inheritedEnvironmentResistance( id1, id2 ){

if ( humanStats[id1].environmentResistance == humanStats[id2].environmentResistance ){

return humanStats[id1].environmentResistance;

}

if ( humanStats[id1].environmentResistance == 0 ){

return humanStats[id2].environmentResistance;

}

if ( humanStats[id2].environmentResistance == 0 ){

return humanStats[id1].environmentResistance;

}

if ( randomBoolean == true ){

return humanStats[id1].environmentResistance;

} else {

return humanStats[id2].environmentResistance;

}
}

function inheritedAggressiveness( id1, id2 ){

if ( humanStats[id1].aggressiveness == humanStats[id2].aggressiveness ){

return humanStats[id1].aggressiveness;

} else {

return randomBoolean;

}
}

function checkDiseaseResistanceE( childN ){

if ( childContainer[childN].diseaseE == childContainer[childN].diseaseResistance ){

return 0;

} else {

return childContainer[childN].diseaseE;

}
}

function createChild( id1, id2, childN ){

childContainer[childN]= { id: humanStats.length + childN, strength: (humanStats[id1].strength + humanStats[id2].strength) / 2, agility: (humanStats[id1].agility + humanStats[id2].agility) / 2, sight: (humanStats[id1].sight + humanStats[id2].sight) / 2, hearing: (humanStats[id1].hearing + humanStats[id2].hearing) / 2, intelligence: (humanStats[id1].intelligence + humanStats[id2].intelligence) / 2, disease: 0, diseaseE : inheritedDiseaseE( id1, id2 ), diseaseResistance: inheritedDiseaseResistance( id1, id2 ), environmentResistance: inheritedEnvironmentResistance( id1, id2 ), aggressiveness: inheritedAggressiveness( id1, id2 )};

childContainer[childN].diseaseE = checkDiseaseResistanceE( childN );

childN++;

spawnRandomPosition();

myMatrix[ randomSpawnX ][ randomSpawnY ]= childContainer[childN].id;

}

function printRawMatrix( sideLength ){

for ( var i = 0; i < sideLength; i++ ){

document.write("<br>");

for ( var j = 0; j < sideLength; j++ ){

    document.write(myMatrix[i][j]);

}

}
}

function shuffleHeightWidthArrays(){

// Fisher–Yates shuffle
horizontalPositionArray = _.shuffle(horizontalPositionArray);
verticalPositionArray = _.shuffle(verticalPositionArray);

}

function initializeSimulation(){

createMatrix( sideLength );
initializeRandomStats( sideLength );
shuffleHeightWidthArrays();

for ( var i = 0; i < sideLength; i++ ){

myMatrix[ verticalPositionArray[i] / 10 ][ horizontalPositionArray[i] / 10 ] = humanStats[i].id;
humanStats[i].xPos = horizontalPositionArray[i] / 10;
humanStats[i].yPos = verticalPositionArray[i] / 10;

}
}

function initializePrinting(){

	for ( var i = 0; i < sideLength; i++ ){

	  
	fill(color(0,0,255));
	rect( horizontalPositionArray[i], verticalPositionArray[i] , 10, 10 );  
  
}

}

function startPrint(){

checkStartButton = true;

}

function spawnRandomPosition(){

var cycleCheck = false;

while ( cycleCheck == false ){

	randomSpawnX= horizontalPositionArray[Math.floor(randomFromInterval(0,99))];
	randomSpawnY= verticalPositionArray[Math.floor(randomFromInterval(0,99))];

	if ( myMatrix[randomSpawnY][randomSpawnX] != 0 ){

		cycleCheck= true;
	}
}

}

var checkChildButton= false;
function printChild(){

checkChildButton= true;

}

function printCanvasChild(){

fill(color(0,255,0));
rect( randomSpawnX, randomSpawnY, 10, 10 );

}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function setup() {

createCanvas(1000, 1000); 
background(255);

initializeSimulation();

for (var x = 0; x < width + 10; x = x + (width / sideLength) ) {

		for (var y = 0; y < height + 10 ; y = y + (height / sideLength) ) {

	
			stroke(0);
			strokeWeight(1);
			line(x, 0, x, height);
			line(0, y, width, y);
	
    	}   
  }

}

function draw() {

if ( checkStartButton == true ){

	initializePrinting();
	checkStartButton= false;
	document.getElementById("startButton").disabled = true;
}

if ( checkChildButton == true ){

	spawnRandomPosition();///
	createChild(0,1, childN);///
	printCanvasChild();
	checkChildButton= false;
	document.getElementById("childButton").disabled = true;
	console.log(randomSpawnX);

}

}

Post the source code? Then maybe someone can diagnose what is going on.

1 Like

Thanks, I’ve posted it right now

Problem solved, It was due to some variables not having a value assigned to them, that’s why the program would block. The array cell which were being called weren’t initialized at all.

1 Like

Not at all.

On the contrary, the purpose of a running program is to handle and to work with changing variables.

When you think of a game, everything changes fast

1 Like

Yeah, that makes sense, thanks for the response ^^

1 Like