I have been designing a simple turn based Civ-like game over the last two days with libGDX to test myself. I have a units class that can be extended to make unit types and I have my game board set up. Everything is almost done... except for the AI, specifically the unit AI. I want to collect all active units into two seperate arrays (the libGDX array), one for the human player and one for the AI units. I will not be using diplomacy for the sake of simplicity.

I first tried Array<Unit> playerUnits = new Array<Units>(); and calling the playerUnits.get(index) method to return the unit. My thought process seems to not be logical, though, as I found there was no way to get the x and y coordinates of units that do not yet exist (all units have to be built in factories in game). Next, I tried to store x and y coords in an XYCoords method, however there was no built in way to add units to the array later.

I have my pathfinder implemented (with a few minor bugs that cause units to flicker once moved) and now need to simply add the reaction to units. I have no idea how to do this and as you have probably guessed by now I have never had to implement a more complex AI than one that follows the player mindlessly. Has anyone had a similar problem that could somehow help me?

  • \$\begingroup\$ @AlexandreVaillancourt The only question is how to effectively fetch unit coordinates for my ai to use from units that are created in game. What is the other? \$\endgroup\$ – JDSweetBeat Jun 20 '15 at 21:23
  • \$\begingroup\$ @Shiro Every player has 1 unit to begin with and as the ai and human creates more they are set to be added to the array. I want the ai to retrieve the x and y coords every turn, after it is updated. \$\endgroup\$ – JDSweetBeat Jun 20 '15 at 22:35
  • \$\begingroup\$ I added an answer, let me know if that didn't answer the question and I will update it. \$\endgroup\$ – dimitris93 Jun 20 '15 at 23:50

I think problem is that you are using the size variable instead of the current count of the items that the array contains, in your for-loop. Example for-loop you should use: for(int i = 0 ; i < count ; i++) playerUnits.get(i).doSomething();. When you create an array, its default size is 16, even though it contains 0 items. As seen from the Array source code. The default constructor is public Array() {true, 16);}. Where 16 is the size.

You can use a different syntax for your for-loop like this: for(Unit u : playerUnits) {u.doSomething();}, which does the same thing as the first for-loop, but doesn't use a count variable, as I don't think that the Array class has a count variable. The second for-loop is called a foreach statement.

  • \$\begingroup\$ I was wondering why it wasnt returning all my units.... As it turns out my logic was off in trying to return x and y values seperately. Thnx for the answer, I realize now that my question might not have specified the problem (btw I know what a foreach statement is). \$\endgroup\$ – JDSweetBeat Jun 22 '15 at 12:31
  • \$\begingroup\$ @DJMethaneMan Great. By the way, the reason you can do a foreach statement it is because the Array<T> class implements Iterable<T> and then some specific methods are defined for it in order to work. Its not something that "just works" out of the box, you have to implement it. Also, you can just return the (x,y) as a Vector2. You should not even have x and y as separate variables. You should have just a Vector2, its a lot easier to work that way. \$\endgroup\$ – dimitris93 Jun 22 '15 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.