Advanced Godot | Creating Custom Android Modules

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys this is mitch with findpoint cgi and today i'm going to talk to you guys about how to create your own android module inside of godot so we're going to go through the process of creating an android module we're going to go through the process of hooking that module up inside of godot and we're going to talk about how to return data from your module and how to emit signals from your module and return data as well so that's what i have in store for you guys today so let's go ahead and get started [Music] so the first thing you're going to need to do is you're going to need to go ahead and download android studio if you don't already have it installed now i have a few videos discussing android studio and a few videos talking about how to export custom exports inside a good dough so make sure that you go through those videos before you do this video because you're gonna need a lot of the tips and tricks that are inside of those videos including why you'd want to use android studio and why you um or at least and how to hook up all of your you know all of the android functions such as adb and you know the other modules that are out there for android development now the next thing that you're going to need is you're going to need to pull down the gedot aar add-on for developing modules so if you scroll all the way down here you'll see there's an aar library for android plugins now you can either pull this one down or you can pull down this one which is a mono c-sharp version both work well i'm going to pull down this one just because it's the standard and i'm using gd script so it just works out but if you are using the aar module with mono then go ahead and pull this one down instead all right so the next thing that we're going to do is we're going to go ahead and open up android studio now in this case it'll probably pop up with a previous program yeah you can see it's opening up with admob that's okay if you guys haven't seen my admob tutorial that's where this one actually shows up and we have a conversation about how to open up an apk inside of your project but if we go here we click on file new new project it's going to pop up and we're going to say no activity because we don't need anything special to do our development we're just doing a no activity project and we'll just call it i don't know my application that seems fine because we're not really going to use any of this this stuff doesn't matter this is literally just creating a new project for the sake of creating a new project so we're going to go ahead and do that you'll see it'll pop up with a new project and it's going to go ahead and download a boatload of data so you'll see here it's going to start pulling down all that data so you know i'll get with you guys once this is done pulling down all that data all right so now that it's done downloading all the little things that it needs to do your gradle building which if you don't know gradle is basically the building system that android uses to build your modules so it's the thing that that creates all the connections and things like that that's going to go ahead and create your aar module so what we're going to do is we're going to hit file new new module okay we'll click on that and then we're going to click android library we'll click next and then we're going to call this hello world one of the biggest classic uh things to learn is uh the hello world we're gonna make this java because i'm old and i like java better than colton and byte code level don't worry about that minimum sdk we're not using anything special so we can go ahead and just use the jelly beam sdk and we'll hit finish and you'll see that it's going to be downloading and loading more stuff so you'll see that we have our beautiful little module right here now what we're also going to need to do is we're going to need to pull down the godot bridge for your android module now we already pulled that down that was this little aar library for plugins so what we'll do is we will reopen up our project and then we will hit file new uh module and then scroll down import jaw jar aar package so we'll double click that and we need to point it to our location so if we click on that little um folder icon and then go find where we did it which in my case it's out in my downloads folder so we will hit uh that hit ok you'll see that it's named it for us let's go ahead and copy that name you don't have to copy that name but i would copy it and then hit finish so now you're going to see it's going to load that in so you'll see it right here now the next major step that you're going to need to do is you're going to need to come down here to your gradle scripts you're going to need to come over to your application hello world don't worry about your project like i said that is not needed we just had to add that so we could make a project and we double click on this you'll see that we have this really interesting build.gradle now what this file does is it tells the gradle build system what is needed to run your project so there's a lot of really important stuff here like your compile sdk version your build tools version you know what your minimum sdk version is now this is a huge number because if for instance you want to do like admob support right you want to build a plug-in that does admob you would need to make sure that your minimum sdk version matches what the minimum sdk version is of the things you're trying to implement and that by extension means godot is going to need to match that minimum sdk version as well and if you guys want a tutorial on that let me know and i'll make be sure to make a tutorial on what the minimum sdk version is and why you want to use it and things like that so if we scroll down you'll see a little dependency section here now this is the dependencies of whatever your project needs to have right so like in this case you need to have android x app comp uh app comp which is the the um application general um it's basically your android libraries same thing with like material library this is a library that's used to do material objects you know the j unit which is for unit testing um this one's also for unit testing and so is this one these actually all three of these are unit testing so we're going to do is we're going to implement this implementation and we'll get rid of that project because we want to pull a project in we don't want to pull in a library want to pull in a project right and then we will say uh parentheses colon and then we're going to paste the name in there so that way it has our godot library now if you don't if you didn't copy it you can actually pull it from right here if you open up the the build gradle for your library you can just pull it from right here because we named it the same as our file so we're gonna come down here put that in here and now comes a very important part we're going to build it and the reason why you want to make your project is because if you don't make your project then your compiler and your ide is not going to make the links that it needs to do to allow you to continue so let's go ahead and make the project so you'll see this little gradle build is running and we're just going to let it kind of do its thing and it's going to take a few seconds it could take upwards of a minute or two so i'll jump back in here once it is done alright so now you will see that we actually have this added here which is a nice little generated build config this is how the ide makes its links and knows what to talk to so what we're going to do is we're going to open up our java and you'll see right here we have example home uh for example hello world example hello world android test test these are stuff that's just for unit testing don't worry about that but it is always good to unit test your code you want to make sure that your stuff comes back the way that you expect it to come back now in this case i'm not going to run through all that but it's just something to keep in mind that unit testing your code is extremely important when you're making large projects now if you right click here click new and then click java class we're going to call this hello world because we're just making a hello world little hello world project so now what we're going to do is we're going to say this needs to extend and we're going to say org dot actually i don't think we need to do org i think we can just get away with the dope plug-in and that'll automatically load all that in so there you go so now we've got this little cool thing that says hey this extends good doll plugin simple enough right but you can see we got this really crazy little underlying here and what it's saying if we hover over it it says hey this must be declared abstract or implemented the abstract method get plug-in name in godot plugin well okay let's go ahead and implement it right it's saying hey you need to implement this method or else you can't extend the godot plugin so let's go ahead and implement that you'll see it says hey here's the methods to implement so we just need to implement the one so let's go ahead and implement it and then well it looks like we got another little uh red underline here so it says hey you don't have a default constructor you need to create a default constructor so let's create one so you can see here now it just created a constructor for us it did all that by itself which is good news what is this well get plugin name is the plugin name for godot so when you are trying to reference that plugin in code you need to make sure that the name is something that is readable and understandable by godot and what i mean by that is for instance if you name it you know hello john right or hello mitch right it will it'll require you to say does the singleton mitch exist inside of godot right so this is kind of how you name your singleton so what we're going to do is we're just going to return quote hello world simple enough oh no space we shouldn't have spaces so let's go ahead and save that so what we just did was we created a really simple little plugin so now right you would be able to load this inside of godot now i wouldn't do anything but you can load it inside of godot some of the things that the documentation doesn't talk about and neither does the extension here they don't enforce you having the some of these functions in here you know if you don't have them you can't run the project so what we're going to do is we're going to create a get plug-in methods here so we'll do a public and we're going to do a list of string and we are going to say get plug-in methods all right and you'll see suddenly it's going to yell at you say hey you can't resolve this so let's go ahead and import that class and what this will do is it will give godot the ability will say non-null because you want it to be not null and you want it to be an override right because you want to override the base class godot plug-in so make sure that i spelled it correctly because my spelling is horrendously bad and then what we're going to do is we're going to say hey i want to return a array a-r-a-y-s dot as list and we are going to say uh whatever we want to return now what this is it's for your plugins methods but we don't have any methods so let's go ahead and create one so public string let's say let's say it's going to return a string call it hello and we're going to say that and let's say our yeah we'll return quote hello world so now we got a function here right that returns hello world simple enough right and it looks like required type is string provided oh sorry capital s you can see here we have this little string hello but we need to tell godot that it has a function that it can run so because if we were to have you know 30 functions in here we don't necessarily want all of those exposed took a dose so you need to tell godot what functions it uh can use so you can see here that's what this little function here is for so we're gonna do is we're gonna say hello and now this little bit of code or i guess this little function here is gonna be exposed using this little bit of code we're gonna do is we're gonna save and we're gonna click the little build icon which is up here if you don't know that's where it's at so you just click that and it'll build it so now you can see it says hey the gradle build is finished and it says hey it dropped it out into my android studio project so let's go ahead and go out that way and you will see that i have a hello world project here so if i go inside of here and i go into build and outputs aar there's my aar file so now we have our aar file right that's our android library aar is android archive something they should they call it android archive library but aa r i don't know why they have the r there maybe someone can tell me in the comments but it they call it aar so i just call it aar so what we need to do is we need to create a godot uh gdap file and what a gdap file is is it's a file that is used to [Music] and i'll put this in here so you can see i'm what i'm doing is i'm just doing a code dot so that visual studio code opens up in this location what it does is it tells godot what um aar file it's looking for and it gives it a lots of lots of different information and i'll go through some of the information for you so we'll do is we'll create a new file we will call it hello world.gdap whoops no caps g-d-a-p and you will see that we have ourselves a little gdap file so we'll do is we'll say um open um little brace close brace and config enter enter i like to put two enters name is equal to quote hello world unquote binary underscore type is equal to quote local binary is equal to quote hello world dash debug dot aar and now you have some options here okay and i'm not gonna go through the whole process here but you have some options here where you could say like dependencies and i'll just go ahead and post in some of the some of the commented code here from a different aar here whoops if you paste this in you can see here you can have what's called dependency so you can say hey i require this aar file also to exist so for instance if if you're using the admob plugin and you wanted to make a custom function for that admob plugin you can say hey i require the admob uh plugin for my stuff to operate or you could say hey i require these remote plugins so you could actually say hey you know i need to use sql lite but i don't want to um bundle it locally with my project just go out and download a specific version or you can actually say i need a custom maven repo so that all of my plugins come from a specific repo for instance if you're running a company this is a really good option in this case we don't need any of this so i'm going to go ahead and get rid of all that and i'm going to go ahead and save this so now for the fun part so what i'm going to do is i'm going to open up godot and i'm going to click new project and i'm going to say android android module tutorial i think that's fair i i it'll work so i'll hit create now and i'm going to make this the right size for you guys so it's not completely uh blown out of proportion here there we are and what i'm going to do is i'll create a 2d scene i will add in a child node and we'll say a button all right so i'm just going to throw a button i don't know somewhere here and the reason why is because we're going to need to call that code so let's just get that out of the way so it's just done and then we need to set this up to be able to load and do custom android exports so let's go project install android build template and if you haven't already seen my tutorial on doing all of this go ahead and check it out i'll link it in the description or up in the doobly-doo around here at this point and uh you know just check it out so now what we need to do is we need to go to export add android make sure that we have that set up use custom build needs to be on and i think that's it for right now all right so now we need to do is we need to right click our res and open in file manager that's going to open up our android module tutorial folder and we will go to android plugins you'll see that the folder is empty so now we need to go and copy our aar file and our gdap file into our plugins directory so that will allow us to enable it and run that plugin so now you'll see we have an android plugins directory which is exciting so if you've gotten this far good job guys this is i know it's there it's just been pretty quick but um you know it's going through this is is you know a lot um so if you go to projects export you scroll down until you can find your little plugin or scroll up maybe scroll up in this case there it is you can see my plugin hello world hit on and hit close so now what we need to do is we need to code it so that we can access our plugin so what i'm going to do is i'm going to right click attach a script we'll call this hello world test because i mean we might as well and we'll click on our button hit node on button up go to our node 2d you'll see right here and we're going to say hey uh print first you know hey we we made it here just to see if the code's working we want to make sure that's working and then if our engine dot has singleton has underscore quote hello world so hey if you have the singleton hello world and and what all this means is hey you're quarrying the game engine hey do you have the singleton you know like you could try to load the singleton without checking if you have it but if you're playing it on a non-android device and you don't have that plug-in available to you because you're not running on android then your entire application is going to crash on you so it's a good idea to just say hey does this exist and if it does then let's go ahead and and continue through our code so what we'll say is hey print we got here exclamation point that's me trying to debug to make sure that our code operates right if i say hey we if i see hey we made it here and hey we got here then we're good right then we know that hey we've got the singleton and our button up function is working and it should work but just in case you know we want to make sure so var will say i don't know uh hello world is equal to engine dot get singleton and we want to grab that hello world singleton all right now that we have that we can say world dot oops actually we don't need world dot we need to do var s is equal to world dot hello because it's a function and actually we're not doing world we're doing hello world my apologies hello world.hello and then we need to do print s like that so now if i save this scene as hello world test you'll see that if i hit play on this scene hey we made it here but it didn't actually fire our our um singleton right it didn't actually run our singleton and the reason why no matter how many times i press this it's not going to do what we want it to do right and the reason why is because we need to do it on android itself we actually need to put it out and install it on our android device so what i'm going to do is i'm going to go ahead and plug in my android device and i'm going to allow all of my usb fingerprinting and stuff like that and let me there we go so now you're gonna see right here there's a little icon see this little icon and that little icon will allow me to install it on my android device now we're going to run into some problems because once you install it on our android device we're not going to be able to see if our plug-in worked it doesn't come into the output we're going to need to use something called adb to look at it now like i said if you haven't checked out my other tutorials make sure you check them out because like i said i go through all of this process for you so what we're going to do is we will click select device no main scene has been selected so we need to select one so let's go ahead and select hello world and now you're gonna see here it's gonna go ahead and download and run all of our stuff that we need to build our own project here and what i'm going to do in the meantime while that's doing that is i'm going to say adb log cat and you'll see that i've ran into a common problem here where adb is not recognized as an internal or external command and what that means is that we haven't pointed our adb in our path so we need to actually point our path i guess you could say so our command prompt to the adb system so we're going to do is we'll open up a [Music] browser here and actually i'm going to want to go with this one and we will go to c users me app data local android sdk platform tools and you'll see there's adb right there so what you're going to want to do is you'll want to copy this open up start menu and type enviro or edit environment system environment variables so we'll bring up our system properties here and we'll go environment variables and we will say hey let's go path edit you'll see here's a bunch of our path stuff i kind of went over this with our emsdk section so we'll do is we'll say hey i need a new path and we will paste our platform tools hit ok hit ok hit ok and then what we're going to do is no matter where we go so if i go to my desktop and i go cmd you will see that if i type adb you'll see that we have our adb information so now we can go adb log cat and you'll notice that suddenly our stuff goes crazy and that's because we're getting every log in the project so you can see here i just opened up the project and you can see how it's freaking out after it saved all that data to our project so if i hit the button you'll see that we got hey we made it here see that right here but you notice that we didn't get we got here which means our engine does not have that singleton which is the reason why i have a hey we made it here so that we could determine that the button did work and that we didn't get here right so now the question is why so let's go ahead and take a look so let's take a look at our project export settings we have hello world enabled so that's a good sign so that's our first problem solved said that one's good so now let's go ahead and take a look at our hello world um gdap you see hello world and that's all good that's good and that's good right it's pointed to correct debug aar file if i remember correctly let me go check real quick yep hello dash world aar no that's correct long as my spelling is correct no i'm second guessing myself yep that looks right all right so now it must be something in our android project so let's go ahead and take a look so i have get plugin name is hello world so that should be correct and it is returning our hello world project i believe i found the issue so it looks like something that i forgot to explain was in the android manifest on on your project so if you actually come to your project and go to manifest android manifest you'll see that there are and there's a lot of information here and we actually need to tell it specifically what um the code is behind um the manifest if that makes sense so we just say hey like this is a plugin and this is version one and it's a specific thing for android as like a singleton so what we're gonna do is we will hit open uh application and we're going to open metadata enter android name and we're going to say org dot the doe engine dot plugin dot version one dot hello world oh with capital h my apologies and then what we're gonna do is we're gonna say android value we're going to say com dot example dot hello world dot hello world so and then we will close our little bracket here so what does this do okay so what this does is it says hey and or godot is going to look in org godot engine plug-in for all of its plug-ins right and it needs to know what this links up to what function this link links up to so if you look at this i have here saying hey this this little thing here is going to link up to this class i guess it's class not a function my apologies but it's what class it links up to so you use this specifically to say hey uh when i load in the singleton i want to expose this class to the godot engine if that makes sense if it doesn't make sense leave a comment and i'll explain it more but basically that's how it works so we're going to do is we're going to save this we're going to click the build icon and you'll see gradle build has finished and if you don't know where this is the event log is located right here so just to let you know it is right here alright so now we'll open up our aar file here again we're going to copy it if you remember you just go to your projects location hello world in my case hello world whatever your project your plugins called and then uh build outputs aar and then we will copy these two and then what we're going to do is we're going to go into our plugins directory inside of the android module tutorial android plugins and we are going to paste replace then let's go ahead and re-export this out and let's see if that worked because to be honest with you i'm not 100 sure if this if this was the issue but we'll find out and and we'll see how it goes so let's replug in my phone and you'll see that it'll pop up right here so we're going to hit play and i'm not really going to be just showing my display because it's just me hitting a button so it's nothing too exciting on my end but um you know just showing you yes i am in fact hitting a button and then we'll say adb logcat so you'll see that's going to go crazy on us for a second and it'll go crazy on us in one more second as soon as it loads in the project all right so now that it's opened our project let's go ahead and click on the little button and you'll see if i cancel this with control c or control x whichever one i need to do all right you'll see here that we have hey we made it here we got here hello world so what is that doing hey we made it here we got here it ran grab this ran the hello world and printed s so what it did was it came all the way out to my application here and it ran and returned hello world from or i guess from here from my hello function it ran this came here and then printed that function or that hello world so that's how we actually got it to work so yay congratulations you know if you've made it this far good job you know it's doing this is kind of a pain in the butt so all right now but what if we want to do something a little bit more devious a little more exciting right so what if we wanted to do something like emitting a signal and if you don't know what signals are they're basically ways to run code inside of godot so what we're going to do is we'll go back to our android studio and we got a little bit more boilerplate code to get going to get this to work but we'll get it working so what we're going to do is we're going to create another function call it public um we'll save void we don't really want to return anything and we'll say uh send signal single uh sick and sick oh my goodness i can't spell send signal all right and what this is gonna do is it's gonna emit a signal okay so we'll say emit signal and we will say quote let's name it so let's just name it i don't know hell well let's name it hello and then we're going to say comma and now we can give it an object of argument so it could be anything you want it to be anything in the world in this case i'm going to tell it to omit a hello this worked exclamation point all right now this by itself is not going to work okay we need to first register it so comma quote send signal all right so now this will hook up with this and also we need to register the signal we can't just up and go hey signal right it doesn't work that way so we need to actually omit that signal or we need to tell godot that that signal can be emitted you know what i mean so what we're gonna do is we're gonna say public set signal information and you see it just did a bunch of crap so let's let's go back real quick public set you'll see here this is a pre-built module that godot's plug-in allows us to use to send signals and to actually create and show what signals that our project can have so if we hit tab bam it just built it all for us which is fantastic it saves so much time so we don't want this return so we'll get rid of that and we'll say set open and then signal sig info space signals is equal to new array set all right now what we're gonna do is we're gonna type signal [Music] dot add a new sig signal information and we're going to say quote whatever our signal name is going to be so in this case since we're already emitting it up here right this section here we're going to just take that and go hello because we want to name it and now we need to tell it what it can accept so and it can accept anything you want it to accept like you could say you know i want to accept uh faygo if you don't know what phago is it's a soda or like a coca-cola right you could just say i want to accept that object right but in this case i'm going to say string dot class you have to specifically call out the class because if you don't you're referring to the object not the class so you always just say whatever it is dot class it'll save you some trouble later all right now we want to return that and then return signals all right so what that's going to do is that's going to set us up to be able to emit signals inside of our godot project so we're going to build it so you'll see it's going to gradle build i am going to reopen up that aar location i'm going to copy this and you'll see here it says 4pm which is my time so that works perfect and then we'll go to plugins and we will go ahead and paste it so now i've replaced it if we go over here now we need to tell godot that it can read those signals so what i'm going to do is i'm going to say hey if the engine has singleton hello world right when they click this thing go ahead and run the hello world right and print s so let's do a little bit of restructuring here let's go ahead and go up here and go if actually let me just do this let's do a little bit of refactoring now let's make it a little bit better so we'll just copy this come up here paste it and let's do var hello world yup that'll work and we'll actually get rid of that and instead do it up here var hello world right and i like to do capitals for this hello world so we'll replace this with a capital h and we'll get rid of that and what that's going to do is it's just going to allow us to access this everywhere right so now instead of saying hey if the engine has this singleton right um get this singleton right what we can do is we can just say get rid of all that and then tab that back one fr h hello world is equal to hello right there you go so that just solves a lot of our little issues here and while we're here let's also go ahead and hook up our signal now what we need to do to hook up the signal is we need to say hey let's connect that signal right so hello world dot [Music] connect all right and we need to connect our signal name so we need to find out what our signal name was so what is our signal name well if we go back to our application and we look at it it was hello it looks like yes it was hello so connect hello with comma self with myself quote and now we get to name it so i always like to go on underscore hello and now we can create a function called on underscore on underscore hello like this and we will say hey this is returning a string correct so you can see here it's returning a string so we're going to say s for string we're going to say print s and pass i like putting pass i know you don't have to but i like putting pass at the end of it um so now what we're saying is hey when that signal happens go ahead and um say you know connect this hello [Music] on hello did i spell hello wrong or am i just crazy no i'm just crazy i guess that's not man when you see hello by itself it just looks so weird to me all right um so now it's going to go ahead and print whatever our hello is so we should see hello this worked right but what we need to do is we now need to run our send signal function right so what we'll do is we will go to our button well you know what actually let's just do this we'll go here and we'll say on button up let's go ahead and also do hello world dot send signal all right i don't know why i put a semicolon at the end of that i see sharp days are getting to me you know what we'll put this below so that way just that way we get the prints in order all right so now if we go ahead and run this it should in well i have a dot see it should go ahead and print out hello world and then it should print out hello this worked so let's go ahead and hit our android icon and let's let that run through all right so now that it's on my android device what we can do is we can hit the button so if i hit it hello hello this worked you could see hello this worked and the uh where's the phone where's the class oh there it is hello world so you can see that it actually fully works so that is the basics of how to create your own little android plug-in now you can use this to interface with thing that android has so that's all i have for you guys today um so we went through how to set up a android project we went through how to create a class how to implement a gradle gradle plugin how to implement some gradle dependencies we also talked about how to hook up a android plug-in inside of godot how to emit signals and how to return data back from your android plug-in so that's all i have for you guys if you like this video go ahead and hit that like button if you dislike this video hit that dislike button because i'm here to make content for you now this video was a request from a viewer so if you guys have any videos you guys want me to cover anything you think would be interesting go ahead and let me know in the comments as well i'm always looking for more content to create for you guys so with that being said guys thank you so much for watching and i'll see y'all next time thanks [Music] you
Info
Channel: FinePointCGI
Views: 2,508
Rating: undefined out of 5
Keywords: godot, godot engine, godot android, godot game engine, godot tutorial, android modules, godot engine tutorial, godot engine android tutorial, android, godot compiling for android, godot to android, godot how to android, android godot, advanced godot | compiling godot 4.0 from source, custom modules, godot 3, godot android notifications, godot android tutorial, godot tutorial android, export godot to android, godot android app
Id: a6MDzdqC5a0
Channel Id: undefined
Length: 44min 52sec (2692 seconds)
Published: Mon Mar 15 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.