GTA 5 (FiveM) Script Development C# - E1 - Intro & Our Bodyguard

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everybody its Ruby as always and welcome to a new little series that I wanted to do so this is gonna be something kind of cool we're gonna be making our own plug-in within GTA 5 and we're gonna be able to basically do whatever we want within the game and we're gonna be able to code it and make that happen the cool thing is is that other than having to buy the game which you don't need to actually write any code everything's free Visual Studio is free 5em is free and obviously you writing the code is free you know if you're your time is yours but so there's one thing we're gonna be using Visual Studio and we're going to be using c-sharp 5 them has multiple ways for you to develop a plug-in you could use Lua or you could even use JavaScript but the JavaScript I don't feel like is flushed out that well and I prefer a more strictly typed language like C sharp and then also just the IDE a Visual Studio makes a lot of things easier it kind of takes a lot of the questions out of things and you actually can kind of stay within the IDE for a lot of stuff which is pretty useful but like I said you don't need GTA 5 you can actually download 5 em and get the libraries in an install Visual Studio and you can code and kind of interact with the code as you want it's just that whenever you'd actually want to test your code you'd kind of be stuck without a way to do that so yeah so anyways other than visual studio and 5m you're also gonna need some kind of text editors so I have Visual Studio code loaded here but really anything would work like Adam or even notepad plus plus just don't use the built in notepad editor in Windows it doesn't handle every file the best way and it kind of forces some different kind of conventions for the file that we don't want always so other than that we kind of just need to get started so I've gone ahead and installed Visual Studio if you've already installed Visual Studio you're going to install it the most important thing to remember is that you install the.net DES desktop development workload package so it'll ask you during install or if you've already installed Visual Studio you can open up visual studio installer and you can follow these steps that I'm doing right now and actually install it yourself it's pretty straightforward so now that we're back at our desktop let's go ahead and create our new project once we're in here we're looking for a certain type of file we're gonna want a class library of the.net framework so it's important that it's this exact one because as you can see there's quite a few different class libraries but the one that we want is the class library dotnet framework for c-sharp you'll see that this one's like Visual Basic and stuff those are the ones that you're not going to want then the first thing is going to ask you for is your actual project name so this is going to depend on what project you're working on first so in our case it's going to be the client 1 the next location that asked you for is where you actually want to store the code by default it's just in your user folder and then the solution name is actually going to be the name of our script so for this first script I think something that'll be I don't I guess kind of easy for us to make first it's kind of like a bodyguard script so let's go ahead and leave that there and then for the framework you're gonna want to make sure you have 4.5.2 selected this is just do two that's the only version that 5em is currently supporting and then you can go ahead and click create and it'll go ahead and set up the initial solution for you so once your loaded into Visual Studio and you have your project loaded it'll be presented with this screen for anybody who's new to Visual Studio it's pretty easy you've got this whole loop this whole left side off over here this is your code editor and then once you actually build stuff an additional box will pop up here at the bottom that will give you your build status over here all to the right is your actual solution this is where all your different projects will be listed there'll be a lot more information here as you build more advanced scripts if for example whenever you have to make a server plug-in as well you'll have some extra options here as well and then down here in the bottom right corner this is where you'll actually interact with some options like the properties for example or different file properties depending on which file you're looking at so once we actually have this created there's a couple things that we have to do to actually set our environment up to work with the Citizen FX which is the 5em API so the first thing we need to actually do is set up a reference for the client library so as you can see we've got all kinds of different things here but we're missing the Citizen FX 1 so we'll right click click add reference it'll pop up in a new window like this and we'll go ahead and click browse and by default this will bring you to your repository folder but up in the bar you're going to want to type % local AppData % and this is just a shortcut to your local AppData folder where 5 mm stores things so here we can see 5 m then the 5 m application data citizen CLR to live mono 4.5 and then you're gonna want to select Citizen FX Quartet DLL you'll notice that there's one called client don't get confused and click that one that's actually not the library we need you're going to want to make sure it's the Citizen FX core dot DLL and it'll be around this size or more or less it depends and we'll go ahead and click actual we don't want to add it here we we can add it here and it's not a problem but sometimes if you for example want to send the solution to somebody else instead of them having to depend on it being in the same spot will actually copy this and then we're gonna want to go to our repos folder which is in by default our users admin and then scroll down here to source repos then our solution name and then client and you can go ahead and paste it in here and then you can go ahead and double click it and it'll add it into your resources click OK once that is at it you'll actually see Citizen FX dot cor pop up over here once you have that open you're gonna want to go to its properties by just clicking on it and then copy local we're gonna want to set this to false because once we actually build our solution we don't need Visual Studio to place this into our output directory this will make a little bit more sense once we actually build our solution so now that this is done the next thing we can actually do is set up the class how we need it to be so to do that but we need to set up the inheritance for it so to do that you're gonna want to reference the loops the class of base script and then Visual Studio gives you these nice red squiggly lines which just basically means an error and you can show potential fixes and you can use this directive using citizen FX core which will then resolve this issue so you'll notice that these are all grayed out the visual studio will grow those out when they're not needed or they're not being used so we can go ahead and remove those for now because we don't actually need them right now so now that we've got our initial inheritance set up there's a few more things that we need to do the first thing we want to do is actually rename this from class 1 so that is just because we want to rename it to main it makes a little bit more sense and it's a lot easier the next thing that will pop up is a option of project rename so this will actually do a full rename of anywhere that the main file is referenced so for example in this text so we'll click yes and it'll automatically do a find and replace for us so now this is done there's one more thing we have to do for the actual properties so we'll go ahead and double click properties and then we'll come up here and then for our assembly name we want to add dotnet to the end that is a 5m requirement then we want to go to build and we want to change this output to let's say dot dot slash output so this will put the built library up one directory and into the output folder this is not so useful right now but it's a little bit more organized and whenever we actually make multiple projects like a server scripts as well it'll put them all into the same location so it's a little easier to work with click advanced and then debugging information we want to change to embedded that'll give you a little bit more information and then you can go ahead and save it and we can come back to our main file so once we're in here we can go ahead and create our our public main so this is just our actual first function that's going to get run as soon as this class is initiated so in here you can do all kinds of things you can register commands event handlers set up configuration values things like that but the thing that we really need to do is for this first one is maybe just register command it's the easiest way of kind of testing your code and it's something that you only need one client for so you don't have to set up a server setup to a trigger command or anything like that so the once we have the base script inheritance setup we can start to use some of the different stuff built in to citizen FX one of those things is the API so we do need to set up an extra use statement for this first so we'll go ahead and do show potential fixes and using citizen FX chord up native we'll go ahead and do that and then the function is named probably as you would expect register command so if we look at the parameters that this one wants it wants a string of command name and then input argument and restricted so the command name let's just set it up as let's say test so this will be so when we type slash test in the game this option will get ran for the input argument we can just pass it a new action of test command and then for restricted let's just let's just set that to false for now it's fine so once again we have another error and for action we need to set up the using system option and then test command well that just doesn't exist yet because this is the command that we need to make so down here below we'll go ahead and create a private static void of test command and basically this means that this element this not something this method is only available to our actual bodyguard script and then it's a void because it doesn't actually return anything so in here we can actually start to write some of our code so one of the things to remember about script development is most of the time it's always going to revolve around your own player so to start with we need to figure out who our player is like our actual player within the game so within Citizen FX all things get stored into classes and you know much like much code does so one of the classes we have available to us is called the ped and any NPC or player character within the game is going to be of the type ped it's a bit different than say an entity or an object or a prop and they have some extra options available to them so and the next thing we need to actually set up is the variable name so this will be us so we'll just name it player so much like API and other things you also have the game variable accessible to you so within here there's a few things that are kind of useful like game timing and stuff like that but the most important thing as you can see here is going to be the player variable so if you look at here you can see that this is actually a type of player so that's actually not the value we want if we go one deeper and we go to the value of character we can see that that is a pet so this is the actual value that we're gonna want for this once this is done we know who our player is so this is useful so that when we spawn our bodyguard we can spawn him relative to our location we don't spawn him across the map and we need to go drive to him/her he needs to run to us or something silly like that so let's go ahead and set up our bodyguard so much like our player he's gonna be a pet as well because he's an NPC and let's name him bodyguard just so we know who's who and then this is gonna be equal to a function value because we need to actually make this guy he doesn't exist yet we're gonna have to make him appear so much like game we also have world accessible to us and this has all kinds of fun functions until like creating vehicles drawing lines getting vehicles but one thing we have accessible to us is create pet so if we take a look at create peds parameters it takes a few ones so the first thing it wants is a model so you have a few options of passing a model to create pet you can do it in the string notation like if I wanted to spawn a paramedic I would do something like this but sometimes these can be kind of hard to find and they're kind of hard to remember so one thing that 5em gives to us is something called up the head hash so if we look within this enum you can see there's all kinds of different values and you can probably guess what like a dolphin is or a doctor but some of these are kind of confusing like east ese and stuff like that they can be a little confusing so there's one thing you can reference and that is the actual ped database so I've got the page loaded over here and as you can see they've actually got a screenshot of every single pedestrian model within the game so there's all kinds of different ones and this list is pretty long I could scroll for a while so with the bodyguard let's just try to find some kind of security guy know if that actually works for me so this kimse guy looks like he's got like some body armor on so I think he fits the bill let's go ahead and go back to visual studio so once we're here we can actually add that value for the name that it gave us right here and that's all you really have to do a little easier to remember and then also it looks a lot better in the code using pet hashes rather than strings the next parameter it wants is a vector3 so a vector3 is an X Y and a Z coordinate so that's wherever you are on the map and then whatever your height is so they could be 0 or 100 or 200 it really depends wherever your player is on the map so for this since we already know what our who our player is we can spawn him relative to our player so what we can do is we can use the player dot position value and that is a vector3 of the players current x y&z coordinate so while we could spawn him right on top of us it would be kind of awkward right so one thing that we can do and that the like different entities and peds have accessible to them is a special value called a forward vector an up Vector and then a right vector so if we reference player position and then we do addition we add some parentheses let's do player dot forward vector and then we'll do x two that'll put him a few steps in front of us and not right on top of us so that we don't have any kind of savant issue so we get another error because if you take a look closely at the return type of this function you'll see that it returns that type of task Pet not up head so basically what that means is that this command we need to await and in order to actually use the await keyword we have to convert our function to an asynchronous task which is that easy sometimes this will cause you some issues depending on if you're using like reference variables but there's a lot of different ways around some stuff so it kind of depends on what your case is so now that we have this guy loaded we're missing one vital step when it comes to actually spawning models within the game the game requires you to load animations and models in the memory before you can actually use them while depending on how this works for example if I ran this command now the first time I would run it it would actually not work if I ran it a second time it would work and it would be a little confusing so the issue with that is because the first time the game loads the model but then it can't do anything because the function doesn't finish in time and then once we load it loaded the second time it's already loaded in the memory and the command will most likely work as you intended so much like world that create pet or API that register command there's a command called API dot request model and this is going to take a unit of a model so we need to typecast it to you int by doing parentheses you meant and then we can reference our same pet hash as before so the Kim SEC a1 guy here and then go ahead and in that line and one thing we want to do is that while this will go ahead and request it we're still immediately jumping into code logic right after so what we want to do is actually interrupt the logic so we'll use a while loop and much like Qwest model we have a function called has model loaded and like before this is we're gonna need to take it as a UN of pet hash and then Kim SEK over one and then we'll finish this out but if you look closely at this logic it's actually the opposite way that we need it so while this has loaded it will do whatever is inside of here so to do that we'll just add an exclamation mark to flip the statement and then within here we can go ahead and use a delay script built into base script to actually sleep this process for a tenth of a second because 100 that's not 100 second so it's actually a hundred milliseconds so basically to go back over we're going to request the model then while the model has not loaded we're going to wait a tenth of a second and then once it has loaded it will continue with the rest of the logic and work as intended so one thing we can do is we can add some debug here so let's say waiting for model to load and that way whenever we press f8 in the game we can actually see a message of how long it took the load and then if you for some reason specify an incorrect model like one that doesn't exist this would loop infinitely so it'd be kind of important to make sure you double check your models and then once you actually test your script to see if you maybe had a spelling mistake or something else like that so now that we have the model loaded we have our guy created we can actually start to interact with him so by default whenever you create this pedestrian if you notice down at the end there's an optional option of heading so it will default to zero which is just a random direction and depending on which way your actual player is facing so what you want to do is maybe giving a look at you so if we look at the body card variable there's all kinds of different things we have accessible to us you know there's a lot of like boolean values like are they an enemy are they driving are they climbing are they being carjacked stuff like that but one thing we have available to us are the task methods so if you look at in here there's all kinds of fun stuff like fleeing from people shooting at people you know going to people so when that we how obviously have available is look at so the parameter this one is looking for is actual in entity so while you may think we need a type of entity actually this will work for our pedestrian as well so we want him to look at our player and the other parameter that it wanted was options so we don't have to worry about that one right now so as our code stands right now he'll spawn in and he'll look at us but we haven't actually given him anything to do which is kind of the big deal so the next thing we want to do is set him as our group member so what that does in the code is actually set team as part of our quote-unquote group in the game and he'll follow us around and then he'll also be our companion so if anybody attacks us they'll attack them and vice versa so once again we'll reference API and then we're going to want to use the function set pet as group member and the first parameter it looks for is int pit which may sound kind of confusing because the a pet is a pet it's not an integer but what this function is actually looking for is the network ID handle of that pedestrian so since we want to set our body guard as the member will reference him here and then we'll use the variable handle attached to him and that is the value that it's looking for is the actual network ID the next thing it wants to know is the group ID that we want to add this bodyguard to so I don't know offhand what the group is and you don't ever want a really hard code these kind of things so once again there's another function that we could use this one is called get paid group index and this one is going to want our player our players network handle as you can see a head so real world reference player dot handle here and then go ahead and in that function so that's all good right now but we've still missing a couple of things for one he's unarmed he doesn't have any weapons and then - he has whatever his default combat ability is so depending on the pet it could be different so let's go ahead and set his combat ability and we'll go ahead and once again we're looking for an int of head so we'll pass the bodyguards handle but then there's this weird in p1 a parameter that it's looking for not very informative not very helpful so this is where the runtime documentation for 5m comes in great great help so what we can actually do is copy this function we can come over to the runtime navigation or a runtime reference I'm sorry and we can paste this function in here and it will pull it up in their actual documentation so it depends on the function honestly how how like popular of a function it is some of these don't have documentation some do so but if we look here someone figured out in the game code once they decrypted it that zero would make them a poor friend one would be average and two would be professional so basically this is how good of a shot they are so I don't know let's go with professional because you know hey I'd kind of like my guys to be a professional so we'll come back to visual studio we'll drop a two on the end and then that is that so the last thing that we're missing is to give him a weapon so as you can probably guess what this function is called give weapon the ped and the first thing it wants again is his handle then the next thing it wants is a weapon hash of unit so much like pet hash we have weapon hash available to us and well let's give him I don't know let's say this assault rifle or something like that I think that'll that'll work pretty good then the next thing we need is the ammo count we'll give him 500 bullets is hidden so falls and then equipped now to true so that way he'll go ahead and equip the weapon so as it stands right now we're actually good he would appear in the game follow us around and be our friend but he doesn't ever really say anything so one thing we can do is use play ambient speech and you can use the generic hi so if you look at this it asks for the speech name is that with I could keep it up on the screen sorry within the game they all have different kinds of speeches built into their models so every character has a generic hi almost every character has like a generic by or generic thanks stuff like that I'll leave the speech information down in the description along with links to the runtime documentation the peddling stuff like that so you can kind of get to them easily so now that we have this done we can go ahead and save our code and we're kind of ready to go ahead and install our script onto our server the next thing you're going to need is you're going to load one more file into your solution four five in to register your plugin for the server you need what's called a resource lua file so i've actually already got one of these prepared on my desktop so here i'm gonna go ahead and go to client right-click add existing item and then much like the Citizen FX core I'm gonna copy this into our repository folder so I have this on our desktop and we'll switch it to all types are all files and we're gonna copy this file and then we're gonna go back to our bodyguard folder and then you can actually drop it in the root folder here because both your server and your client will use the same resource that Lua file so we'll go ahead and add it in here and then we want to click on this file and then copy the output directory we actually want to change to copy always this way it'll always put it into our build directory so now that we have this done you can close that and make sure everything is saved make sure your client is saved and you filled out all the information that we did earlier and then you're actually ready to go ahead and build this script for the first time so let's go ahead and do build build solution you'll see the build status window pop down here at the bottom left and this will tell you any errors that you run across any mistakes that you might have made like maybe an unused variable or if you didn't use like say an asynchronous task correctly it would let you know here but hey we're pretty lucky build one succeeded so we know it's successfully built so depending on wherever you pointed your actual output directory so for example ours was dot dot slash output we'll come back to our project and if you left at default its users your user then source repos then or your solution name and then here we see the output directory here and we have these two files created so these are the files that you actually need to load onto your server if you've never actually made a 5 M server it's pretty straightforward one of the things that you'll need of course is all of these server files so let's go ahead and load those now alright so I've got the documentation loaded up here and as soon as you start you obviously want to follow their instructions install the redistributable and then the first thing you want to do is go ahead and install the artifacts and then you need to go ahead and open up this server data repository so if you come to artifacts it's gonna be a long list and really the key is you just want to grab the latest one if you run into any kind of weird issue or things don't work maybe try to go back up ours and there could be some kind of issue with that build so you want to go ahead and download this once you have it downloaded we're going to need to extract it to a new folder so we'll go ahead and open it up and we'll extract - and you want to leave this in a location where it's kind of easy to get to so we'll create a new FX server folder in our C Drive and we'll extract everything there so we'll go back to our web browser and you're gonna want to go ahead and download the CFX server data you can click clone or download and then download zip it'll download it and then what this is going to do is give you the resources that you need for the server so within that main folder is a resources folder if we come back and let's go let's go ahead and open up a new folder we'll leave that one open up to our output directory and then we'll go to see FX server where we installed our server and we're gonna drag this resources folder into here then go ahead and go into the resources folder and let's go ahead and make our plugin folder here so we'll name this the same as our solution bodyguard and then if we come back to this output directory we can go ahead and copy and paste these files into here alright so now that we've got everything extracted and we've got our bodyguard script created you can go ahead and come back to the root folder FFX server and we need to actually create our server configuration file now so we can just create a new text document and we'll name this server dot CFG and then go ahead and open this up in your text editor I'm gonna open it up with visual studio code then if we come back to the starting our setting up a server page if you scroll all the way down they actually give you a sample configuration so I'm just gonna copy this and I'm gonna paste it into this new file we just created and if we scroll up here you're gonna see all these resources will start by default so we'll go ahead and start our loops start our bodyguard script this is going to be whatever you name the folder within the resources folder the next thing you want to do is change this to en-us or whatever is appropriate for your area you can set your server hostname here like let's say Ruby's dev server and then the next important thing you have to do is set up the Steam Web API key and then your actual 5em license key so they give you the URLs to do that here and this is required or else you might run into like some authentication issues and then the server just won't let you launch if you don't have a license key it's required all right so I've gone ahead and filled out my API keys so we're done now we can go ahead and close the visual studio code and we can go ahead and open up the FX server folder once again and the next thing we actually need to do is we're on our server so to do that you can just make sure you don't have any files selected in the folder and hold down shift and right click off to the side not that high though right click off to the side and you you'll get a open PowerShell window here or command terminal window if you don't have that option you can always just go to start and then actually open up the command terminal here and then type the command CD and then space wherever you actually installed your server so for us that would be c FX server for us so now that we're here and we're actually in the correct directory we can go ahead and run the actual server so to do that the command is dot backslash run that's CMD + exec space backslash server des Eugene everything's got a space between it so once you run this it's going to actually start the server and run your configuration file so the important thing to pay attention to here your to hear is that there's no errors so if we scroll back up to the top we can see that it loaded our resource just fine and we don't really see any errors so that's good news so the next thing we can do is go ahead and load into 5 m and GTA 5 and actually test our code alright so we're actually loaded in the game now so we can go ahead and press T then do slash test and we can see that our command is loading so if we go ahead and do slash test oh and he appears and he said hi to us so that's cool let's go ahead and see if he follows us that would mean he's a part of our group and all that good stuff he's just kind of awkwardly okay you know he's coming he had to think about it for a second but um let's maybe antagonize this guard and that'll kind of test whether or not everything is working right so we might just be able to punch him and get away with it we'll see Oh yep well we killed him I didn't mean to do that but um this guy is not very happy so up and you can see he's actually gonna pop out his uh I don't shoot us though oh Jesus he should fight that cop for us though you know and obviously this would work with like gang members and stuff like that too although I would probably suggest against it oh no let's get another guy help us please oh no the other guy died let's get another one so as you can see you can obviously make as many as you want um it doesn't matter because obviously we're creating a new pedestrian every time so other than our cop problem we're kind of all good to go here but I think this is a really good stopping point for this episode and I want to call that good for today I'd really love to hear from you guys about your questions or comments or maybe what you guys want to make in this series because the world is almost endless with possibilities oh no I don't feel bad for those cops but um really it's down to whatever you want to do I've made a lot of cool plugins so far so it's really what everybody wants but I'm going in that here hopefully you guys enjoyed and I'll see you guys next time
Info
Channel: rubysown
Views: 67,885
Rating: undefined out of 5
Keywords: rubysown, gta 5, grand theft auto, fivem, five m, fivereborn, five, script development, custom script, tutorial, walk through, how to make a plugin, how to create a plugin, gta 5 mod development, visual studio, c#, lua, .net, mod, code, getting started
Id: vaar__zGsvA
Channel Id: undefined
Length: 32min 27sec (1947 seconds)
Published: Sun Sep 29 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.