{"id":38757,"date":"2017-05-19T10:08:38","date_gmt":"2017-05-19T14:08:38","guid":{"rendered":"http:\/\/www.shamusyoung.com\/twentysidedtale\/?p=38757"},"modified":"2017-05-20T10:33:47","modified_gmt":"2017-05-20T14:33:47","slug":"learning-something-new","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=38757","title":{"rendered":"Learning C# &#8211; Sort Of"},"content":{"rendered":"<p>As I alluded to <a href=\"?p=38571\">last Friday<\/a>, I&#8217;ve been dabbling with the idea of expanding (modernizing) my programming knowledge and learning some Unity. I&#8217;ve been stuck in my old habits for a long time now. I&#8217;ve always been caught in this Catch-22 where I don&#8217;t want to stop working on an existing project to learn something radical and new, because doing so would bring the project to a halt. But if I&#8217;m <em>not<\/em> working on a project then I don&#8217;t have any immediate use for the New Thing. I&#8217;m either too busy or I don&#8217;t need it.<\/p>\n<p>But for whatever reason, now feels like a good time to take a crack at it. To this end I&#8217;ve been watching Unity tutorials. This is both fascinating and maddening. <\/p>\n<p>I have decades of coding experience, but I&#8217;m new to both Unity and C#. I&#8217;m a C++ programmer. C# and C++ are very similar, but not so similar that I can just jump in and begin writing useful C# code without educating myself first. The problem is that aren&#8217;t really any tutorials out there for me. Everything is either high-level stuff that assumes a deep knowledge of both C# and Unity, or (I am not joking here) it teaches you how to make some rudimentary &#8220;game&#8221; without needing to write a single line of code.<\/p>\n<p>The latter actually kind of pisses me off. I get that this is part of the allure of Unity for most people, but for me it&#8217;s like I took a class with a master carpenter in hopes of learning woodworking, and instead he spent the entire class showing us how to assemble IKEA furniture. This wouldn&#8217;t be so bad if I was just scanning a text document, but it&#8217;s pretty annoying to sit through fifteen minutes of rambling video waiting for them to get through this introduction crap and to the main part of the video, only to realize that this click-and-drag stuff IS the main part of the video and I&#8217;ve just wasted my time again.<\/p>\n<p>For couple of days in a row I&#8217;ve opened up Unity to an empty project with the silly notion that I was going to begin making some small thing. But then two hours later I was still scanning through video tutorials looking for answers and I hadn&#8217;t typed a single line of code. <\/p>\n<p><!--more-->Part of the problem is that what I want to do is kind of strange. I don&#8217;t want to import a model from the model library. I want to create my own out of raw polygons. I want to set up my own surface normals, set the UV mapping, and render it. <\/p>\n<p>For my money, this series on <a href=\"https:\/\/www.youtube.com\/watch?v=wbpMiKiSKm8\">Procedural Landmass Generation<\/a> by <a href=\"https:\/\/www.youtube.com\/channel\/UCmtyQOKKmrMVaKuRXz02jbQ\">Sebastian Lague<\/a> is the best tutorial so far. It has a lot of what I need to get going. The problem is that the bits I need are little ten-second segments sprinkled throughout the sprawling four and a half hour video series. That&#8217;s not a very efficient way of learning.<\/p>\n<h3>Why Video?<\/h3>\n<p><div class='imagefull'><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/unity_splash.jpg' width=100% alt='I&apos;m about 60% sure this is unrelated to the Unitarianism in Dead Space.' title='I&apos;m about 60% sure this is unrelated to the Unitarianism in Dead Space.'\/><\/div><div class='mouseover-alt'>I&apos;m about 60% sure this is unrelated to the Unitarianism in Dead Space.<\/div><\/p>\n<p>I get that there are some concepts that work really well in video. In the past I&#8217;ve tried to describe fiddly concepts like <a href=\"?p=15945\">surface normals<\/a>, <a href=\"?p=15843\">bilinear filtering<\/a>, one-sided polygons, Z-buffering, camera views, and texture addressing. These concepts are all things that are relatively easy to illustrate using video but somewhat long-winded to explain in prose. When teaching this sort of high-level conceptual stuff, video is tremendously useful.<\/p>\n<p>And the other hand, when it&#8217;s time to write code then <strong>nothing<\/strong> beats plain text. <\/p>\n<p>I&#8217;m not sure why video is such a popular format when it&#8217;s so obviously inferior. Is this what the public wants? Does it make the material more approachable? Does it feel more comfortable when it&#8217;s delivered like a lecture in school because that&#8217;s how most people learned as children? Or is the tutorial a video simply for the convenience of the producer? I don&#8217;t know. The whole thing is so strange to me. I&#8217;ve always thought of video as more labor-intensive to produce and lectures more time-consuming to consume, so from my perspective it feels like everyone is wasting their time for no reason.<\/p>\n<p>So we&#8217;re actually facing two problems: One is the problem where there aren&#8217;t good tutorials for experienced programmers to bring them up to speed in Unity, and the other is that most tutorials are videos when they should be articles. I&#8217;m caught at the intersection of these two problems, which means I&#8217;ve spent a couple of hours this week watching someone explain to me nine things I already know and one thing I can&#8217;t understand because I don&#8217;t have the context.<\/p>\n<h3>C# First Impressions<\/h3>\n<p><div class='imagefull'><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gr_screenshot1.jpg' width=100% alt='Here, have a random Good Robot screenshot for no reason.' title='Here, have a random Good Robot screenshot for no reason.'\/><\/div><div class='mouseover-alt'>Here, have a random Good Robot screenshot for no reason.<\/div><\/p>\n<p>C# and C++ are so similar that C# falls into some kind of uncanny valley for me. It&#8217;s so much like C++ that I get baffled when presented with something very different from what I&#8217;m used to. If the two languages used radically different syntax then the deviations wouldn&#8217;t feel so strange. But as it stands this thing looks so familiar yet feels so alien. <\/p>\n<p>Consider the classic &#8220;Hello World&#8221; program:<\/p>\n<pre lang=\"c\">\/\/ my first program in C++\r\n#include <iostream>\r\n\r\nint main()\r\n{\r\n  std::cout << \"Hello World!\";\r\n}<\/pre>\n<p>When your write a program in C or C++, the program begins at main (). The operations within main () will be executed in order, and when main () ends the program stops running. Here's the main() loop<span class='snote' title='1'>This is actually the main loop for the incomplete Linux version. The Windows version uses WinMain () and... uh. Look, it's stupid but we don't have to to get into it.<\/span> from <a href=\"?cat=498\">Good Robot<\/a>:<\/p>\n<pre lang=\"c\" line=\"1\">int main(int argc, char** argv)\r\n{\r\n  init();\r\n  run();\r\n  term();\r\n  SteamAPI_Shutdown();\r\n  return 0;\r\n}<\/pre>\n<p>On line 3 the program calls init () to initialize everything. This starts up the rendering, initializes the sound system, loads the fonts, reads in all the data files that drive the game, and so on. Then the program spends most of its time in run (), which keeps the whole thing going by maintaining the rendering loop, reading input, and doing all the other stuff a program needs to do to keep itself alive. When the user exits the game, run () ends. Execution passes to term () to clean up all the stuff we did in init (). After that we shut down the Steam API. Technically   SteamAPI_Shutdown() ought to go <em>inside<\/em> of term (), since that's what term() is for. I think Arvind put it here because we were always shy of modifying each other's code. <\/p>\n<p>At any rate, once main () is out of instructions to run, execution ends and the program poofs out of existence.<\/p>\n<p>This is how things have worked since literally before I was born. (Development began on the C language in 1969, and I didn't show up until 1971.)<\/p>\n<p>Now let's look at \"Hello World\" in C#:<\/p>\n<pre lang=\"csharp\">\/\/ Hello1.cs\r\npublic class Hello1\r\n{\r\n   public static void Main()\r\n   {\r\n      System.Console.WriteLine(\"Hello, World!\");\r\n   }\r\n}<\/pre>\n<p>As a veteran C and C++ programmer, this source code is surreal in the way it mixes the familiar with the inexplicable.<\/p>\n<p>First off, we declare a class. Okay. That's a fine thing to do. That's something you can (and often should) do in C++ in any program above trivial complexity. But usually you declare a class and then you instantiate it. The class definition is the blueprint, and somewhere in your code you need to actually create something that uses that class. Maybe something like this example of C++ code:<\/p>\n<pre lang=\"c\" line=\"1\">\r\n\/\/This class is stupid because all the members are private and there are no methods to \r\n\/\/make use of them. But just go with it. It's an example program and I don't want to \r\n\/\/build a whole class interface. That would only confuse people.\r\n\r\nclass Player\r\n{ \r\n  string name;\r\n  int    hitpoints;\r\n  int    mana;\r\n  int    XP;\r\n};\r\n\r\nclass Player player1;\r\n<\/pre>\n<p>On lines 5 through 11 we define the blueprint for the Player class. But nothing actually <em>happens<\/em> until line 13 when we cause a specific player to exist, and we store their data in a variable named \"player1\".<\/p>\n<p>But that doesn't happen in the C# program. We define a class called Hello1, but then... what causes an instance of Hello1 to begin existing? <\/p>\n<p>Things get even more bizarre when we see that Main () is <em>inside<\/em> of this class! So now our program is contained within a class that never exists? Madness! What would happen if I made another file called Hello2.cs, with a class called Hello2, which also contained Main ()? Who is in charge, here?<\/p>\n<p>This is just a small example of the strangeness. <\/p>\n<p>To be clear, I'm not saying that C# is a dumb language for dumb people. This sort of thing makes sense once you know the rules. I'm just trying to show that learning C# after all of these years of C++ is filled with strange moments of hilarious confusion.<\/p>\n<p>You can compare this to something like PHP. PHP is an objectively <a href=\"https:\/\/eev.ee\/blog\/2012\/04\/09\/php-a-fractal-of-bad-design\/\">terrible language<\/a>. But when I looked at PHP code for the first time I was immediately able to grasp where execution would begin, what order operations would be performed in, and I was able to get the general gist of what a PHP program was doing. I might get tripped up on some funny syntax<span class='snote' title='2'>Or worse, in familiar syntax that behaves in confusing ways. Seriously. PHP is a minefield.<\/span>, but the large-scale stuff was easy to follow. In contrast, in C# the mysteries are all structural rather than syntax-based. Where does the program begin execution? Where does it end? When are the members of Hello1 allocated<span class='snote' title='3'>In the example above Hello1 doesn't have member variables like \"hitpoints\" to \"favorite fruit\". But it COULD.<\/span>? <\/p>\n<p>I think to do this properly I need to back off from Unity, learn some C#, and then come back to Unity. Trying to learn both at once is like trying to build without a foundation. <\/p>\n<p>As a bonus, I'm pretty sure you can learn C# without needing to sit through hours of stupid video tutorials.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As I alluded to last Friday, I&#8217;ve been dabbling with the idea of expanding (modernizing) my programming knowledge and learning some Unity. I&#8217;ve been stuck in my old habits for a long time now. I&#8217;ve always been caught in this Catch-22 where I don&#8217;t want to stop working on an existing project to learn something [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66],"tags":[],"class_list":["post-38757","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/38757","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=38757"}],"version-history":[{"count":0,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/38757\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=38757"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=38757"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=38757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}