Setting Up Rust in Godot 4.1! Advanced Godot 4!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys this is Mitch with finepoint CGI and today we're going to talk about how to use rust with Godot I know I did this tutorial I want to say almost two years ago but so much has changed so I thought I would update it for Godot for 1.1 and see how the integration works now so we're gonna go through the process of installing rust then we're going to go through and actually build a cargo Library so that way we can actually build our library we're gonna talk about how to compile and do things like that now we're going to talk about how to handle different node types so in this case we're going to do a node 2D but of course you can use any node that you want then we're going to talk about how to hook it all up and get it running and we're going to talk about how to set up an API entry point so you can actually call it from GD script and call signals from your rust implementation and finally we're going to talk about how to do break points and debugging within rust so that's what I have in store for you guys today so let's go ahead and get started started okay so the first thing that we need to do is we need to actually install rust and rust up is basically what I've been told is one of the standards of rust so we're going to go ahead and install this one so we'll grab rust up Dash init.exe right here or if you are using the windows subsystem for Linux you can actually do curl Proto and then use this it's up to you on whichever one that you want to do if you're running Linux just grab this curl here and drop it into your terminal and it'll automatically install everything in my case I'm going to click on rest stop exe I'm gonna let that go I'm going to keep it because I have security controls on this so it's not going to let me download things and we'll double click on Rust up now it's going to run through and install rust up and this may take a while so I will be right back now once you see rust is now installed or rust is installed now great then you should be good if you hit hit enter key to continue like so it's going to close it so let's go ahead and open up a terminal real quick so we'll type terminal or if you are a person that uses the command prompt you can just type CMD and that should work now you should be able to type cargo and it should just show up with some information here cargo is basically the way you interact with rust so if you've never used rust before that's one of the big things is Cargo is that big package manager so it's kind of like npm or JavaScript it's gigantic and it's very important to understand cargo so just keep it in mind and make sure you learn it really well now first things first we need to create a rust project and that's actually going to be relatively easy what we'll do is I'm going to make dur and I'm just gonna call it Godot rust tutorial and that's just going to make a directory so I'm going to be able to CD your dough rust tutorial and that will get me into that directory and the reason why I'm doing that is because if I create a new crate in my main fine point CGI directory then it's just going to be existing in my main directory so I wanted to make sure that that's not a thing so now to make a crate all we have to do is say cargo new we have to come up with the name so we'll just call it test crate dash dash lib and what this does is it creates a new crate so basically it creates a new library for your Godot project now dash dash lib actually creates it as a library because if you don't do a dash dash lib then it's going to create it as an executable and that's not going to work with Godot so we need to actually make it a lib we'll hit enter and it's going to create one so it says created Library test crate package so if we hit LS you'll see we have test crate here CD test crate like that and we are now in that crepe now what I like to use I like to use Visual Studio code if you have vs code installed that's perfect because that's exactly what I'm going to use so I'm going to type code Dot and what that's going to do is it's going to open up vs code so I'll snap this guy in and I will trust the author so you can see here this is our rust project now you should install some things to work with rust rust is not something that's super simple to use and without having some kind of intellisense you're going to be up a creek really quick so we're going to go to extensions and we're going to type rust and it will come up with an extension here called rust it's up to you if you want to use this one or if you just want to pull the rust analyzer totally up to you on which one you prefer rust itself comes with rust analyzer so it comes with this and it comes with crates and it comes with rust syntax so it's up to you on which one you want to do I'm gonna do Rust right here because it comes with a lot of the common stuff that I like to use so we'll click install and we'll just let that pull it in all right once we have that we can close this go to our Explorer and let's open up our cargo.toml file and what we're going to do is we're going to add some stuff so first things first this has a dependency it has a Godot dependency so we need to say Godot is equal to bracket get equals quote https colon slash slash gethub.com slash Godot rust slash gdext quote comma branch is equal to whoa Master now I'll have this in the description so you guys can just pull this down if you don't want to type it and then we need to tell it to be a lib and we need to say crate Dash type is equal to quote cdy lib unquote that will basically say hey I have a dependency it's going to be called Godot it's going to be this Branch out on the Godot rust GitHub so we can pull that down and this is going to tell it to build it as a dynamic library and what that means is it means it's going to build it like a C plus plus project instead of a rust project so that allows that interoperability between C plus plus and Godot and rust so that's extremely important to have so we'll hit Ctrl s and then we'll take a look at our lib.rs so the lib.rs is your library.rs so most of this is pretty much useless we don't actually need any of this all this is it's just an open library but since we're not going to be using any of this it doesn't really matter so we'll get rid of that and we're going to set up some basic stuff so first things first use Godot colon colon Prelude colon colon star semicolon and then we're going to create a struct so we're going to say struct and our extension and this is where we get to name our extension inside of Godot so you can call it whatever you would like in my case since this is a test extension I'm going to call it test extension and then we need to kind of register that so we'll say pound bracket bracket GD extension unsafe impel and then we will call it extension library for test extension bracket bracket and basically what happens is we're building a struct that basically just says hey this is a tag right that exists inside of Godot more or less and then we have the unsafe implementation extension library for test extension so this is basically saying this is an extension Library which is extremely important now to actually interact with the Godot system we need to actually create a class so we can come in here and just say use Godot colon colon engine colon colon node 2D and that's basically just going to pull in godotes no 2D code and it'll allow us to actually build a node 2D node inside of Godot and now we can basically just derive our class from the Godot class and then set up our class we can say pound bracket bracket derive Godot class pound class base is equal to and we have to choose our base which is going to be a node 2D because that's what we picked up here so because we picked that up here we need to kind of match those up and then we have to create our struct so struct test bracket bracket and then we can set up any of our special parameters that we want so for instance if we were making a character controller we could have like their speed or their jump height things like that but in this case since we're basically just doing a hello world we'll just keep it empty and if we want to override any special base parameters we could just go pound bracket bracket base and then from there we could actually pull back those base parameters so what base basically allows you to do is it allows you to declare a field that allows you to access the base instance so a lot of times you'll need to use this if you need some way to access your previous instance so you could say node to D colon base node 2D and that basically allows you to access that if you need to and now we can basically implement it so we can say use your dough colon colon engine colon colon node 2D virtual and basically that just is using the node 2D virtual which actually allows us to pull back a lot of their virtual functions for that specific class so it's really important that we have that then we could say hashtag Godot underscore API impl node 2D virtual 4 and we have to use our struct name so test bracket bracket and basically that's creating our class we're saying that we are implementing this as this so this struct here is getting the node 2D virtual stuff and the Godot API is basically registering test as a part of the Godot API and now from here we can basically create our function so FN init and we'll take in our node 2D base node 2D so we'll basically say node 2D colon base node 2D and we will return so pointer self like that and then from here we can basically just do whatever it is that we need to do for an init function so we can basically just say Godot underscore print hello like that and what that's going to do is it's going to print to our console hello when we get to that point now we also can set up anything else that we need so for instance while we're in our function here we can also say self bracket bracket node 2D like that and that'll basically just return our base node 2D as our self so that way we have access to all of those parameters and all those things from our base node now I am not a rust developer so I'm sure I'm explaining this horribly so I'm sure you know please correct me in the comments because I don't know much about rust but the init here is basically equivalent to the godot's init so it is when it gets created this is what it runs more or less now you might be asking okay but how do I actually do something over time well that's where FN underscore process and mute self comma Delta colon f64 like that and that'll basically give us a process with our Delta as if as if it would be in GD script and basically we can just come in here and say Godot underscore print and we can just say hello from process and I'm going to bring in my Delta like that and it's mad at me so let's take a look at it there's Associated function with similar name of process I actually think I need to get rid of the underscore let's see if that fixes it there we go and now it's going to get mad at us no rules expected for token plus so for right now what I'm going to do is I'll remove this and I'll just say hello from process because I want to spend too much time debugging it so we'll hit Ctrl s and then we can go into our terminal here so we can go up to run view terminal and it'll bring up our terminal here and then we have to build it so cargo build enter and you'll see hopefully everything will compile correctly so it's going to download everything from the Godot core and do all the things and then hopefully it will build correctly so we will see now it looks like it did build it as an unoptimized debug Target so perfect that's exactly what I wanted it does give us some uh helpers here and warnings but that should be fine so what we'll do is I will right click reveal in file explorer and that's going to take me right there I'm going to go to my test crate and underneath Target debug it should show everything that I need now in Windows you will see a DOT dll if you are in Linux you'll see a so file and if you are in Windows or Macintosh you should see Dy lib so we'll grab the test crate dll we'll copy it and what I'm going to do is create a new Godot project so we'll come in here new project and we'll come in here and say rust tutorial create a folder and create now we're going to right click create a new folder we're going to call it lib enter we're going to right click open in file manager and we're going to paste that test crate.dll in there now if we click back to Godot you'll notice that nothing happens so what's going on why isn't it working well for us to actually have this stuff work we need to have what's called a GD extension file and a GD extensional file a GD extension file allows Godot to know that this is a library that we want to load so we're going to right click new text file we'll hit Ctrl a get rid of it and call it test crate.gd extension like that and we'll hit yes and it's going to make it as an extension file now I'm going to open up my vs code here I'm going to open up my lib folder and drag this up into my vs code so that way I can edit it now GD extension files are designed to teach Godot how to read your extension so what we'll do is we'll put bracket bracket configuration enter entry underscore symbol is equal to gdext underscore rust underscore init and then compatibility minimum and we'll make that 4.1 because this is for good o four one if you are running Godot 4-0 this would be Godot 40 right you'd put 4-0 if this was three five you do three five though that being said if you are in three five just be aware that you need to use the Godot native not the GD extension there's a two totally different worlds here so you'll want to make sure that if you are using three five that instead of doing Godot Russ GD extension you do Godot rust GD native and it should in theory work we'll go back to our GD extension and what we'll do is we'll come in here bracket bracket libraries and here's where we'll Define what our libraries are now in our case we only have windows so I'll say windows.debug dot x 86 underscore 64 is equal to quote res colon slash slash and now we have to tell it where it's at so lib slash and I believe we have it under just test crate.dll so test crate.dll we'll copy and paste that and we will say Windows release x8664. now you don't have to do the windows release if you don't want to but in my case I am just because it's good if I need to do a release I'm not getting debug I'm not getting compiling errors and things like that but you should do a release version of your crate and change this in the future so now we'll hit Ctrl s and we'll go back to Godot now if everything's been done correctly hopefully you won't have any errors in your output in my case I don't and I have my GD extension here so you can see that it can see that it exists and we'll see if this works hopefully by creating a 2d scene right clicking our node 2D add in child node and we go to node 2D we should hopefully see our test node right here so you can see that under node 2D we have our test node so we double click that guy you can see it says hello right here because it's being initialized so if we hit clear and hit play and we save this there you go so hit stop you will see hello from process and hello so now your question might be okay I can now access it so what else can I do with it well it's like any other language really so you can do pretty much anything that you want so for instance if we're for instance if we're in our process let's come in here and pull out our print here if we were to pull back let's say when the user hits an input we could actually set up a basic controller so we could say let input equal input colon colon Singleton let mute velocity equals Vector 2 colon colon new bracket 0.0 comma 0.0 like that and then we could just say if input colon colon is action pressed and we can pass in our input like that and we have to pass in an action now this is where things get a little strange so string name colon colon from string quote I'm going to say a dot unwrap then what we can do is say velocity dot x minus equals 1.0 like that could basically duplicate this guy four times and I can say a d w s and then I can come in here and just say Plus y minus y plus like that and what that's going to do is it's going to allow us to move our character left right up and down and then we can say let mute pause is equal to self dot node 2D dot get position like that self dot node2d dot set position pause plus velocity like that and you'll notice that I have a self dot node 2D so what does that mean well self.node2d if you remember up here we set ourself as node 2D which was a type node 2D so that's how we get back our own objects stuff so that's how we actually pull all that back so we can hit Ctrl s now you might be asking okay hold on a minute so you're telling me that every single class that I have has to be done inside of this and the answer is no you don't but it is a little bit complicated to do it inside of another file so if I come into my SRC I hit plus and I type something like player or I guess in this case since I'm doing test we'll say test.rs we'll come in here and we'll just grab all of this so we'll copy it go into test RS we'll paste we'll come up to RS we'll grab these two guys and we'll come up here and paste it then we could probably pull this guy up to the top as well like that and that should work now if we come in here and we compile so if we hit view terminal up enter you'll see a we get an error so we have some warnings um it's mad at us because we have an extra equals so it's going to be plus equals not plus minus equals we'll hit up build it's mad at us because we have oh it's because this hasn't been removed yet so we'll come in here we have duplicates here so we'll come in and pull all of this out like that like control s we'll hit up build and you'll notice that it's built so now we can come in here go into our library come up here go into our debug if you'll see our modify date has changed so I'm going to make sure I refresh that copy paste replace and you'll notice that we have an issue folder is in use cannot be completed because a folder or file is open in another program now this is a known issue in the Godot game engine so unfortunately what we have to do is we have to close Godot try again and then we have to reopen Godot and reload our project now unfortunately that is something that Godot is aware of if you are on Windows that is going to be a problem so just keep that in mind and you'll be okay now if we right click add in a child node and we type in test you'll notice that our note is gone so what happened well when we compiled our library this whole system does not know that test RS exists so we actually have to add it so we have to come in here and say mod test and now if we compile it and we close Godot like so and we come in here we copy this and we paste it and then we reopen it you will notice that now if we right click and type test you will see that test exists so that's something to keep in mind you need to keep a reference of that class somewhere inside of your lib RS using mod now granted there are some people who probably know rust way better than I do so they'll be able to explain things way better and probably be better at handling all of this than me but that's just something to keep in mind now for this code to work we need a input mapping so we'll come up here we'll go to project project settings we'll go into the input map we will add in a new action w a s and d and we'll say w is w a is a s is s and D is D we'll hit close I'm going to drag in my icon for my Sprite so I'll put them right on where my node 2D is and I'll put them right underneath my test.tscn and now if I hit play in theory three I should be able to hit was and d and my character should be able to move awesome so now we're pretty much there right it all just works so what else can we do well we can actually build our own functionality into Godot and what I mean by that is this is us just building a actual character controller but what if we wanted to be able to access information and call functions from GD script well that's where the pound function comes in so if I hit pound bracket bracket funk FN hello world like that and I pass in and mute self comma words colon string name like that in theory this will work we'll see because who knows so we'll come in here and say Godot print words like that and then let's say we wanted to have it emit a signal so when you call this I want you to emit a signal right so what can we do well we can come in here and go FN hello world signal like that and then we can go pound bracket bracket signal like that and then we can come in here and fire that off by going self Dot node 2D dot emit signal and we could pass in our signal so quote hello world signal dot into like that and then we can pass our variance so I'm going to say and open close because I'm not going to be passing anything let's see what happens if we try to compile it it might compile it doesn't look like it's going to so let's see I think I'm going to need to do this let's grab this guy pull it out and say impl I think that's going to take me let me see yeah that'll take me out Implement test like that bracket bracket paste let's see if it takes that instead and we'll annotate this with the pound bracket bracket Godot underscore API let's see if that will take it now we should be able to build and it's still mad at us so what do we have it's mostly small stuff but we do have some small stuff so there's no rule let me try changing this to a type string instead and see if that allows this let's see the following other types can be used good dough string so maybe we'll have to do good dose string instead that makes that happy but this is trying to be a FM literal see if two string does two string work in Rust so let's see what happens if I do quote quote bracket bracket comma words to string and let's see if that builds hey built all right so I guess that's how you have to do it so once we have that let's go into here let's go into our debug because it built finally we'll come into Godot rust will close it we will copy this guy paste it and replace and then we're going to right click the dough engine and we'll snap this guy in here and now in theory we should be able to do if we look at our test we look at our node we have a Hello World signal so now we can just go attach a script node2d.gd and we can say let's attach hello world signal to here and type print bracket bracket got signal call like that and on ready we can go dollar sign test Dot and you should see the function hello world and we have to pass in a string so we can say this is our test string like that and if we hit play hello this is our test string got a signal call and our stuff once again still works awesome so basically that's everything that you need to know but I know the question that might be on your mind how would I go about debugging this and now that is a really painful question but I will show you how to do it so if we go to our vs code here we can come in here and go into our run and debug and we can create a launch.json now it's going to have different extensions here and you'll notice that they're all basically useless so what we'll do is we'll go to install extension for rust and we'll come in here and type code LL D B and that is a native debugger so we'll click on that bad boy and we will install it and what this is going to do is it's going to allow us to debug C plus plus and rust code right inside of our editor so now we can come over here to run a debug click create a Json file and we'll pick ldb or lldb now you'll see that it has all this stuff in here type request name right debug we can actually name this whatever we want we'll come up by our request we'll hit quote pre-launch task and we're actually going to pass in a pre-launch task so we'll say rust colon cargo build and that's going to set it up so that way it builds our project when we click on debug beforehand and now we need to give it some arguments so we'll come into the arguments folder we'll hit Dash e comma Dash W and we need to wrap that in a quote real quick gotta come down here comma quote windows and we'll tell it what to run so we'll hit quote C colon slash slash and we'll pass in where our Godot is located now in our case if I look at this guy here I go to my downloads and then I go to I believe I have it you doe four one stable copy as path and then we'll come in here and just paste and then what I'm going to do is just double slash all of our slashes like this and hopefully this will just work so we'll see though because it's definitely a little bit on the finicky side for me so hopefully this will be relatively painless now if you want to you can come in and actually set up your Windows Mac and Linux ones as well if you prefer to do it on maclytics or Windows and I think this has a colon expected which means I probably have something wrong here if I put a comma Now I figured it out so you need to come in here and type program quote colon space and then quote like that and then we can get rid of this little program here so we'll just get rid of that and in theory this should allow us to do debugging now we'll see so we'll come in here and I'm going to do a break point on my process node right here and I'm going to hit actually I'll do it on here there we go we'll hit play okay so I figured it out so basically what was going on was a my program I have here was the wrong program I was giving Godot 4.1 instead of 4.1.1 so that was my fault so when I'm doing my building I'm actually building for 4.1.1 so that's the reason why this went kind of kaput on me now in terms of arguments I actually got it wrong we're supposed to do dash dash path and that will be the path to our rust project so in this case it's C users 5.cgi documents rust tutorial and finally the other thing that we have to do is we have to go into our Godot here go to our project project settings go into run and set our main scene as the scene we are going to debug and then I will save Godot I will close it so we'll click debug and hopefully there we go it's loaded our project if we click on test.r RS you can see I have a break point here on my velocity I'm going to hit a and it will snap to that thing you can see I actually have my static my globals my references it is a little bit on the slow side something I've noticed is the debugging is very finicky I don't know if there's something that I'm doing necessarily wrong here but it does look like it is a little bit on the slow side like you can see it's thinking as it's going and if I hit play it doesn't really do much else so I don't know if there's something that I might be doing wrong here but that's basically how you set this up and you can see that it actually works you can see I pulled back all that information my node information my Delta my input and I can hit play and it will continue to work so it does operate it's just a touch bit finicky if I hit a again you can see I got my break point and I can step through my code like that and that's basically everything that you pretty much need to know about how to use rust inside of the dough so if you like this video go and hit that like button hey you know if you dislike this video again hit dislike button because I am here to make content for you guys this video as with every video on this channel was a viewer suggested video so please if you have any suggestions throw them in the comments below or you can jump on my GitHub and throw in a suggestion I actually have an entire GitHub dedicated to all of my suggestions and I take them very seriously so if you have any please let me know and hey if you guys have any questions or comments jump on my Discord links in the description and I'll be more than happy to help you out providing that I of course have the time but that is all I have for you guys today so thank you so much again for watching and I will see you all next time thanks thank you
Info
Channel: FinePointCGI
Views: 11,238
Rating: undefined out of 5
Keywords: godot, godot 4, godot engine, godot tutorial, godot game engine, godot 4 tutorial, godot 4.0, godot 4 beta, godot 3, godot 4 getting started, godot 4.1, godot getting started, godot window settings, godot settings, game development in godot 4, godot project settings, post processing effects in godot a quick overview, understanding godot 4, new features in godot 3, godot 3d, simple level select screen in godot 4, godot anti aliasing, godot 4 introduction
Id: z14cfTc40uQ
Channel Id: undefined
Length: 35min 40sec (2140 seconds)
Published: Mon Sep 25 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.