Thursday 31 December 2015

Classes and Movement

Carrying on from my previous post, Running into Issues, and attempting to use debug, I have now
implemented a class system, that will change the base stats of the units, and how they interact with
other classes.

Movement has now been further refined. The player will click on the "Move" button, which will allow
the unit to move to any space on the map at this present moment. A unit can only move once per turn,
and can now attack units adjacent to themselves. Depending on the class of the current unit and 
opposing unit, modifiers may come into play, giving the current unit positive or negative accuracy and
attack modifiers.

Units now have an "Actions" limit, allowing them to perform two actions in one turn. 

All code relating to changes can be found below.

For my next step, I intend to:
  • Look at creating a separate class for unit classes
  • Have AI units moving next to, and attacking player units
  • Adding a movement limit.

Wednesday 9 December 2015

Running into Issues, and attempting to use debug

Following on from my previous post; Map Implementation, I have continued to follow YouTube user Paul Metcalf's tutorial, Unity3D Simple Tactics Turn Based Game Creation Tutorial Part 2: Combat .

I will be posting commented code upon completing this part of the tutorial, this post is intended to highlight an issue I ran into, and how I attempted to fix it.

Relating back to the code in Map Implementation, two booleans were added to the Player class:





These booleans were added, so that the current unit would know if it was in a state ready for movement, or combat. The movement formerly added will now function only if the moving boolean is true. This was done through the Move function in the UserPlayer class:

The Move function was then set to a UI Button:
 As seen in the snippet of code above, if moving is false (represented by "!moving"), moving should be set to true when the function is run.

When the game was run with the above settings, I could not get the current unit moving. I could not understand this at all, so tried a few different fixes to see if I could progress.

Coding Fixes
To begin with, I attempted to change the code slightly, adding a debug log, changing variables, attempting to call the player in different manners. These are all shown below; this is within the Move function in the Userplayer class.

Having tried all of the above, I decided to take another approach:

Debug Mode
Though I have not formally used it before, I decided to attempt to use the debug functions within MonoDevelop, to see if this would help me find the answers to my problems:
I set the debug to start running once the if statement was activated.
Once the function had started, moving is definitely set to false, before it has attempted to switch to true.
Once the function has moved passed "moving = true", it is evident that moving is now set to true.
Yet, regardless of the above, moving on the current unit was still set to false.
At this point, I turned to my lecturer Chris for hopefully a hint. After a brief look at it, he brought to my attention where I was telling the button to get the Move function from; a unit prefab that was in the scene, unrelated to the loaded game.
This meant that I would have to relocate the button functions to an object that was always in the scene, such as the GameManager. So after a quick tweak, I made the change:
Now, the draw on the players list, calling on the current unit moving via the currentPlayerIndex int, and changing the variables through that method.

In hindsight, it was a silly mistake to make; I had believed for some reason I was assigning the button to the player prefab as a type, rather than as a singular game object. Once I realised the mistake, it was a simple fix, but I believe I have gained useful experience from this;

  • Pay attention to what objects are being assigned, and make sure to check if it is the object, or object type, being assigned.
  • The debug log is a helpful tool, for breaking down functions one step at a time, allowing me to view what the function is doing at every point it runs.
  • If in doubt when trying to run a function off a prefab, move the function into a constant class, and try it with external access to the base class it's effecting.