Monday 21 March 2016

The Inventory System

Continuing on from my last post, Final A* Implementation and Inventory, I have now made much further progress with the inventory system, with it now being implemented to a functional level.


  • The Unit class now has a functioning inventory list, with Item classes now being pushed into it.
  • I have attempted to add a small level of security to the Item class, by storing its variables as private, and having them accessed externally via get methods.
  • The first entry in the inventory is registered as the active weapon, with combat now being slightly adjusted based on this. Further fine tuning will be required for the combat mechanics
    • This needs to be slightly tweaked, so that certain unit classes can only use a specific type of weapon.
  • A health vial has been implemented. When clicked on in the inventory, it will restore a units health, provided they are injured to begin with.
    • This was one of my key concerns with my project at this point. The current units inventory is represented by 5 buttons on the UI. As the inventory could end up having items added and removed from it, I wanted to make sure that the button corresponding to an item, based on position in the inventory, would always run the desired function.
    • Prior to this, I had only used UI buttons OnClick() inspector mechanics, manually adding the desired function to a button. I had to do research into dynamic button assignment, which lead me to this post on the Unity3D forums.
    • Initially, I had tried the following:

for(int i = 0; i < units[currentUnitIndex].inventory.Count; i++) {
            if(units[currentUnitIndex].inventory[i].itemType == Item.ItemType.Tool) {
                    inv[i].onClick = units[currentUnitIndex].inventory[i]._heal(units[currentUnitIndex]));
            }

        }

    • This lead to no success in the slightest. Upon finding the forum post mentioned above, I tweaked the for loop slightly, leading to this:

for(int i = 0; i < units[currentUnitIndex].inventory.Count; i++) {
            if(units[currentUnitIndex].inventory[i].itemType == Item.ItemType.Tool) {
                    inv[i].onClick.RemoveAllListeners();
                    inv[i].onClick.AddListener(delegate { units[currentUnitIndex].inventory[i]._heal(units[currentUnitIndex]); });
                }
            }
        }
    • While this for loop lead to some results, I kept having errors return, stating that the index was out of range. Starting to panic, I decided to create an Item temporary variable within the loop, storing the Item used in there, and calling on the _heal method from there. At this point I also added an if statement, declaring that the method would only be available if the Item name was "Health Vial", but realised shortly after that it was not required, due to how I set the _heal function up. My for loop ended up as so:
        for(int i = 0; i < units[currentUnitIndex].inventory.Count; i++) {
            if(units[currentUnitIndex].inventory[i].itemType == Item.ItemType.Tool) {
                    Item item = units[currentUnitIndex].inventory[i];
                    inv[i].onClick.RemoveAllListeners();
                    inv[i].onClick.AddListener(delegate { item._heal(units[currentUnitIndex]); });
            }
        }

    • By storing it as a temporary variable, this seemed to remove any issues with the index being out of range, allowing it to work with no visible issue, and no errors.
    • Having contemplated the removed if statement during the writing of this, I have decided to add it back, as I may add items that effect stats temporarily, meaning that Item.ItemType.Tool will not only be used for healing units:
        for(int i = 0; i < units[currentUnitIndex].inventory.Count; i++) {
            if (units[currentUnitIndex].inventory[i].itemType == Item.ItemType.Tool) {
                if (units[currentUnitIndex].inventory[i].Name == "Health Vial") { 
                    Item item = units[currentUnitIndex].inventory[i];
                    inv[i].onClick.RemoveAllListeners();
                    inv[i].onClick.AddListener(delegate { item._heal(units[currentUnitIndex]); });
                }
            }
        }
  • This now means that combat is being dictated much more by weapon choice and type, and a health Item can be applied to units.
  • As a small bug fix, I now have changed code around, so that the path is visible to players, and only shows the path up to the amount of spaces a Unit can move. While I had attempted to implement this earlier, I realised only recently that an if statement within the Tile update was turning them white, meaning the path could never be fully visualised.
My next steps will possibly be my most challenging, as I am aiming for AI implementation, as it has been put off due to other aspects of the project. This will start off with readings during this current week, and hopefully start implementation by next week.

There is one more aspect I would like to attempt to tackle before this too. At this current point, units are being instantiated in based on the worldSize for positioning. Instead, I would like to be able to use the grid[ , ] array to position the units, as this will make for more direct instantiation of units, requiring much less math to place units initially. While this will not be my primary focus, I believe it will aid in AI implementation, in terms of positioning AI units.




No comments:

Post a Comment