Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (774)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (856)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Accessing my Arrays  (Read 1235 times)
0 Members and 1 Guest are viewing this topic.
Offline Gibbo3771

JGO Kernel


Medals: 128
Projects: 5
Exp: 1 year


Currently inactive on forums :(


« Posted 2013-11-29 13:12:58 »

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 :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
      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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
   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.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline quew8

JGO Knight


Medals: 53



« Reply #1 - Posted 2013-11-29 19:08:50 »

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.
Offline Gibbo3771

JGO Kernel


Medals: 128
Projects: 5
Exp: 1 year


Currently inactive on forums :(


« Reply #2 - Posted 2013-11-29 19:39:50 »

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<Defense> 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:

1  
2  
3  
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.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Opiop
« Reply #3 - Posted 2013-11-29 19:42:11 »

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.

 
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 167
Projects: 5
Exp: 6 years



« Reply #4 - Posted 2013-11-29 19:46:00 »

If you're worried about class-specific methods, just use a cast.
Offline Gibbo3771

JGO Kernel


Medals: 128
Projects: 5
Exp: 1 year


Currently inactive on forums :(


« Reply #5 - Posted 2013-11-29 19:50:56 »

You would still have to do null checks in a foreach loop, but I don't know why it would cause any errors unrelated your game code?

 

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.

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

The way I originally had my arrays was all Array<Module> 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.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (1865 views)
2018-06-10 19:43:48

EgonOlsen (1887 views)
2018-06-10 19:43:44

EgonOlsen (1255 views)
2018-06-10 19:43:20

DesertCoockie (1686 views)
2018-05-13 18:23:11

nelsongames (1353 views)
2018-04-24 18:15:36

nelsongames (1970 views)
2018-04-24 18:14:32

ivj94 (2742 views)
2018-03-24 14:47:39

ivj94 (1944 views)
2018-03-24 14:46:31

ivj94 (3035 views)
2018-03-24 14:43:53

Solater (1085 views)
2018-03-17 05:04:08
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!