Will Ada Replace C/C++?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello I'm the third owner of this channel some time ago NSA issued a list of so-called safe languages that you're supposed to use instead of those nasty C and C++ and I find it fascinating because it contains Ruby and python out of all things it's just like if you are high performance game engine developer in C++ what are you doing programming in such unsafe language think about the children consider switching to Ruby anyway at the end of the list you will find a very special programming language that is surrounded by mystery it's a the legendary language named after the first ever computer programmer a the LEL and commissioned by US Department of Defense and has a syntax that looks like Pascal with begins and ends and stuff very few people programming this language but a lot of them are talking about it what I decided to do is to check out this language on my twitch Channel t.tv and ended up doing sort of like a 20 days of uh game development challenge where I set a goal for myself to develop and deliver a game in 20 days in a this was a good opportunity for me to explore the language on something more complex uh than a hello world and practice my scope management and project delivering skills Tech YouTubers are famous for their ability to deliver projects you know because one of the biggest problems I've noticed with actually delivering the projects is keeping the scope small so I thought what if I give myself like a hard deadline 20 days and by the end of the 20 days I must release whatever I've got that way I'm forced to keep the scope small the result of this experiment turned out very educational for me and in today's video I would like to share what I've learned and most importantly what I've made [Music] this is ERS the game that I made in 20 days in Aid the best way I can describe it is dark souls of Bomberman the game is turn based that means the time moves only when you move you can be progressing to the level by fighting bosses with your bombs and solving simple mini puzzles you are expected to die a lot but don't get discouraged by that take it as an opportunity to learn something new about the [Music] game if the boss seems difficult that means you're approaching it [Music] incorrectly the game is pretty small it has only one level and three bosses I had only 20 days to develop this entire thing come on the executables for Windows and Linux are available I'm not some sort of a smelly nerd so the link to the H iio page is in the description it's absolutely free and open source the link to the GitHub page with the source code is going to be also available in the description it's under MIT license so feel free to studied and moded as long as you are within the MIT license special thanks to type 42 who is the author of the soundtrack for the game to be honest this particular soundtrack is responsible for like 30% of the vibe of the game I found them accidentally right because I was just like searching for some stuff on open game Arc and it just like put ambient like when you some sort of like a dark ambient and I found this track right away and then I look at some other works of the artists and it's just like they're so goddamn underrated check out their YouTube check out of their soundtrack like I'm going to put all of that in the description as well it's just like I was looking for copper and found gold like for real oh and one more thing if you find the last boss difficult this is because you have a skill issue not because I suck at designing games okay but don't worry there is an easy way to cheese this boss if you find that way a patch yourself on the back so yeah that's basically what you can do in a in 2024 so without further Ado let's finally take a look at this mystical language let's start with a classic let's Implement a hello world in [Music] it I'm going to assume that you already know C and C++ development on a decent level so you know what is a translation unit object file linking static and dynamic libraries you are a programmer I'm going to assume that you are a programmer the first thing we need to do we need to create a file hello. ADB the extension for Ada is called ADB which is rather interesting like isn't it supposed to be Ada in Ada there's two main extensions It's ADS and ADB so translation unit or I think they call them packages it doesn't really matter so a single sort of like translation unit of AA consist of two files the first one is the specification and the second one is the body specification is an analog of headers in CN C++ and body contains implementations so in our case we're not going to have any sort of like forward declarations for this specific unit or anything like that so we don't really need ads we only need ADB the next thing we want to do we want to create an entry point so we're going to create a procedure it's kind of similar to Pascal if you ever worked with Pascal you're going to feel at home with a so you have procedures and you have also functions the difference between procedure and function is that procedure doesn't return a value and function does return a value right so the same like in Pascal then you have to specify the name of the procedure right so and actually the name has to be the same as the name of the file but since a is case insensitive you can capitalize it like that then you have to say you're obligated to say is right so procedure hello is for whatever reason this is just a syntax and then you put begin and end which denotes the body of the procedure then within the procedure you have to put a statement that prints hello world into the standard output and this is a function actually procedure called put line and and in a double quotes you say hello world but this is not going to work because on top of that you also have to say that I'm linking with a. text iio and after that you will be able to use put line but by prefixing with this specific package name to not prefix this specific package name you need to import all of the names from this package using user and after that this is going to work so how do you even compile this program you need to have a a compiler what's interesting is that ADA is supported by GCC in fact GCC is the main Ada compiler that ADA compiler is called nut and it's integrated with GCC compil system so if you use Linux and I do assume that you use Linux because I don't really know how to use Windows right so I'm more of a Linux guy it's up to you to figure out how to use this entire thing on Windows I have no idea so if you're on Linux these things should be available in your rers just uh search for things like n Ada and just like install whatever it has has and you may try to just go ahead and do GCC hello ADB and uh so yeah it says very interesting thing it says that minus C or minus s is required for Ada minus c means that you want to compile a translation unit and minus s means that you want to generate assembly we don't really want to generate any assembly we want to just compile the translation unit and it is going to create an object file on top of that it is going to create something called alif file so but essentially the only thing we care about is object file so we can try to link this object file creating a hello and it is not going to work because it needs some sort of a runtime right as you can see we're trying to link this entire thing and it cannot find the Ada runtime functions and stuff like that and this is because you're not supposed to compile a programs like that so what the not developers did they introduced a special command called not make and this command is one of the many uh commands from the so-called n toolbox and what you're supposed to do you're supposed to say not make the name of the program right and as you can see it does a bunch of things all right so it actually caches some of the stuff right so if we remove all of the garbage that it did in here and all the garbage data accidentally created that happens and take a look at the Full Compilation log you will notice that it does the same thing that we did it just called the GCC with minus C which generated o and some alif file and then it calls not bind with that alif file I have no idea what that Al file is and only after that you can link this entire final thing and produce an executable that prints hello world I've no idea what is bind but apparently a developers added like an entire step into the compilation of a native program right so usually what we had in CN C++ is that we had compilation and then linking so a developers not developers were not satisfied with just two steps they introduced the third one binding so you compile your program you bind your program and then you only link it but anyway so if you're using that make you don't even notice that there's also this thing called GPR build which is an advanced build system for Ada which I personally didn't use because I have an allery on so-called Advanced build systems throughout my career almost 20 years of software development I worked extensively with s things as seake Maven gradal and even a thing which you probably don't know called SBT which is a build tool for Scala and I worked commercially with Scala and the thing about this build tool is that they encourage piling more and more complexity into your project they make it so goddamn easy that by the time that you notice the price you're paying it is too late and you can't get rid of those build systems so as soon as I saw this thing I realized where it is all going and I didn't even look into that that doesn't mean that it's not useful it probably is useful and you probably want to use that and I'm just saying that I personally don't want to use that and I personally didn't use it for my game I shipped my entire game without using that at all I only use not make maybe I'm going to use this thing when I feel like it's Justified but for now I'm going to put it aside another thing that you will probably need is some sort of a mode for Ada for highlight the source code in a and you can probably find something relevant for your text editor in Vim a mode is available out of the box probably there's something in Visual Studio code there's like literally everything for visual studio code I don't know for sure I just don't use Visual Studio code for emac if you're using emac for whatever freaking reason maybe you are a weirder like me do not use whatever is available in Alpa because this a mode simply does not work it is a very common and known problem a lot of people complained about that when you try to install it it will simply not work it will try to compile some a programs that will fail with compilation errors nobody knows how to fix them so generally people suggest to not use a mode from emac what we found during those 20 days is a fork of old a mode that actually works and that's the thing that I'm using right now nobody really knows about it as you can see but it's a fork of old version of AD mode that was distributed with emac some time ago and it actually works it is beautiful uh thank you so much tord Bond I'm going to give the link to this uh repo in the description for anyone who's interested to use this mode in emac if you use emac all right so I know how to write a hello world in Ada I know how to produce an actual executable but the question is how do I write and ship an entire freaking game in this language and the answer is by using shed tone of c and C++ code if you never heard about rap let me show you something cool it's a library for developing video games but it's suitable for developing all sorts of multimedia applications the games after all are ultimate multimedia applications in the releases on the GitHub page there are pre-build binaries for the library so in our case we probably care about Linux amd64 let's download this thing so and if you unpack this Library you will have um a bunch of header files right so the library is written in C that's important and in the lip folder you have the uh static and dynamic libraries I prefer to actually link with r statically so let's actually try to create a simple hello world with this library right so I'm going to start with just you know including stdi then I'm going to create domain the usual thing right and uh then I'm going to just print hello world nothing particularly special I just want to test if I can build this entire thing so I'm going to do CCO Main main.c and I'm going to run this program right away just to confirm that it in fact prints hello world so the first thing we have to do we have to include Ray itself R.H and what we can do now we can just go ahead and initialize a window with a certain size 800 by 600 for instance and then we're going to say hello Ray leap so after that we have to organize an event Loop right so any self-respecting gu application has an event loop I mean you obligate you to have an event Loop because you're interacting with the user so there's a special function called window should close which checks whether the window should be already closed right so for instance the user tried to close the window or press Escape or something like that and while the window should not be closed we keep interacting with the user how do we keep interacting with the user We Begin drawing something on the screen then we draw something for example we can clear the background right so we can clear the background with all sorts of colors so let's actually clear it with red so we can instantly see that something is happening and then we can end the drawing between begin drawing and end drawing CES you can actually put all sorts of things uh there's a lot of different functions that can draw different Primitives shapes and stuff like that even 3D 3D is available in this library and after that once you're done with your application you just close the window just go ahead and try to build this entire thing obviously it's not going to build because it cannot find the R.H the R.H is available in the include folder so what we have to do when we build our application we have to specify where to search for the header files right so why the minus I flag let's go ahead and try to rebuild it one more time and this time it builds but it doesn't link so it cannot find all of these beautiful functions that we try to call the thing we have to do now we have to link with the library lib Ray li. a the static one and it cannot find that and this is because it is located within the liap folder so let's fix that as well let's tell the compiler where to search for these libraries as well with a minus L flag so let's put dot slash in Here and Now it complains with other problems now it complains with not being able to find mathematical functions like a secf and stuff like that and it's easy to fix by just linking with the math library and there we go we successfully build an application that created a window with a red color that's it that that's literally it and you get a native application pretty self-contained one that doesn't really depend much in terms of dynamic libraries right it only depends on lip C and lip M and stuff like that so it is a very cool Library I really recommend to check it out if you plan to make a game consider using R seriously it's freaking awesome uh but why I'm talking about rip it's it's a C library right so aren't we writing an a today right an a is supposed to replace C and here is the biggest secret in Tech the biggest secret in Tech is that majority of the useful code on planet Earth is written in C and C++ and if you're making a language and you want that language to be useful in the long term you need to link you need to tap into this unlimited useful code base that is written in CN C++ in fact if you are programming in Python majority of the Frameworks that you use are basically wrappers around some CN C++ code they just are all of the machine learning Frameworks py game the the thing that you use for for making video games like all of that is a wrapper around code and because of that this entire idea that we're going to solve the memory safety of a applications with just this magical language that we all simultaneously going to switch to it sounds kind of weird to me because we have so much useful code already written in CN C++ and it's enormous amount of effort to rewrite all of that and nobody's going to do that L literally nobody's going to do that you you start pushing your language like Ada or rust or anything like that and then you realize that you can't really do anything useful in it unless you link with CN C++ code and all of your memory safety features just evaporate right because you're already using this unsafe code but it's a useful code it's a lot of useful unsafe code so because of that I feel like approaching this problem with memory safety to Applications by introducing a new language and forcing everyone to use this language it's not a particularly productive direction to move in honestly a more productive Direction in my opinion would be trying to do something with c compilers c and C++ comp compilers have a lot of different interesting flags that allow you to enable out of bounds check memory sanitization and all sorts of things like a stack fortification there's a lot of interesting things you can do with the c and C++ compilers already I'm not saying that they solve the problem of memory safety but I'm saying that this could be a more productive direction to move to because that does not mean that we have to throw away all of that useful code that we rely on so yeah so that that's basically my take on this me safe languages it's pretty cool but the reality is majority of the code on planet Earth is written in C in C++ and we have to do something with that as any self-respecting language that is trying to be useful ad is capable of interfacing with C in fact we can just try to go ahead and uh link with rip in Ada let's open our hello world in Ada super quick and try to compile it like so I'm going to do not make and it just build it as it is and if we take a look at the flags of not make there should be a thing called L ARS yeah there we go so L args and the thing about L ARS is that after L ARS you provide the arguments that are going to be passed to a Linker right so that means one of the things you can do you can take all of the Linker flags that we passed to AUM pilot down there and pass them to nut while L ARS like so yeah so it's already built so it's actually forc to rebuil and it actually linked with rap it passed all of the flags to the Linker but it doesn't really do anything because we don't call any of the rap functions the way you interface with C in a is by using interfaces. C package that comes with the standard library of ADA so the first thing we have to do we have to do with interfaces C and also we have to import all of the names from this package and essentially we have to forward declare the functions that we want to use specifically we want to use the function init window from Ray Li uh we can take a look at the definition of this function function we can just open the header and find the init window function so here it is here is the signature of this entire function we're going to put it in the comment in here and it has only three arguments width height and the title so the thing we have to do in here we have to put the definition of this function between procedure is and begin this is the place where you're going to do all of the definitions that going to be available down there between begin and end so what we want to do we want to declare a function init window which is going to accept parameters width as you can see you first provide the name of the parameter and then the type so we use the type int and the type int is actually imported from the interfaces C the official integer type of ADA is actually integer so this is how you declare a variable this is the variable X which is an integer int is the integer specifically for interfacing with C so the second parameter that we want to accept we want to accept height and it's also an integer and as you can see the parameters are separated by semicolon similarly to Pascal and the parameters with the same type can be grouped together with a comma like so again like in Pascal so and the third thing we have to provide in here we have to provide the title so as far as in our interface you see experts special type called chars array and that's how you pass strings to a c function in fact this function does not really return anything so because of that it must be a procedure so since it's a declaration we apparently don't have to put is after this entire thing but we have to tell it some extra information about this function so first of all we have to say that this function is going to be imported function right so import is true the next thing we have to do we have to say that the con the call convention for this function is C uh so convention C and then we can even specify the external name right but since this function is already called exactly like the C function we don't really have to do that but to be fair in Ada you don't really use camel case in Ada you use snake case and camel case simultaneously like this because of that maybe it does make sense sense to provide the external name because of the convention of ADA it's going to be slightly different though in my opinion it doesn't really matter but I kind of like to follow the convention of the language that I'm programming and that's it so we just declared this function and we can now try to call this entire function so let's provide the size of the window 800 600 and then we have to provide the literal that says hello from a but this is not going to compile right because string literals are not charge arrays we can try to go ahead and just try to to compile that so chars array is undefined it's called Char array sure there we go wait it actually compiled surprisingly so I wonder if it's going to work or not because by default a the literals they are not n terminated so right now we're passing thing that is not n terminated to a c function and that may blow up but anyway so let's try to actually run this entire thing and it actually successfully run so as you can see it started to log relief stuff and then it exited right so because we didn't organize an event Loop so to organize an event Loop we need a function window should close and it doesn't really accept any parameters the only thing it does it returns a Boolean so and we can copy paste this entire thing in here so it's also imported convention is C and the name of the function is window should close there we go so now we can organize the loop while not window should close as you can see instead of exclamation mark we have not in here just like in Pascal to denote the body of the loop we have to do Loop and loop for whatever reason it's not a begin end and in here what we have to do we have to actually begin drawing and here is an interesting thing as you can see if the function or procedure do not accept any parameters you don't have to put parentheses in here just like in Pascal and similarly with begin and end drawing so let's actually again copy paste this entire Stu so this is going to be begin drawing since it doesn't return anything it's going to be a procedure begin drawing and then I'm going to copy paste it again this is end drawing so let's try to uh maybe run around this entire thing so it didn't compile because you have to say return so not everything just like in Pascal right in Pascal you would use this kind of thing to do knowe that the function returns something in a it's returned so there's a little bit of a discrepancy between a and Pascal so be aware of that right so Boolean is undefined I think it's C Boolean right so I don't quite remember yeah it is in fact and we just created a window uh with the title hello from a theg blend Funk new termination n termination if I'm not mistaken to make it n terminated you have to use a function to C so and that thing will take a string literal and basically convert it to char array also n terminating it automatically so let's actually give it a try and there we go so it says hello from a so yeah there we go you can already write games in this thing using R and stuff so in fact you can link with any c code in here and since you can link with any c code you can do a lot of us use ful stuff with this language already and that's literally what I did with my game so as you can see I'm basically developing my own bindings and a lot of people get frustrated when they see me defining bindings myself because Ed has some sort of bindings generators but the problem is that I kind of don't trust binding generators they usually fail they do things slightly incorrectly then you have to go and troubleshoot all of that and I dealed with this kind of stuff so many times that I don't even want to go into that that in the first place but maybe binding generation in a is actually perfect I don't really know you go try it and tell me right so I'm just doing things my way another common criticism that I quite often hear is why don't you just use the Bindings that already made by somebody else and the thing is bindings are not a useful code it it's not particularly useful code it doesn't solve any problem except interfacing with actually useful code and by their nature bindings are just added complexity and if you're using that edit complexity that you don't control you are in trouble I like to be in control of this additional dependency that I introduce but then you may say isn't it a lot of work defining uh all of the functions of rip there's a lot of functions in rip like it's like you're wasting time but here's an interesting thing do you really use the entirety of radio or the entirety of any library in fact like imagine you using free type a library for rendering fonts are you using 100% of the API of this Library be honest you don't and essentially why don't you just like Define only the functions that you use that is not that hard because again you're probably using like 1% of the API anyway and you get a thing very simple thing that you control because the problem with somebody's else bindings is that they're not complete they can be abandoned and so on and so forth right so but here you have a thing that does exactly what you want and you control thus you minimizing the surface area of failure the more dependencies you can control control the less likely you're going to have problems in the future so that's my general approach you don't really have to do it like that you don't really have to do it like that in fact feel free to use somebody's else bindings feel free to use the binding generators like who am I to tell you what to do what I'm doing in this video is just sharing my Approach if you don't like it don't do it all right so we established how to make video games in a for Linux but play video games on Linux for video games there's a special niche operating system that is developed specifically for video games called Windows the real question is how do you ship your game on Windows luckily since nut is part of GCC it is available in ninja W so that means you can actually cross compile your game and furthermore in the releases of R there is a version of r that is specifically compiled for mja w so let's download mja w we actually need 64bit version we get there we go and let's unpack it the thing about MW is that it works not only on Windows it's not just Port of GCC to Windows it also works on Linux which enables you to cross compile from Linux to Windows the utility that you need is actually called x86 64 w64 a m w32 not make win32 this is a version of nmake from mjw it is probably a separate package on your Linux distrib distribtion so you will have to install it accordingly you can use this thing as a drop in replacement for the nut make that you use for building Linux applications so let's take the command line that we use to build our game and literally replace the nut make we also have to change the path to the libraries accordingly so now it has to be win 64 instead of Linux like so and let's go ahead and just run this command line and see how it goes and all right it complains about missing some things and these things are windows specific if I remember correctly those things are win mm and gdi32 right so these are like Windows standard libraries and there we go so this thing just produced a Windows executable and what's cool is that this Windows executable will work on wine I can run this XZ from wine and I also tasted this kind of executables on my actual windows Windows 8 I still have Windows 8 and they do work so and in fact that's the approach I used to deliver my game on Windows and it works it works beautifully so we know how to write video games an we know how to build the executables for different operating systems we can ship it on Linux we can ship it on Windows as far as I know there is an a compiler for Mac OS since the source code of the game is open people have submitted build scripts for Mac OS and reported that it does in fact work there but unfortunately I'm too poor for Apple products so I can't verify it myself uh but let's now talk about the language itself the language itself is rather interesting throughout my 20 days journey I enjoyed it a lot but the main problem that I had is documentation since not that many people program in that language not that many people write about that language and documentation the state of the documentation is kind of subpar one of the website that you will probably stumble upon immed mediately when you start learning Ada is learn a core.com so this is basically the the website the learning website by the developers of ADA as far as know the ad core is the company that develops a right now uh right it's an okay place to start but then later the examples that they provide they I would describe them as not deep enough at least they were not deep enough for me personally one of the things I had to do quite a lot is to actually grab the standard library of ADA so one of the things I did is essentially I would go to user folder in here and I would just find all of the files that end with ads because these are Ada signature files and that will reveal the folder where the standard library of ADA resides and I would just go there like so and here are all of the files in the standard library and this allows me to just grab some things for instance I want to find a function that converts uh literal to string and there we go so I can actually find it in here and read more about it since it's a ADB file this is implementation and stuff like that so that was actually extremely useful in fact this is something that I generally recommend to anyone in any language don't just read documentation also read the source code for instance when you're just looking at the documentation for a certain API and you're reading about a certain function don't be afraid to pick into the implementation of the function to get more insight into how it works that way you can actually learn more about the library or the language that you're working with and it's especially important when the documentation for the language or library is lacking at some point you will have to read the source code and that's what I generally recommend the best documentation is the source code because it never gets outdated because if it gets outdated it does not compile anymore people are forced to update it so yeah let's talk about the language itself the main thing that you see instantly about the language is its syntax I'm not going to focus on a synta too much because I don't think it's the most interesting part of ADA honestly I think it's the most boring part of ADA the most uninteresting one I'd rather focus on the semantics of the language it has pretty much everything you would expect from a procedural language there's also a little bit of O features from the op craze era I personally didn't use that some people do use it the most interesting feature for me personally was Adis enumerations and how they synergize with Adas arrays let's talk about that so let's define an array shall we the way you define array in a is the same as in Pascal yet again you say the name of the array and then you say the keyword array and then you have to define the range of the array so a doesn't Fair one based indexing or zero based indexing it's up to you to decide right so you can Define from one to 100 right so in that case it's going to be one based indexing and we're going to say that the elements of the array are going to be integers let's initialize this array let's iterate R starting from 1 to 100 the entire range of the array and then we have to assign a value to each individual element so let's actually use the double of the index as the value there we go we initialize the array so after that let's go ahead and print each individual element of the array a has a second way of iterating arrays which is similar to for each so essentially you can say for X in AIS and that will iterate each element of x's by a reference X is a reference to each individual element Loop and loop and and then we can go ahead and print the X so to convert X into a string you have to use single code image don't ask me why this is just a being a if you want to convert some sort of a type to a string you have to do the image so a string that represents the type is called image for whatever reason let's go ahead and try to print this en thing and something went wrong because because you supposed to use of instead of in to do a for each of course of course and there we go so we actually initialized and printed the array pretty cool so if we have to change the capacity of the array we have to change it in two places in here in the definition in here in the loop so there is a way to actually fix that so you can take AIS and say that you're iterating the range of that array so essentially this part of the array definition is called range and you can take the name of the variable and you can take its range and that will work so you only have to modify the capacity of the array in one place in fact you can take the range and turn it into its own separate type right so Define a new type and you have to use a keyword type then let's call this type something like access index and then we're going to say that this particular type is a range from 100 to 200 and that enables us to use literally this name instead of the range and that will actually work almost yeah and this one is very interesting because access index creates a completely separate type which is not comp comptible with the standard integer so here I I can't just take index and just like assign it to here because the element has supposed to be integer so I suppose one of the things I can do in here I can forcefully cost an index to an integer and that will uh that will actually work so essentially if you have a variable which is called I and it's an integer and it's something like 69 you won't be able to index access with that specific variable at all because it's not compatible with the expected type type of the index That's The Power of adaa so why that's so powerful there is a very common trick of using indexes instead of pointers in languages like C C++ Ras and generally in all the languages you can say that index in Array is kind of similar to a pointer it's a relative pointer a pointer relative to the beginning of the array and in fact indices are such a powerful technique that they allow you to circumvent borrow checker in Rust and rust developers will go completely ballistic if you tell them something like that indices do allow you to bypass borrow Checker because if you treat index as sort of like a reference to a particular element in an array you can have multiple indices to a same element in the array simultaneously by which you're going to be modifying things thus bringing back the problems that the borrow Checker is supposed to solve and it's a very common way to again circumvent borrow Checker and it's a very easy way to implement for example a circular link list in Rust but the problem with just indices instead of pointers generally even in cc++ is that pointers at least have a type associated with them that they're referring to when you are studying to use indexes instead of pointers you're kind of losing information about which array that index is indexing a is actually fixing that problem you can take the range of array and turn it into its own separate type and tell a use that type for index in that specific array so now the index actually restores the information about which array it can index like so right and it not only becomes stronger typed not only becomes stronger typed but it also becomes self-documented if I look at I and I look at its type I know oh okay that thing index that specific array they are now connected somehow on the other hand if it was just an integer I would never know if I is somehow connected to access unless there was some sort of a comment some sort of a note telling me that explicitly but now I don't have to have a comment for that I can see that in a code directly very much convenient and by the way this is what I constantly use throughout my game if we open the source code and take a look at the game State all right so game state is a huge structure right so the structures inat are called records so you define type game state is record and you can have fields of the record in here so in here I have an array of entities I I call it eers if we take a look at the definition of eers array it is a type which is an array which is indexed by eer index and the range is from one to 15 because the game is very much simple I just don't expect to have that many entities in the game so I basically have a fixed array of entities and what's cool is that now I use this eer index instead of a pointer to a particular entity throughout the code base so let's actually take a look at that right for instance I have a function that answers the question eer can stand here so and in here I accept the state of the game the position within the game within the grid of the game and then the index of the eer and then I look up that specific eer and I take its size and I just check if this thing can stand in here it becomes a replacement for a pointer throughout the entire game furthermore uh you don't have to use ranges as an indices for the arrays you can also use enumerations right so for example let's Define enumeration which is called I don't know direction right so and the kind of directions you can have in a grid based game is usually left right up and down and there we go so you've got a direction and now you can define an array which Associates a Direction with a specific Vector let's actually call it something like Direction vectors right and it's going to be an array indexed by direction of for example Vector 2 and Vector 2 let's say is going to be something like like a record so records are structures and so essentially it's going to have two Fields X and Y which are going to be floats and then you can even reinitialize this sort of like a table saying that for instance the direction right is associated with moving to let's say one Zer right all of that has to be floats by the way this is a very cool feature of ADA again if you have a record you can have record literals so essentially you put parenthesis and within parentheses you sequentially put a values of each individual field in here but if you want to be specific you can actually put a name in here like so there we go and you can basically associate each individual Direction with some sort of a vector right so moving to the left decreases X moving up does not change X at all but it decreases y moving down increases Y and there we go now you have a table of different directions uh which is rather convenient and it only can be indexed by that enumeration and nothing else it cannot be indexed by integer you can force for convert an integer to a Direction but accidentally you can't just use a wrong type for an index of that specific array and since this array doesn't ever change you can also make it constant it will prevent it from modifying it and maybe enable some optimizations I don't know so and this is exactly what I was talking about when I said that arrays and enumerations synergize in Ada in fact I think I already have the type Direction defined in my yeah there we go so it is defined in my game and I have precisely this kind of table uh so let's see where else I use this kind of stuff all right so here I'm iterating each individual Direction so let me take a look at the context of where it is defined uh recompute oh yeah so here is the path finding right so I'm using BFS to make the enemy pathfind the player and to maintain the wave at each individual iteration I have to look around the cell and because of that I'm just iterating each individual direction right and for each Direction I get the direction vector and I compute the new position based on the old position and then check whether I have to go there or not so this is one way to use this kind of feature and stuff like that and I use this kind of stuff throughout the game and it really helps me to build up this the main specific dictionary of the problem I'm soling it is really freaking cool it might not be a super impressive feature for people who like sophisticated languages like C++ and rust and stuff like that they like to bathe in these complicated abstractions and everything but as a person who really tells you Simplicity as the person who really admires Simplicity I really like this simple feature that allows me to describe so many things without even writing a single comment because I can bake a lot of information I can bake a lot of story into these enumerations into these ranges into those indices I can really tell a story by writing a code and this is the coolest feature of a in my opinion and it might not be impressive for a lot of people but I really really enjoy and I wish more languages really married together arrays and indices and to be fair this video doesn't do the justice for AA because AA has a lot of interesting features that I just cannot cover on this video because it's already too long so for example I didn't even take a look at such thing as spark which is a formal verification tool for a so essentially it's some sort of like a small subset of ADA with additional thing that allow you to really prove the correctness of your application which is very useful for application that really need like an extra security I also didn't look at the concurrency in adaa so the a has a very interesting take on concurrency that I tried on one of my streams and I kind of enjoyed it a has a lot of interesting stuff and I wish there a lot of people explored this language so we had more information about it so yeah anyway should you bother with learning AA if you're only starting programming and you're trying to get into this industry because there's a lot of money in here right and you're looking for some language that is easy to learn and that will score you a job at Fang don't bother with this language this language is definitely not for you who this language for I think I would recommend this language for people who are passionate about programming they are passionate not only about programming but also software engineering and by software engineering I mean not just writing code but also trying to produce an executable that has certain kinds of parameters certain kind of performance a certain kind of memory footprint not just writing code but actually engineering a piece of software I would recommend this language for these kind of people and also if you are passionate about programming languages in general this language has quite a few interesting idea that you may enjoy and will this language replace C and C++ I don't really know and I'm not sure if I even care I just like programming
Info
Channel: Tsoding
Views: 74,539
Rating: undefined out of 5
Keywords:
Id: MUISz2qA640
Channel Id: undefined
Length: 44min 56sec (2696 seconds)
Published: Sat Apr 13 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.