Accessing my Arrays

So this is not so much a debug problem but more of a “is this the best approach” but since I am new, it goes here.

I am in the process of coding the core of my game and I am using a few LibGDX arrays however the question applies to any arrays since I am mainly concerned with my use of foreach loops.

Currently at the moment I have 2 arrays in the world, 1 to hold enemies and 1 to hold collidable objects, such as asteroids or debris.

The ships in the game, enemy and player have 3 arrays each, one for holding every weapon object they have, one for holding every electronic system (most basic one being a targetting system, others to come) and one for holding every defense system (such as hull repairers, shield upgrades etc etc) and one holding system modifiers, such as damage amplifiers, engine mods, extra fuel bay, ammo bay etc etc.

It is essentially modules that get stored in these and in order to get access to them I am CONSTANTLY needing to use loops such as :

		for (Electronic electronic : currentShip.getElectronic()) {
			for (Ship ship : GameScreen.ships) {
				if (touchPos.x > ship.getBody().getPosition().x
						- (ship.getWidth() / GameScreen.WORLD_TO_BOX_WIDTH)
						&& touchPos.x < ship.getPosition().x + ship.getWidth()
								/ GameScreen.WORLD_TO_BOX_WIDTH
						&& touchPos.y > ship.getBody().getPosition().y
								- (ship.getHeight() / GameScreen.WORLD_TO_BOX_HEIGHT)
						&& touchPos.y < ship.getBody().getPosition().y
								+ ship.getHeight()
								/ GameScreen.WORLD_TO_BOX_HEIGHT) {
					tmpSelect = ship;
					if (ships.contains(tmpSelect, false)) {
						return true;
					}

				} else {
					if (electronic.selectedTarget != null) {
						return false;
					}
				}
			}
		}

Now these are literally everywhere in my code and almost any class that handles any sort of module activation/usage has to have access to that array so that the game knows which target you want to shoot, lock, unlock etc etc, so I made it static (i know, bad idea. Some classes have it passed to it in the constructor).

Another example where I am using the same code:

	for (Electronic electronic : currentShip.getElectronic()) {
			if (currentShip != null) {
				if (button == 0)
					if (electronic.targetClicked(touchPos, currentShip, ships)) {
						electronic.selectedTarget = electronic.getTmpSelect();
					} else {
						electronic.selectedTarget = null;
						uICore.removeRightClickMenu();
					}
				if (button == 1) {
					if (uICore.hasRightClickMenu()) {
						uICore.removeRightClickMenu();
					}
					if(!electronic.inRange(currentShip.getPosition(), tmpVector)){
						uICore.rightClickMenu(false, touchPos);
					}
					if(electronic.targetClicked(touchPos, currentShip, ships) && electronic.lockedTargets.contains(electronic.selectedTarget, false)){
						uICore.rightClickMenu(false, touchPosUI);
					}
					if (electronic.targetClicked(touchPos, currentShip, ships))
						uICore.rightClickMenu(true, touchPosUI);
				}
			}
		}

The above code is in my controller based class, which pretty much talks to everything in order to draw the very little UI I have atm.

Is this the best way to access my arrays? I don’t think it is inefficient since none of these loops are in the game loop and being iterated 60 times a second, well one is in the loop but it has a conditional statement before it will execute.

Anyone know any other way or am I ok to use those loops? I so far have zero broken code with using them, now that I am used to it lol.

I have read that it is better to use the regular for loops for games rather than the foreach. Foreach loops create lots of temporary instances and so the garbage collector has a lot of work to do (I am somewhat skeptical but do it since it can’t hurt). Since you aren’t doing it in the game loop, I think, as you said, that it isn’t inefficient however (assuming this foreach loops == bad), I recommend getting into the habit of using regular loops.

This is picky stuff, but I get the impression that that’s what you were looking for.

I will change one of my loops to a regular loop and see how it plays out, it might make it harder to avoid errors?

For instance, say I have my Array which can hold armor repairers, shield boosters and hull repairers. These all share the same base activate method, say I am iterating through the loop like so:

for(in i = 0; i < getDefense().size; i++){
    getDefense.get(i).someMethod();
}

Could this not cause errors? Well some of my code has error handling but it mostly consists of != null checks which afaik might get messy within a for loop like that.

You would still have to do null checks in a foreach loop, but I don’t know why it would cause any errors unrelated to your game code? If you have a big array of game objects, just make sure to cast each element in the array to the correct class before calling on class specific functions.

If you’re worried about class-specific methods, just use a cast.

Yeah each of my foreach loop always makes sure that the module you are looking for is not null but maybe just because I have not used the normal loop in my code much, I am not used to structuring it.

The way I originally had my arrays was all Array and then using a cast to tell the game that the Electronic class was a module using (Module) but it was causing problems when I was using the same method to add multiple module types, when really I should be using a overloading.

But like I said my code works perfectly fine and I have kept 99% of my loops out of the loop but it’s good to get advice for future refactoring if I ever find the need, eg. something ain’t working as planned.