Advanced Godot | How to Integrate Rust into Godot

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys this is mitch with fine point cgi  and today we're going to talk about how to   use rust with godot so first we're going to talk  about installing rust then we are going to go   ahead and set up our demo project and we are  going to go ahead and compile our rust module   and then finally executed inside of godot so  that's what i have in store for you guys today so   let's go ahead and get started okay so the first  thing that we're going to need to do is we need to   go ahead and download rust so we're going to come  over here and pull down the 64-bit edition of rust   so let's go ahead and click that and save  it and we are also going to need the llvm   snapshot here which is a compiler so  this is a lv lvm compiler here and   we'll go ahead and pull that down as well so we'll  grab the 64-bit edition and save that as well now you may or may not need that if you have  visual studio installed it just kind of depends on   what you installed with your visual studio but i'm  going to go ahead and pull it down anyway so i'm   going to open up my downloads here i'm going to  go ahead and double click rust up init right here   and then we will hit 1 and hit enter and this  will go ahead and install rust now while this is   installing let's go ahead and click on our llvm  installer let's double click on that click yes   and then it will go ahead and unpack our date and  the reason why i'm doing both at the same time is   because it's it takes a few minutes for both of  them so now that rust is done we'll go ahead and   hit enter and let's wait for our lvm to finish  up so we're going to go ahead and click next   and then we're going to hit agree and we will go  ahead and add it to system path make sure that you   add it to system path or this entire installation  is borderline useless so we'll go ahead and click   next and click next and then install okay so we're  going to go ahead and hit finish and now from here   we need to go ahead and set up our project  so we're going to go to my documents folder   we're going to right click hit new go to folder  and we're going to call this rust demo project   we'll go ahead and enter into there and then  we are going to click here and type cmd and   that will open up a command prompt here for us  so what we need to do now is use what's called   cargo and cargo is rust's build system  so it actually builds your project   for you and does all of that process so  what we're going to do is we will type   cargo space new double dash lib because we're  going to make a library and we can at this point   we can name it whatever we want so i'm just going  to call it rust demo project all right you will   see that we got a folder here that now has a  bunch of stuff in it so now what we need to do   is we need to go ahead and open up visual studio  code so we'll say code dot and that's going to go   ahead and open a visual studio code for us so you  will see that we now have visual studio code here   with our demo project here now one of the things  that we should do is install the rust bindings   that way we have all of those cool little rust  bindings that we have so if we go to extensions   and then we type in rust you will see  that there is a rust programming language   for visual studio code which will provide you  know hints and things like that so we're going   to go ahead and install that one other plug-in i  would make sure i pull down is the rust analyzer   i don't pull this down immediately but i  do pull it down later after the tutorial   is already finished but make sure you pull this  down it really helps with syntax highlighting   so make sure you grab this one and then  what we're going to do is we'll go back   to our explorer and go ahead and open up our  cargo toml file so we open that up and you   will see that it has our package it has our name  our version our edition of rust that we're using   and we have some additional you know cbar keys  their definition and that's just basically saying   go out here if you need more information but  down here we have this thing called dependencies   and dependencies is where you include stuff  into your project so we need to go ahead and   include gd native so we'll go gd native and  we're going to use version in my case 2.9.0 and that's just going to allow us to have those  bindings for gd native for godot so that way we   can actually use godot's stuff with rust also one  of the other things that we're going to need to do   is up here let's go ahead and add a lib section  and let's go ahead and add a new crate type   and what crate type is is it is a what is this  going to be right so you're saying like this   is going to be a library or something like  that right so if you hit uh bracket bracket   and then quote and i want it to be a cdy lib  so i'm going to make this into a library file   and that's what i'm telling the compiler to do is  to make this into a library file so we're going to   go ahead and hit control s and save and then from  here we'll open up our src file and click on our   lib and you will see that we have some code here  now the first thing that we need to do is we need   to make sure we have our using statement so if we  come up here we can have use gd native colon colon   prelude colon colon asterix semicolon and what  we're saying is we're going to grab all of the   gd native code libraries and just kind of pull  it into this specific class then we're gonna go   ahead and hit ctrl s make sure we save and then  i'm going to come down here and pull all this out   because we don't need it and we're going to say  hashtag drive and we are going to say native class and then we're going to say bracket bracket inherit and we're going to inherit from  our node class and then we need to make   ourselves a structure so we're going  to say struct and we'll say hello world all right and then we're going  to say bracket bracket g d native   colon colon which inherits from methods so what  these hashtags mean is they are called attributes   and it's something to tell the compiler different  information about your code so we're saying hey   we're deriving this from the native class so we're  basically telling rust to generate ourselves on   a native class and it's saying hey compiler we are  inheriting from our node class inside of gdnative   and then here we are telling the compiler that  this is the next section that we're about to write   so our classes are a part of the gdnative methods  so we'll come down here and say impul which is for   implementation and hello world and then we are  we'll open and close our break our brackets here   and then we'll say fn for  function new underscore owner colon and node and then we're going to create  a memory pointer to self so we're going to say   this is pointing to ourselves hello world and  then we're going to make another compiler flag   here and we're going to say let's export which  will go ahead and expose this function out to   an external entity so in this case  it's going to be godot so we'll say fn   underscore ready and then we will say  and self comma underscore owner colon and node there we are and we will say godot  underscore print exclamation point hello world   all right and now that we have this we can go  ahead and do a function outside of our little   implementation class called init and what we're  going to use that for is we're going to use that   for when this file is ran this init here is  going to be ran so we'll say handle colon a knit handle and then we'll say  handle dot add underscore class   colon colon hello world and then we'll go ahead  and call our godot init so godot underscore   init exclamation point init awesome so what did  we do here so we derived this entire thing from   native class we inherited our node we put this  class here as a part of our gdnative methods   we created a function of when this is  created it's kind of like construct   constructor so it's saying hey  when this is created do hello world   and then we are exporting this out to  godot and then we are saying godot print   and then we're saying hey when this file exists  go ahead and make sure you add your handle class   so add it to godot add the hello world class  to godot and then go ahead and initialize godot   so now from here if we go back to our  command prompt here and we type cargo   build oh i'm in the wrong directory cd  rust demo project and then cargo build   so what this is going to do is it's going  to go out and fetch all of our dependencies   that we have so you're going to see this fetch  happen where it's going to actually go and pull   all of our dough dependencies it's going  to go out and and fetch any other compiler   dependencies that it needs and once this is  completed i will be right back all right as   you can see it has compiled correctly so that's  awesome so now that we have this let's go ahead   and open up our explorer so you can see here's our  explorer and we're going to need to go ahead and   add a godot project here so what i'm going to  do is i am going to go ahead and open up godot   and we will go ahead and add in a new project now  in my case i'm just going to grab my rust demo   project location i'm going to come over here  paste it and i am going to create a folder called   godot rust project it's very creative we're going  to go ahead and create our folder and create an   edit okay so now that we have godot open we need  to go ahead and copy our library file over here   into godot so we need to first right  click create a new folder and call it bin   and then what we need to do is we need  to click on our folder here go into our   rust demo project go into target and go into  debug and then grab our rustdemoproject.dll   and then we're going to back out to our rust  project we're going to go into our godot rust   project we're going to go into our bin folder  and we're going to paste this into that folder   now that we have our dll into our folder what  we need to do is we need to create a link   for godot to know that this exists so we're going  to come out here into godot we're going to click   the little create new resource button here and  then we are going to go ahead and make a gd native   library here you can see that this is basically  just a giant repository to point us to where our   library is located so we're going to scroll  down here to windows 64. we're going to click   on the folder icon we're going to go into  bin and we're going to click on our rustdemo   project.dll so we're going to double click on that  and then we are going to click on the save icon so   we can save this so we'll click save save as and  then we want to make sure that this is a gdn lib   file so we click that hit save and there  we go so now we have ourselves a gdn   lib file we can actually rename this instead of  calling it new gdn lib i probably should have   named it hello world you don't have to name it  hello world but i'd rather be named hello world   so we'll call it that and then we need to go ahead  and use our library so let's go ahead and make a   2d node and let's right click add in a regular  node if you remember we used node here and here   and here to say that this is a type node if this  was a sprite you would need to attach it to a   sprite if it was a 2d node or a node 2d then you  would have to attach it to a node 2d so if i come   up here right click attach a script and let's go  ahead and attach a native script and let's call it   hello whoops hello world dot gdn all right and  now from here we need to come over here to our   library section and add our gdn lib file so we're  going to click on that click load and we are going   to point it to our hello world gdn lib hit open  and now we've added that to our project from here   let's go ahead and save our scene and let's just  call it rust test scene now let's go ahead and   hit play and we need to go ahead and select a  main scene so let's select our rust test scene   oh but it does not look like it  worked let's see what's going on   okay so i figured out what the issue was so what  you need to do is you need to open up visual   studio code and open up your gdns file you'll  see that there's a section here called resource   and what it's missing is you need to  say resource underscore name is equal to   hello world and then you also need to  have class underscore name is equal to hello world and what that'll do is that will  actually allow you to reference that code   inside of godot so godot doesn't know that  it exists because it looks at it and goes   yeah you have a external library but i don't  know what class it is so after you do this   you go ahead and you open up godot and then you  hit play and you will see hello world will appear   awesome what do i think about rust and godot i  think that it is way simpler than c plus plus   and i'd be really interested in doing  a performance benchmark on this to see   how well it stacks up to c plus plus because i  think that it'll probably be pretty close since   it basically compiles down to native script it  should be almost the same and it's a lot simpler   to set up than c plus plus though again that will  be changing with the native script changes that we   have so i think it's an awesome way to code inside  of godot but that's all i have for you guys today   so if you like this video go ahead and hit that  like button hey you know if you dislike this video   go ahead and hit that dislike button because i'm  here to make content for you guys this video was   a viewer's suggestion as with most of my videos  so if you guys have any suggestions throw them in   the comments below and i'll be more than happy to  make videos on whatever suggestions you guys have   and hey if you have any questions about this  or if you just want to have someone to chat   with either throw a comment down below or jump  on our discord link is in the description we'd   be more than happy to chat with you we got some  really cool guys out there but that's all i have   for you guys today so thank you so much again for  watching and i will see you all next time thanks you
Info
Channel: FinePointCGI
Views: 1,815
Rating: undefined out of 5
Keywords: godot, godot game engine, godot engine, godot tutorial, godot getting started, godot beginner tutorial, getting started with godot, godot engine tutorial, what is godot, getting started with godot engine, godot explained, godot engine tutorial 2d, godot engine tutorial 3d, godot engine tutorial for beginners, godot getting started 2d, godot getting started 3d, learning godot, learn to animate in godot, godot scripting languages, godot review, godot scripting
Id: dTD-_pQ9Vl4
Channel Id: undefined
Length: 17min 41sec (1061 seconds)
Published: Mon Oct 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.