Check for existing objects

Hello,

I am really struggling with trying to create a simple way for my game to check if a gameobject inside an array exists or not…

What I currently have is an AJAX call with a setInterval that checks my PHP/MySQL server every 5 seconds for the “units” that belong to the user, gets data about the unit like the x/y position, type of unit etc, then inserts them into the game with that data.

The problem is that I can’t create a proper condition to check if the gameobject is currently inside of my array of game objects, and the setInterval call just spawns a duplicate of the units every 5 seconds.

(I’ve removed the code irrelevant to the problem to make this easier to read, apologies if I mistakenly removed something important to the problem)

My GameObject:

metruss = [];
containsCount=0;

//METRUS GAME OBJECT
class Metrus extends Phaser.Physics.Arcade.Image {

constructor(scene, x, y, motion, direction, distance, id) {
    super(scene, x, y, 'metrustotalsprite', direction.offset);

	this.id = id;
	this.name = id;

    this.startX = x;
    this.startY = y;

	var unitSprite = this.scene.matter.add.gameObject(this, { shape: 'circle', width: 30, height: 30, circleRadius: 30});	
	unitSprite.setName(id);

	unitSprite.displayWidth = 30;
	unitSprite.displayHeight = 30;
	unitSprite.scale = 0.4;

The AJAX Call:

create () {
//LOTS OF OTHER WORLD RELATED CODE LIKE SPAWNING MY ISOMETRIC TILES HERE, ETC

function unitData(callback){
		
		var updateInterval = 5000;
		setInterval(function(){
			$.ajax({
				  type: 'GET',
				global: true, 
				  dataType: "json",
				  url:'../includes/units.php',
				contentType: 'application/json',
				  success: callback 
			});
		},updateInterval);
		
	}
	
	unitData(result => {
		
	const tileWidthHalf = tileWidth / 2;
	const tileHeightHalf = tileHeight / 2;
	
	const centerX = mapWidth * tileWidthHalf;
	const centerY = 64;
		
	for (var z = 0; z < result.unitId.length; z++)
	{

	var unitX = result.unitX[z];
	var unitY = result.unitY[z];

	var unitId = result.unitId[z];
	var unitType = result.unitType[z];
	var unitAction = result.unitAction[z];
	var unitDirection = result.unitDirection[z];	

	var unitCount = metruss.length;
	var serverCount = result.unitId.length;

console.log('UnitCount='+unitCount+' ServerCount='+serverCount);
		
//////////////////////////////
//THE SOLUTION:
//////////////////////////////

	//CHECK FOR COUNT LOOP
	if (metruss.length > 0) {	
		
			metruss.forEach(function callbackFn(Metrus, index) { 

				if (Metrus.id.includes(unitId) == true) {
					containsCount++;
				}

			});

	}   
	
	for (var i = 0; i < levelData.length; i++)
	{
		for (var j = 0; j < levelData[0].length; j++)
		{
			
			const tx = (j - i) * tileWidthHalf
			const ty = (j + i) * tileHeightHalf
			const txOffset = tx - 64;
			const tyOffset = ty - 64;

			if (unitX == i)
			{
				if (unitY == j) 
				{

				   //CHECK TO SEE IF UNIT HAS ALREADY BEEN COUNTED
				  if (containsCount == 0) 
				 {

				thisUnit = metruss.push(this.add.existing(new Metrus(this, centerX + txOffset, centerY + tyOffset, unitAction, unitDirection, 150, unitId)));
				this.cameras.main.centerOn(centerX + tx, centerY + ty);
                                    }
													
				}

		        }
									
        }
			
    }
			
    }
		
});

}

Looks like I found the answer, all I had to do was a simple if and foreach loop with a counter to check if the gameobject was in array yet or not (solution edited into original post)

But I appear to have a new problem, if anyone can shed any light on it I would appreciate it:

If I make a change on my mysql database, the ajax call does not appear to reflect the change.
Ie: If I add a row (unit) the change is not reflected in the game after the interval delay, nor does it remove the gameobject if I delete a row (although I suspect that is because there’s no method to delete a gameobject that doesnt exist in my code yet)

Anyone know the best way to go about this?

PS: Live working demo of this at http://astralforge.net

Thanks!

cache: false, // Appends _={timestamp} to the request query string