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 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. |
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.
No comments:
Post a Comment