So I was working at my game as usual in Saturday, and I wrote the code necessary for each AI to know the range of the weapon it's carrying, so depending on the weapon it moves close to its target as much as it's required. After doing so, I saved my work on Visual Studio, and hit the build button so my code is compiled before I switch to Unity to test out the changes in Play mode. So I fired up Play mode, and something weird happened.
The enemy character indeed approached me, but its attack wasn't hurting me any more. How could this be happening ? Why wasn't I losing health all of a sudden ? I added some Debug.Log() lines in the code to see what was happening. The enemy's sword wasn't registering a hit. But the sword was clearly hitting me. "I probably messed something with my code" I though, but I was sure that I had done nothing that would effect hit registers in any way. So I started using GIT to review the changes I made in the code, and it was clear: I had done nothing wrong in the code. After more tries in Play mode I discovered that a bool that is supposed to turn true when the AI is attacking was always false. The sword has a bool that tells if it is used in an attack, so it can only hurt NPCs or the player's character when someone uses it in an attack, so the player doesn't lose health if he just touches some sword lying around. But the bool of the sword the enemy NPC was holding wasn't turning to "true" while the attack was executed for some reason. Bools are turned true or false through script, through code, so it seemed as something had gone wrong in the code. Even though I was 100% sure that I had done nothing wrong in the code, I decided to revert all the code to its previous state by using GIT, thinking that that would fix the problem. So I lost the work I had done, making NPCs being conscious of the range of the weapon they are holding. And guess what ? The problem was still there. Even though the code had reverted back to when everything was working fine, the sword still wouldn't register a hit, and its bool would stay forever false. At that point I started feeling like I was becoming crazy. It didn't made sense. I started debugging the game using Visual Studio's step-by-step method, watching the execution of each line of code. And that made me feel even more crazy. Visual Studio was telling me that the bool is turning to true, but in Unity the bool of the sword would never become true. At that point I thought that it might be a bug in Unity or Visual Studio. So I looked out and I found that there are newer updated versions for both, and I decided to update both, and so I did.
But that didn't fixed the problem either.
At that point in the noon I gave up. A thousand thoughts passed by my mind. I even thought about quitting. Was I a loser ? Was the universe conspiring against me ? I had so much mental pressure I couldn't keep trying that day. And I thought it was over. The next day came along and my determination made me open up Unity and Visual Studio again, and keep trying to find a way out, a solution. Eventually I did. It turns out that there were two sword items in the scene, both having the exact same name. One was the sword attached to the NPC's hand, which was a child of the "hand" bone of the NPC's armature, and the other was a sword lying at some point in the world. Both with the exact same name. And guess what ? The sword gameObject that was passed in the script as being equipped by the NPC was the sword that was lying around and not the one fixed to the NPC's hand.
So the code was giving the NPC's animator component the command to make the sword attack animation, but the sword that would actually damage the player while the animation lasted was that other random sword lying somewhere in the map. How did this happened ? I don't know. Perhaps I dragged and dropped the wrong sword in the editor, and forgot about it. But all my confusion and stress could have been avoided if one of two things were happening:
1) If Unity didn't allowed gameObjects of the same name to exist in the same scene, and instead forced each object to have a unique name. It kind of does, but only if the two or more objects are children of the same parent. So if I place 2 swords on the map, Unity will automatically rename one of the swords "sword (1)", and if I add another one, it will name it "sword (2)" etc. But if I place a sword not as a child of the map itself, but as a child of a NPC that is part of the map, then this renaming won't happen. And there will be 2 gameObjects of the exact same type and with the exact same name, on the same scene. There is a reason in programming you are not allowed to have two objects with the same name, and you get a compiler error when and if you try to do it. Because it's a bad, bad idea. I really hope Unity did that too, it's just seems the reasonable thing to do.
2) Another thing that I think would be interesting, is the possibility to "git" changes in the Unity editor. Git is an invaluable tool. It allows you to "save" separate versions of your work, and check them later on to see what has changed. This allows you not only to find problems that might have gone unnoticed, but also to revert your work to a previous state if something went really wrong. But you can't have that with the changes done in the Unity editor. All history of your work in the editor gets lost as soon as you close the editor, or the undo hits its memory limit.
So that's it. That was my weekend's story of developing my first game, an action RPG. I hope other developers might find this story useful, as it might save them some hours of madness.
The enemy character indeed approached me, but its attack wasn't hurting me any more. How could this be happening ? Why wasn't I losing health all of a sudden ? I added some Debug.Log() lines in the code to see what was happening. The enemy's sword wasn't registering a hit. But the sword was clearly hitting me. "I probably messed something with my code" I though, but I was sure that I had done nothing that would effect hit registers in any way. So I started using GIT to review the changes I made in the code, and it was clear: I had done nothing wrong in the code. After more tries in Play mode I discovered that a bool that is supposed to turn true when the AI is attacking was always false. The sword has a bool that tells if it is used in an attack, so it can only hurt NPCs or the player's character when someone uses it in an attack, so the player doesn't lose health if he just touches some sword lying around. But the bool of the sword the enemy NPC was holding wasn't turning to "true" while the attack was executed for some reason. Bools are turned true or false through script, through code, so it seemed as something had gone wrong in the code. Even though I was 100% sure that I had done nothing wrong in the code, I decided to revert all the code to its previous state by using GIT, thinking that that would fix the problem. So I lost the work I had done, making NPCs being conscious of the range of the weapon they are holding. And guess what ? The problem was still there. Even though the code had reverted back to when everything was working fine, the sword still wouldn't register a hit, and its bool would stay forever false. At that point I started feeling like I was becoming crazy. It didn't made sense. I started debugging the game using Visual Studio's step-by-step method, watching the execution of each line of code. And that made me feel even more crazy. Visual Studio was telling me that the bool is turning to true, but in Unity the bool of the sword would never become true. At that point I thought that it might be a bug in Unity or Visual Studio. So I looked out and I found that there are newer updated versions for both, and I decided to update both, and so I did.
But that didn't fixed the problem either.
At that point in the noon I gave up. A thousand thoughts passed by my mind. I even thought about quitting. Was I a loser ? Was the universe conspiring against me ? I had so much mental pressure I couldn't keep trying that day. And I thought it was over. The next day came along and my determination made me open up Unity and Visual Studio again, and keep trying to find a way out, a solution. Eventually I did. It turns out that there were two sword items in the scene, both having the exact same name. One was the sword attached to the NPC's hand, which was a child of the "hand" bone of the NPC's armature, and the other was a sword lying at some point in the world. Both with the exact same name. And guess what ? The sword gameObject that was passed in the script as being equipped by the NPC was the sword that was lying around and not the one fixed to the NPC's hand.
So the code was giving the NPC's animator component the command to make the sword attack animation, but the sword that would actually damage the player while the animation lasted was that other random sword lying somewhere in the map. How did this happened ? I don't know. Perhaps I dragged and dropped the wrong sword in the editor, and forgot about it. But all my confusion and stress could have been avoided if one of two things were happening:
1) If Unity didn't allowed gameObjects of the same name to exist in the same scene, and instead forced each object to have a unique name. It kind of does, but only if the two or more objects are children of the same parent. So if I place 2 swords on the map, Unity will automatically rename one of the swords "sword (1)", and if I add another one, it will name it "sword (2)" etc. But if I place a sword not as a child of the map itself, but as a child of a NPC that is part of the map, then this renaming won't happen. And there will be 2 gameObjects of the exact same type and with the exact same name, on the same scene. There is a reason in programming you are not allowed to have two objects with the same name, and you get a compiler error when and if you try to do it. Because it's a bad, bad idea. I really hope Unity did that too, it's just seems the reasonable thing to do.
2) Another thing that I think would be interesting, is the possibility to "git" changes in the Unity editor. Git is an invaluable tool. It allows you to "save" separate versions of your work, and check them later on to see what has changed. This allows you not only to find problems that might have gone unnoticed, but also to revert your work to a previous state if something went really wrong. But you can't have that with the changes done in the Unity editor. All history of your work in the editor gets lost as soon as you close the editor, or the undo hits its memory limit.
So that's it. That was my weekend's story of developing my first game, an action RPG. I hope other developers might find this story useful, as it might save them some hours of madness.
Δεν υπάρχουν σχόλια:
Δημοσίευση σχολίου