Wanted to clarify GetKeyDown overloaded methods

I am an experienced programmer so I should probably skip this section of the class, but I figured what the heck I might pick up something new in Unity or something.

Anyway maybe it is because I am experienced that this jumped out at me, but I wanted to talk about it a moment.

In the video, you start with one form of the Input.GetKeyDown method that takes a string as the key name argument. You then do your little mini challenge and suddenly switch to a second form of the method (an overloaded version) that takes a KeyCode enum as the key argument.

I am not sure what bothers me more; the fact that such a switch could potentially be very confusing to someone without experience, or the fact that the lack of posts here shows that it wasn’t confusing to anyone :slight_smile: I think it is the latter though because it makes me think many people don’t understand enough to actually be confused. Programming is a very exacting science, and it is so very important to understand why things work the way they work. I think starting with the string argument form of the method and then suddenly switching to the KeyCode argument version of the method is both bad form and potentially confusing at this point, and more so potentially steers inexperienced programmers down a bad path.

The reason I say that is because at this time you have not explained how the same method can potentially take different parameters. While I agree it is too early for that, the problem is this lesson sort of leaves the impression with people that maybe the type of a parameter isn’t important when it very much is. This potentially sets people up with a bad understanding of fundamentals which can grow like a rot. One of the single most common mistakes made by new programmers is because of a lack of understanding of types.

This lesson would be much better of sticking with one form of the method or the other and not switching between them. Stick with Input.GetKeyCode('up') instead so that people see the same type (string) being used. Or start with the Keycode version and stick with that. I don’t know Unity itself well enough to know if one form is preferred over another or what those reason might be, but just pick one and stick with it.

Anyway this isn’t a hate post or a rant. I hope I came across as I intended which was to simply give feedback on how this can be better, especially for teaching new programmers.

EDIT: This gets even more evil in the IF lesson where you stumble around trying to get Return working (I know you weren’t really stumbling, it was an example) and you swap back and forth like the parameter isn’t important. Please, this is really bad :frowning:


I don’t know if something change since video was recorded, but while doing the minichallenge I found this page:
Conventional Game Input where it doesn’t use KeyCode.

Also, at Input.GetKeyDown page it uses the KeyCode, so I’m a little confused…:frowning:

I think this is because the use of game collider library but i am not sure about it …

What you have with Unity is some flexibility for different configurations for different games.

Taking number wizard as an example, you only really need _up, down and something to indicate correct.

You could hard wire “u” and “d” for up and down, but then the game will always use those keys. They are also fairly unusual. A lot of people with a bit of gaming experience on a keyboard may be more likely to assume the W,S,A,D keys for movement, or maybe the arrow keys.

If you consider one of the next games you will create, Text101, you create a bit of an adventure game, there are multiple items you can pick up, look at, and so on. In that type of game having more keys open for access makes more sense. For example, as a player I may be presented with an option which read “To take the lamp, press L” - that seems fairly reasonable. But if I had to press “L” for every item I picked up that would be a bit odd.

Then you come to other types of games where perhaps we want to deal more with movement on the horizontal and vertical axis, but using a keyboard instead of a different input controller. Here, hard wiring a specific key, and I would argue to some degree even W,S,A,D would be bad isn’t the best approach, instead you can use things like the horizontal and vertical but if you also provide your user with a configuration screen where they can change the specific keys that are mapped to those, everyone wins.

1 Like

Wow. You answered a question I didn’t know I had because of my lack of understanding, as you said. I saw him use the string, saw him not use the string, and I didn’t understand I had to ask a question about that because I really had no idea what was going on. Thanks for pointing this out. This caused me to google a few things and I understand it a lot more because of this post.

Wow thanks for bringing this to our attention. It did kinda baffle me that it looked completely different after the mini challenge, but I just thought I wasn’t understanding it properly.

I’ve, personally, found DotNetPearls.com to be a wonderful reference for C#.

Privacy & Terms