Games in Godot! Let's make a classic paddle game with C#! | .NET Conf 2023 Student Zone

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] going to make a classic paddle game with CP I am Michael a hawker and while I am an engineer by day I've uh been working in the industry for over a decade I am a game designer and developer by night it is a passion of mine I've done a number of projects in this space I've created a mobile puzzle game I've created a programming game I've even kickstarted my own board game uh but this summer I started tinkering with got and c and wanted to kind of share what I've learned with you today uh and how we can make games together so what is Gotto Gotto is a free crossplatform open source game engine it can be used to create 2D or 3D games or any combination there in between and it supports programming with their builtin uh scripting language C++ and of course why we're here today is C and the community also supports various other levels uh different programming languages so there's a little bit of uh something here for everyone and if you've played any of these hit games lately such as R cassette beats domee keeper you've actually already seen what got can do uh to create games there are uh pretty awesome uh things out there that have been made with this engine and a little tidbit is it's named after the play Waiting for godau so that's where the name comes from uh so if you are familiar with unity at all already and migrating to G um or just also to introduce you to some of the terminology here in G itself today if you've done neither then I wanted to kind of introduce some of this terminology you'll hear a lot during this talk uh so scenes represent everything that you're going to see on the screen for your game so that it's kind of similar between unity and GTO um the other thing you'll hear a lot in unity is called prefabs and these are objects that you're going to be putting on the screen or dynamically creating with your code and in Gotto it's also just a scene um so scenes really encapsulate anything you're either going to see on the screen or that you're going to create multiple uh copies of within your game uh so it's pretty straightforward from that standpoint you'll see a theme here with GTO as well in unity you have game objects which represent those things that you're putting within your scene and something that you're going to manipulate within the game world uh and in got those are called nodes and similarly in unity you have game objects that have these things that are added with components that kind of describe the behavior in them and in Gau they're just nodes so pretty much in Gau everything is a node you're creating this hierarchy of a tree of nodes that represent the different things within your game so you might have a player that's a physical object and then within it it has a node that represents the visual display of that object as well as the shape of how it's going to interact with different physical objects maybe it plays positional audio uh it has a camera following it so all those different things are going to be different nodes that construct this hierarchy within the tree um and then all of those can be uh encapsulated within a scene that's displayed on the screen but you can also encapsulate any of those other subp parts within their own scene so if you wanted to have multiple enemies you can make an enemy a scene and then use that multiple times within your other scenes so everything's kind of composited and boils down to a node within gutto so it's pretty straightforward but uh it makes really easy and flexible to do whatever you need to and construct things however you'd like within the editor uh so to get us started I have a template that I've created for net new uh which works with visual studio and visual studio code and that I've tested with the latest godo engine on Windows and makes it super simple for us just to get started and hook up our debugger and be all set for game development uh so there's a GitHub repository here it's also on nit so we're just going to use the net new tool directly from the command line to get us started so let's go go gadget gdau and dive into working with the game engine we're just going to go to their website got engine. org and you can see it's the game engine we've been waiting for uh potatoes in the background there we're going to download their latest uh version here and if we look on this page there's a Gotto engine.net and so this is the version we actually want to download to be able to use C for our development so I'm going to click on this and it's going to download the zip file and if I open this up and look in here we have an executable and so we're going to want to copy and extract this to somewhere on our hard drive I've actually already done that so I've just created this folder and copied gdau here and if I double click on this executable it's going to open up the GTO engine you can see this first time we're running it here it's going to say hey we don't have any projects would we like to open the assent Library I'm just going to cancel that for now because we're going to use this template uh to to help us get started with uh C development in GTO uh you can see one thing the template needs is got's location so to do this we can follow these uh step-by-step instructions I'll go through that quickly here um but this lets us easily kind of share between different projects or if we're working with other people we don't have to hardcode location of Gau within our project we can just have this environment variable set up um and so to do that we need to copy the location of G so we're going to come in here we're going to write uh shift right click actually that's going to give us this copy as path option and then if you search in your start menu for uh edit environment variables for your account that'll bring up this dialogue we can click new uh and then type in G 4 uh and paste in that path right so once we click okay and whenever we open up terminal or vs code in the future now we'll have access to those variables so if I go uh and look at this variable within my terminal that I already had open uh because I had already set this up beforehand we can see I have the path to where gutto is installed and so now we can just use net new install and I can use the name of that template mare. net. blank template and so it's going to pull that template down from nit and we can see it gives us this new template godon net and so now we can just type in net new goet net uh and then we're going to use minus n to give it a name I'm just going to call this got paddle and then we can use code uh to just open this up in Visual Studio Studio code uh so now we have Visual Studio code open we have a few different things uh at first uh there's going to be some recommendations for extensions that uh are usable by this template for doing C and gutto development in uh V vs code I've already installed those um but you can see we have a Gotto project file we've got our main scene and C file that this is kind of template set up for us we've got an icon and then we we got our C project and solution files and we have our vs code config here as well um and then Gotto creates this directory for its own internal use so you never really have to worry about that one um inside the launch config for visual studio code you can actually see where this environment variable is used to point out uh to visual studio code where got was installed uh if you didn't want to use the environment variable you could also just paste in and replace uh the quoted value here with that path um and then that should be everything that you need there um you can actually see that um the language service for gdo isn't connected because we don't have the editor open um before we kind of get started with uh debugging in Visual Studio code we have to make sure we've loaded the project at least once in uh goto so I'm going to go and click the import button here and so now I can just go to that folder where I've uh created my project and open this up within the editor So within the editor here we need to do one piece of configuration in the editor menu and editor settings uh if we want to use our external editor we're just going to scroll down here on the left to net and select editor and here we have external editor and we want to select Visual Studio code you can see there's if you wanted to use Visual Studio you could select that um if you disable the setting it's just going to use got's built-in script editor but we're just going to leave this as Visual Studio code because I changed that uh earlier in my settings and so now here we have the main Gotto window we have our main scene here which was automatically loaded uh you can see that from the file system down uh below here in this tab uh and then this represents our game screen right so this little bounding box here that you can faintly see represents what the window and viewport into our game um and so I can uh pan around here using the space button I can zoom in and zoom out um so it just kind of gives me that viewport to navigate into the world if I want to actually see what's in this game scene I can see that in the scene tab so you can see here we have this root node which has a script attached that's our actual main. CS um and then we have this Sprite icon here as well if we hover over this icon we can actually see the C file that we've attached to this root node here and if we click on something we can actually see in the Spector in the inspector the properties right so we have a Sprite 2D here which inherits from node 2D and then inherits from canvas item so this kind of shows you the composition of the node that you've selected on and allows you to edit properties so you can see position here as being set we could use this uh little reset icon if we want to change you know reset back to the default value if we want but I'm just going to leave that for now so we've got GTO set up we can go back to visual studio code um we can hit retry here and now that language service if you're doing anything with their built-in scripting language in the future in Visual Studio code will be all set and we're going to open up our uh main scene C file here that we attached we can see I've put a uh the template comes with a debug statement here I can actually set a debug point in the margin and now I can just hit F5 and you'll see that Visual Studio code is building already and it will launch launch our game and there we go it will automatically hit the break point and so if we now uh continue we can actually see that that game loaded message from our debug statement is printed out in do the console um and this ready method is just called whenever that node enters the scene for the first time just like the comment in the template says so uh we're all set up now we can use Visual Studio code for our game development and got and so now we need to actually create a game so one of the first things we need to do is uh for you know classic paddle game we need to actually have some paddles that we want uh for the game and we're going to have two of them right one for each player so whenever we're thinking about if we want to add a node to an existing scene or create a new scene we want to think is this something that we're going to use probably more than once and if that answer is yes then we probably want to create a scene so we want two paddles so that's more than one thing so we're going to create a scene uh to do that we're going to use this plus uh icon up at the top here and that's going to give us some options right we can create a 2d scene a 3D scene we can just create some UI or you can actually create a scene based on any other type of node that got supports um and that's what we're going to do here we want to we don't want to create a whole new screen of our game we just want to create something that represents the paddle you know this one physical object within our game and so we're going to click on other node that's going to bring up the create new node window you can see we have the 2D we have the the control the 3D we have all the different types of nodes we could create um and we're going to click in here and at the top there's Collision objects and physics bodies so these are all the different things that can interact with the physics engine so rather than doing a bunch of math we're going to kind of offload all the math or most of the math to the physics engine in got um and use make use of these types of nodes to help us um so there's things like our static bodies these are things that don't move in the game world we have rigid bodies which are things that we want the physics engine to control so that could be maybe something good for the ball we'll look at that later um and then we have character bodies which we can see here are some things that are moved by a script so we're going to have paddles we want the player to control those so we need some script there to actually handle that input and move it so character body seems like a good choice for us and like anything in game development or development in general there's lots of different ways to do things um and and how we could create this but this seems like a good starting point uh here for us in our first project so I'm just going to double click on that and we can see we have a character body it's going to have a little warning says that the node has no shape And so right and if we see if we zoom in in the editor with the zoom controls we can actually use this little icon in the top left to Center back on the origin um but there's nothing here right we want to actually have oops we want to actually have that paddle right we want to display a paddle we want it to have some physicality to it so uh we we need to actually add more nodes here to give this um some substance so I'm going to right click I'm going to add child node and to go back into our 2D nodes right we're making a 2d game and I'm actually going to select polygon 2D and so this is going to let us draw a polygon uh that's we're actually going to be able to see within the engine you can see these new tools appeared this is going to let me uh create a node uh or polygon within the editor so I can just create a rough paddle here and close that now this isn't very rectangular or Square um and you know I could fiddle around here and move these points um but that that's going to be hard to make perfectly rectangular uh so what I can do is I can go over to the inspector right I can look at the polygon 2D properties and we have this data um that I can open here and this represents the polygon you can see there's some value set because we created those in the editor and a lot of properties in gdau you can actually expand if you click on them so now you can see we actually have the four different points in here and so if I actually um set these values I can kind of more precisely Define uh the bounds of my paddle here to actually be completely rectangular um and then however this is just the visual right and we need to tell the physics engine what are the actual parameters for this physical object so we're going to add a child node and we're going to come into the node list and we're going to look at these two options for collisions we have a polygon which is what we created um but we also have shape and since we just created a rectangle and we're actually going to select the shape one that's going to add this new node and we can see over here we have a shape that's currently empty we're going to click on here and select rectangle shape that's going to give this little blue box that we can start expanding and we can actually make this the same size as our Visual and so now the visual representation of the object and the physical representation for the physics engine are going to be the same um so I'm going to uh save this now and so just use contr S I'm going to save it I'm going to name it paddle and so now we have a scene called paddle it showed up in our file system and now I can go to the root node and I can and click on ADD script and of course we're going to want to change the language here from ghos script to C and you can see there's some basic templates here uh there's one that assumes you're creating a character for a platformer we're going to use that actually as a base um we can see the name is paddle. C that's fine we're going to create this is going to automatically open Visual Studio code as our external editor here um I could optionally add a namespace here if I um want um this isn't really uh important it's really for your own organization GTO doesn't really um matter or look into the the C name spaces but you can see here that we've inherited from that root node so we our root node we selected was character body 2D that's the same class that our code will inherit from and we can kind of really composite things with c um however we would like we can have one of these classes used for multiple scenes we can have multiple classes for each scene we can kind of prescribe however we want in our game uh to whatever needs we need um so there's a lot of flexibility there which you'll get more comfortable with as you work with c and gutto um now this assumes like I said that you're creating a platform G platformer game you have a character you're going to move left and right and be jumping and affected by gravity we just have a paddle we want to move up and down um so we're going to delete our jump and gravity code here and same here for this code that's being handled uh in this physics process method so this is run basically all the time in the background for helping process physics and there's this input helper called input get vector and this is just going to take the input uh from gutto and give us a vector that represents its direction and rather than using the x coordinates for left and right we're going to use the y-coordinates for up and down and so we're just going to change that here and then we're going to uh set that changed velocity back and then this move and slide kind of does all the magic for us automatically from GTO so we can save this script now and we can go back to the editor we can save our scene right now that we've attached that script here we can see that's attached to the root there and if we go back to our main scene we can now just drag this paddle into our scene and there's our paddle we can move it within the scene here and we actually want two paddles right so we can actually just rightclick and do duplicate and now we have a second paddle in our game that has the same exact Behavior as our first one right maybe we'll name this right paddle and I can just double click and name this left paddle um and now let's run it see what happens and so here's our game I can hit left or up and down right we we change from left and right to up and down and we have our paddles that can move perfect right uh next step is to add the ball uh so the ball we just have one of and so we don't really need to create a whole new scene for that so I'm just going to do do the work to add the ball in uh our main scene here so I'm going to go to our node TD 2D I'm going to add another child node and we want this to be a physics object as well right we talked about the physics objects and looking at the physics body we want to actually have this be controlled by the physics engine so we're going to make this a rigid body and so I'm just put that at the top here um I can name it ball right and then let's use this icon I can just drag that into the ball to represent the visual um this might be a little bit larger so I can shrink it down I can hold shift to make sure it stays square right and it's in the middle um but one thing we'll note is there's this other little um Crosshair up here and that's actually where the ball is right now we can see it's highlighted in Red so the icon is offset um so every node can kind of have its own local position within the editor um so we're actually just going to go over to the position here and reset it um so that way everything is uh centrally located here and this kind of how you want to set up most um objects initially right I can zoom and then use this little icon to Center so now we can see our ball um and then we'll move it back after when we're ready to uh have it where we actually want it to start in the scene uh so I'm going to right click on the ball again we're going to um add another child node right and we need to add that uh Collision shape right we want to make sure that the ball has some uh some boundaries that the physics engines knows right that represents what visual that we're currently displaying is so I'm going to add that again I'm going to go over to the shape I'm going to click and select rectangle shape right this gives me this light blue box and I can just drag that over here now of course I could make I could load an image that's an actual Circle and then make this a circle if I wanted just doing this for Simplicity um but now we have this um uh this ball that has a visual and a collision right so we can zoom out and we want to uh move it um over here now if I start dragging you can see it's actually grabbed the Collision shape I'm going hit control Z and undo so I'm going to select the ball and then I'm going to go to uh the move mode here and so this is going to give me these handles right and so that way I'm actually moving this entire node um so now that way everything is moved together so just something to be aware of when you're moving nodes around that you're making sure you're moving the actual node that you want so in this case I want to use move the parent ball rather than it's visual or um physical uh properties for the physics engine um great so now that we have a ball we want to add another script to this as well um and you can see it's remembered that we're doing C development so I can create a ball and this is now inheriting from rigid body 2D and we have the ready method so when it enters the scene we want the this ball to go somewhere um so we're going to use a method called apply impulse and what this is going to do is tell the physics engine to just basically shove this ball in a Direction um and so to do that we're going to grab a unit Vector so uh it doesn't really matter which one um but we're going to rotate it we're going to use this rotated method so basically we're going to take this little vector and we're going to rotate it in a random direction right because that's what we want um and so if we look at how many radians are in a circle right any direction we could go that's 2 pi so we're just going to um use the gdau method for random uh Rand f is going to give us a value between 0 and 1 and then we're just going to multiply that by two and by pi uh and so that's basically going to give us our random direction right we're saying give us a random a random number between zero and one multiply that by you know whichever angles we can go within a circle um and you know we're going to rotate a vector a unit Vector from that to but that's going to be super tiny right we want to have some oomph to it so we need to multiply this entire Vector that we've created by a value so I'm just going to go in here and I'm going to say multiply that by 300 so this is just going to give some initial Force to the ball and so now if we run this I could have also just hit F5 and vs code we can see that the ball has gone down in a random direction if I run it again it should go in a different direction there we go um it seems like there's a little bit of a curve to it it's also just going off the screen which we don't want uh oh see it went up and down so that's curious um we actually want to go into our project settings now and down below there's physics and 2D and you can actually see there's some default gravity value right we don't want gravity in our pong uh pop paddle game and so we're going to turn that off um and uh and so now when we hit play we should be able to see the ball uh go in a direction and it's just going to stay going in that direction uh but it was also going off the screen right so rather than trying to uh worry about you know where it actually is uh we can actually just use the physics engine again to help us with this we're going to add another child node and now this time we're going to use another physics entity we're going to use a static body right this represents a fixed space um on our level and we're going to add another child node right because we need to give it a shape so we're going to use Collision shape again and I'll uh zoom in here uh and then I'm going to select rectangle right and so now I can take this rectangle and we can just basically create this barrier at the top of our game right and then I can duplicate this shape I could also duplicate this the body but it's easiest just to duplicate the shape and there we go so now this represents a wall on the top and the bottom of our level and so here we go we can see the ball has now stopped but it also did not bounce uh the physics engine is also now to the rescue if we go click on our ball we can go into all the properties for rigid body 2D and we can actually see there's this physics material this represents the properties of the ball we can just click and create a new material and then click on it again you can see there's friction we don't want any friction for our ball we also want it to bounce and so now if with those those simple changes we've manipulated the physical properties of the ball and we can see that it's bouncing off of our paddles and the walls everything is looking good uh it is seemingly slowing down so that's another problem by default uh there's this thing called linear damping and it kind of represents kind of a rough idea of friction within the world over time everything will slow down and kind of come to a stop um and we don't really want that for our our ball as well so we're going to go back down into our ball properties look at linear and by default it's just going to combine whatever we've set here to the default U we're just going to replace it and then just keep it as zero so we're going to have no dampening of any kind uh on our ball so it'll always maintain its velocity and so now once the ball goes it will continue going and we'll bounce and there's our basic paddle game and so from here um we that's kind of all the time we've got for for now but I will be continuing this uh talk at net comp and so from there we'll kind of look at how we can look at different messages and kind of create scoring um and maybe kind of shore up some of the physics uh a little bit uh with how the ball collides with the paddle and making sure that that doesn't happen uh and maybe giving the ball a little bit more speed when you hit it type things um I might add a little bit of those extra details into the sample um that will be shared and I can actually uh just thank you for your time and joining me here today and if you want any of those resources the repository with this demo code is up in the uh repository here um there's other resources here you can use and of course be sure to catch me at done ATC as well we'll take this project uh and start adding some scoring and doing a little bit more with some C uh directly and there we [Music] go
Info
Channel: dotnet
Views: 3,788
Rating: undefined out of 5
Keywords: .NET
Id: gdQjX2qcoNU
Channel Id: undefined
Length: 28min 50sec (1730 seconds)
Published: Wed Dec 13 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.