GTA 5 (FiveM) Script Development C# - E2 - Summoning/Creating a Coroner

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everybody this is Ruby as always and welcome to episode 2 of our 5m c-sharp gta5 coding adventure so last time we created some bodyguards and that's pretty cool but not a lot of codes only about 30 lines of code and they only do some kind of basic stuff like protect us and follow us around and stuff like that so I wanted to move on to something a little cooler a little more advanced and we're gonna work on a corner script today so basically this kind of fits into more of like a police are peek on a server and like if there's a dead body you could call the corner EMS or something like that so the reason I chose this is I have some experience making one before for a police RP server that I do some development on and then it just it involves a lot of kind of different API calls and we're gonna do a lot of cool stuff with it by the end of it so it's a good learning experience for a lot of different stuff and like spawning people and spawning vehicles and making them do stuff and then triggering events when they're at locations there's a lot of cool stuff will do so anyways once we're at Visual Studio we can go ahead and create a new project for this one we're gonna create another class library net framework for c-sharp and then go ahead and name the project name client and then we'll name the solution summon and then we'll want to make sure it's dotnet framework 4.5.2 same as last time and then let's go ahead and create the project so now that we have the visual studio project open we're going to do much of the same things that we did last time so let's go ahead and rename class 1 to main and we'll go ahead and click yes to do a full rename of everything and we can go ahead get rid of these imports then for our references let's go ahead and right-click and do add reference and browse and you can always for example copy the Citizen FX core dot DLL from your bodyguard project you can copy it from here or if you didn't do that project you can always go to % local AppData % then locate 5mm the app data citizen CLR to lib mono four point five and then you can grab it and copy it from here and then go back to your repos folder summon client and then we'll paste it here and then we'll go ahead and add it here cool so all done there and we can go ahead and click OK then we're gonna want to click on that again and make sure we don't copy it locally after that's done we can go ahead and create some of the properties and edit them that well as we'll need to so we'll go ahead and rename this to client net and then oops not build event and we'll go to build and do dot slash output and then advanced and we'll change this to embed it and then we can go ahead and save this go back to our main and then we can go ahead and create our public main function in here and in here basically all we're going to really want to do is register our events for each of the different summons that we're gonna want to create you want to kind of keep it organized obviously you could do everything of one file but when it comes to like code maintenance and code readability you're really aren't going to want to separate everything out so for now let's just register all our commands in here so we'll use API doc whoops register command and then we'll have to obviously import that and then of course we're missing our base inheritance for everything so let's go ahead and add that here show potential fixes and go ahead and add that in there and then now we can go ahead and do show potential fixes here and do that for that one so for this let's go ahead and you can name this command whatever you'd like we can name it for example the first one we're gonna do is coroner and the next thing we'll do is a new action and we'll make a new function right here and then false for this last parameter and for new action let's go ahead and using system and then this summon coroner function will have to create here so let's go ahead and go below here and we'll actually create so ma uh it doesn't actually need to be public so let me get private void summon corner because it can be private because we're actually not going to be using it outside of outside of this class so in here we will do an ecologically need to when we come to some in a corner but for now we'll leave this blank because we actually need to create a new class over here so we'll right-click on client and go to add class and you'll want to change this to whatever you're creating so for us that would be a corner and we'll go ahead and add and once you're in here you don't need to set up any kind of inheritance for this you can just leave this as is yeah but within our corner class we need to go ahead and create a function to do the summon event so for this we want to make sure it's public and we'll do a static void of summon except Li don't uh screw up the capitalization like I did and we'll go ahead and create this and then once we're in here we're going to want to start doing all of our different summoning actions so one sec we'll go back to main and let's go ahead and add that here so if we type coroner you can actually see it pop down there and then we can do our dot summon so now when we execute the command slash coroner it'll trigger this summoned coroner function which will then call the function from the main class so now that we're back into here we can start to actually write the code so one of the things we'll need is the player itself so we'll go ahead and do that for game and then player character and show potential fixes using citizen FX core let's go ahead and clean up these long needed ones and now that we're here one thing we can do just so that people know that the coroner is coming is we can show them a notification so to do that you'll want to use screen dot show notification and let's go ahead and show uh potential fixes using the UI and then this wants a message and if it's blinking or not so for this we just want to pass it a string message so we can say the coroner is on his way to you now and we'll say that so after this is done we obviously need to start actually making the event happen so for this event if you kind of think about it you're gonna need maybe like a driver and assistant the van or whatever car they're gonna drive to the scene and then there's a bunch of configuration of like where are they gonna go where are they gonna spawn from so the first thing we want to probably do is create the van so we'll make a comment here for van and there's a couple different ways that you could do this for example you could take the player position and do a forward vector and write vector and put them off away from the player but there's an API command available that will get you the closest road within a certain amount so for example that one would be will do API and then let's go ahead and do the fix for it and then enter here is a function called get int which is basically get this number you'll see this this will always be like you can pass it one two three four 50 75 100 however far you really want them to spawn away from you and you'll see the vehicle node but we want to get the the int closest vehicle node with heading this one here so this one has quite a few parameters but the first three that it wants is the position so this is going to be from this position what is the closest one so for this we want to choose the player's position and then the XY and z coordinates and then the next thing it needs is the int is an int int closest so if you choose one two three four five they will be probably like only right down the road from you which wouldn't be very realistic for this obviously the coroner is never going to be right down the road from you so you want to spawn him quite a bit away so usually something within like set five to a hundred works here pretty well in my opinion the next thing it wants is a reference of a vector3 about position so this is something we really haven't worked with yet with a reference variable which basic basically just means you need to pass this a reference of this variable so for example this wanted it was a vector3 so we'll create a vector3 of will say spawn location and we'll go ahead and put that there but now if we go ahead and add this here and it will still give you an error because if you notice you have you can't pass it and on assigned local variable so for this you can fix this by either for example you could assign it to the position of the player or you could just make it a blank one and just do new vector3 and that's perfectly fine so moving on with the next one the next thing it wants is a ref load of the heading so much like this one we'll create a float of spawn heading and this will be the direction on the road that they're facing and for this we can just make it equal to zero F is fine so we'll pass this variable here as well and then the next thing that it wants is a bunch of unknown variables so these aren't very helpful obviously you're not going to know what these are so let's go reference the actual native documentation so I'll copy this function name and I'll go ahead and paste it here there we go so as we can see there's a quite a bit of documentation about it and basically these guys are just passing some of the different parameters that the game always seems to pass to it so we will go ahead and do that so if we we can do nine three and two and a half and then I know that unknown one should be zero just from my experience so for this let's go ahead and create a new int because this first one is actually a reference variable so we need to create this this guy here and let's go ahead and pass them there and then the next one that it wants was unknown to which was nine then the next one that it wants to zone three which they were saying was a float of three and the last one is a float of two point five and there we go so one thing we forgot is to actually add the ref keywords here we'll just add those in here and then this is now oops a valid function so what this actually returns if we take a look at it is it just returns a pool of true or false so we don't actually have to capture the output of this function we could to determine if it failed or not but this isn't really a function that's going to fail so we don't have to worry too much about it but we'll the way we'll do is it will spawn it will assign not spawn is this spawn location and spawn heading into these variables here so that we can then access them right after this function so what we'll need to do next is maybe create the vehicle because that this location is whatever gonna spawn the coroner's vehicle so one thing we'll need to do is go ahead and create it so we're gonna make a new variable called sieve an entity and we're gonna make this equal to a weight and much like our create bed from last time there's a create vehicle and you can pass this a model which can be like a name of like a string like you could for example pass it whoops except spell it right obviously I like burrito three or what you can do is pass it a vehicle hash of burrito and we want burrito three that's the kind of plain van that's voluntas in GTA 5 the next parameter that it wants is a vector3 of the position so we'll pass this spawn location and then the last optional parameter is the heading so we'll spawn this as spawn heading and go ahead and do that one thing is gonna complain about is because we can only use a weight within an async method so it's pretty simple all you have to do is come up here to your summon function at a sink to it and then the next thing we actually need to do is create this variable for our entire class the reason we want to save this into the class as a class variable and a class property I guess you could say is that we want to use this different integer throughout the script right now we don't really need to use it but later on like say when we want to clean up this corner like when they're not needed anymore we're gonna need to know who they are and we don't want to keep some long-running loop running to wait until then when we can just store it and use it later so for here we'll go ahead and create a private static vehicle because this actually will return a vehicle if we look at this it returns a vehicle so this will need to be a vehicle and we'll call this C van entity here so now this is all good so after that's done the vehicle will be created and it will created at that location so depending on however it spawns it could be anything so something else that we can do with vehicles and we haven't really got a chance to do yet is some different mods so depending on what you want to do you can set like the turbo or the color you set license plates for example so that's what we'll take a look at real quick so sieve an entity and then if you look through there's all different kinds of methods available to you but we're looking for one just called mods and then if you look in here you can see all the different kinds of mods that you can do to it so for us let's take a look at the primary color and then what you want to go ahead and open this up not as the function but as a value and you can pass it the vehicle color hash and then we'll do a metallic black for example would be good go ahead in that line there and sieve an entity and then we can also for example set it to like a as8 exempt plate since this would be kind of like a government plate plated vehicle so you can set the license plate as well and you can set this to anything you want like si si here si corner if you want it to look a little more realistic you can always use a dollar sign before the variable and then assign it maybe like a random number so we'll do this and then you can open up a curly bracket and in here you can put any kind of variable for us I want to use the random library so we haven't added this yet but let's go ahead and type it out and then we want to do a next of ten so for random this is gonna be something else that will probably reuse throughout the script quite a few times so instead of making a new random instance and every single function will just create a global one that the entire class can use so for this one we're also gonna assign read-only that's something we haven't seen before that's because this random one we don't actually want to ever assign anything to it we only ever want to get values from it so it kind of prevents you from doing anything kind of funky like that so we'll go ahead and import random like it said and then we can close this off at the end so random dot next you can pass it this and this is just the max value so you could set this to like this and it would generate you know I but I really doubt there's that many corners in San Andreas so 10 is a pretty good number so then now that we've set the license plate you can also set the style of the license plate so let's take a look at this and license plate style and in here you can do license plate style it's got one available for you and the one we're gonna want is blue on white three that's the SI exemple one so after that's done we've got the vehicle created we've got it black but there's nobody in it and we also I'm never going to know where it's at so one thing we could do and it kind of helped helps people know where the different entities are within the world is we can create a blip for him so we'll create another line here for van blip and we're gonna create a new variable for see van blip and this is going to be equal to API and then we're gonna add blip for entity so this one just takes an INT of an entity so for this we're gonna want to pass our sieve an entity handle and then let's go ahead and create this variable up here so for this one we're gonna want to create a private static int of sieve and blip because this just returns an integer in the end so anyways now that we have the blip created if you just left it like this it would be a red blip and it would say enemy so the corners not really our enemies so there's some extra configuration for the blip that we'll need to do so one of the ones we'll want to do is maybe color the blip differently so we can set blip color and make sure you do the British spelling for color and then it wants the blip so we'll call seve and blip which is our stored variable for it and then the end of color so I don't really know these oft hands so we'll copy this function again and let's go to the native documentation and in here they have all the different colors listed so this dark grey looks pretty good let's go with that one let's go back to visual studio and let's set this to 40 and in the line so now we have the blip it's colored but it will still say enemy on it so let's go ahead and there's another one we'll do begin text command set blip named that's a that's a mouthful I'll tell you that much so in here this is a little confusing but you just want to set this to string then this next one is add text component string and then this is gonna be whatever you want the blip to say so it could be corner or you know enemy or bodyguard like if you wanted to go back and add blips to the body guards for this we'll just set it to corner and then the last thing we need for the blip is to end this command so it knows we're done with it set a little name and then it'll go ahead and actually assign this text component to this actual blip so in here we'll call flip again and end that off so now we have the van we've got the blip created so next let's go ahead and create the driver so we'll go ahead and make a new section for driver and what we'll go and want to go ahead and do is load the model so what we can do is we can go ahead and create this new load function and we'll go ahead and create a public static async task rule of load model and UN model and within here we can go ahead and start to do our different requests so let's go ahead and import system threading task and within here we can go ahead and call API dot request model and you can do that and then we'll do kind of the same thing we did this last time so while API has model loaded and then call a model again go ahead and do this then debug write line waiting for model model to load and then we'll await base script delay 100 milliseconds so that until it actually loads this model it will wait every 100 milliseconds and then we'll just return true and this is all good so something else you could do also is detective this is a valid model if you wanted so if API and then is model in CD image probably a little bit of some legacy code function there because uh well I guess GTA 5 probably came out on CDs too but I guess everybody's used to digital downloads these days so you could always write debug write line in valid invalid model model whoops we got to add our dollar sign at the beginning to let us inject a variable into the string so invalid model model was supplied to load model for example I gotta get a new keyboard mouse shift key and then you want to return false because you don't actually want it to do it because if you if it's an invalid model that doesn't exist this code will just load infinitely so you always want to kind of be careful with that kind of stuff so one thing we can do actually is you usually won't run into loading issues with vehicles but it can happen if it's not like a common one is we can go ahead and throw this before our our van so we'll go ahead and do you int vehicle hash burrito three and this will make sure that the actual vehicle gets loaded before so in here we'll do the same thing before we spawn the the actual pedestrian so we'll do UN again ped hash and then I've already kind of done some research on a good pedestrian that would look good for here so the there's a doctor for example that works really well for this so we'll go ahead and load Model UN Pedder a12 s mmm and you could also always just do everything in string notations but I always find that doing everything through the different hashes always kind of makes everything look better in my opinion so now that we have the pedestrian loaded we can go ahead and actually creating so we'll go ahead and create a new variable again and we'll call him C van ped one for example let's go ahead and then do our weight and much like we did last one we'll do create pet and in here you can pass the fetish of doctor and then the next thing that it wants is the position where it should spawn them so let's just set this to the spawn location of where it's going to spawn the van and we'll go ahead and create this new variable up here so we'll create a private static head of C van pet one and then just to save us some time because we know we're going to have a co-driver let's go ahead and create C van pet two so down here now that we have him created we have him stored we for one need to actually put him into the vehicle and we will then also need to eventually give him a location to drive to since he's the driver so there is a function available if you look here you can actually set into vehicle from the pet himself there's also of course other native functions that you can do to do this for you so the things that it wants is a vehicle of vehicles so we have that stored luckily with Steve an entity and then it wants the vehicle seat so we can do vehicle seat and then driver for example for this guy so now that we actually have him set into the vehicle we can do a couple other things for example one thing you can do is so that he can't be targeted by the player this kind of prevents maybe somebody from making him angry but there's a lot of other things that you can do to kind of prevent that too so the next guy will create is the passenger so will do a weight load model and then do ru int and then ped hash and then there's a scientist model that works pretty well for this then C van tattoo will go ahead and create the second ped finish line test and then we can spawn him at the same location and then C van pet to set him into the vehicle of C van entity and vehicle seat passenger because he's going to be the passenger in the vehicle and then we can also always set him to can be targeted false as well and then they're all set so other than that so they now have a spawn location we've spawned the actual pedestrians in the vehicle they're in the vehicle now they need to know where to actually drive to so you could always make them just drive to the player location if you wanted to but you'll kind of run into some issues like what if the player is off the road what they are behind some cars so typically what you'll want to do is you can use a function very similar to the one that we use to spawn the vehicle but instead of int we can just get closest vehicle node so what we go ahead and do is we'll make a new one for some configuration and let's go ahead and call API dot get closest vehicle node with a heading so let's see we want to get this first one right here because we don't want to do pants you could always do that one again and just call 0 or 1 or whatever you really want to but this may be a little bit of a cleaner way to do it expect except spelled position right player dot position Y player position Z and then once again we have to make up some reference variables so let's go ahead and create this new line here and we will do a vector3 of let's say a target location and we'll go ahead and make it a new vector3 all right so it wants target location and the next thing it wants is another heading so let's do float of target and heading equals zero F and then let's not forget our rough keywords this time and then what is the next variable that it wants no type so not too sure what that could mean there there's a couple different in that you can pass to it but whenever you're not sure I always just reference this because most of the time people kind of have some feedback on how everything works so see this one has the node type and then some other options to it so p6 and p7 for example so this guy right here says p6 is always 3 p7 is always zero and then let's see there's all the different road types here so we can probably just choose one and everything will be fine so let's go back to visual studio we come out here we'll do one for that node type and then the it said three and zero as the other parameters so we'll go ahead and save those there so after this is done now our target location will be filled out so one thing we can go ahead and do is we can access the tasks option of the driver so that's C van pet wine tasks and then the one we're looking for is drive two so drive two takes a vehicle so this also could mean that you could also spawn him outside and he could get into it and drive it away I've used this before for like an event where you have a pedestrian outside of vehicle and when you drive up close to him you want him to run away well this is something else you could do it to him you could use with them and they would then run to the vehicle you know and drop it away so the vehicle here is of course the van that they're already in so they don't to run anywhere and then they want a vector3 for the target so we'll pass target location then the radius so this is how far away from that position is okay for them to stop so we can pass it like ten F here that'll be about ten meters and then the speed this is how fast they'll drive there if you set it to one F there will be like a turtle but usually around like ten or something like that it's like a normal speed I think and then twenty is kind of like a quicker speed so then there's driving style so this is kind of a it looks very different at first but it's basically a bunch of flags that you can set so I'll link a website down in the in the description where you can actually generate different driving styles and like for example if you don't want them to wait at red lights or you do want them to rate wait at red lights you can configure all that so one that I've already generated is two six two nine seven two this one seems to work pretty well and I think I've even seen other people use this one in code before so after we have them all set to drive - the last thing we'll actually need to do is trigger some kind of value in our code that we know the events have been spawned it's not very useful right now but for the next part of this video where we're gonna make them actually do stuff once they're at a location we're gonna need to know if the event is spawned you know if they're out of the vehicle if they're in the middle of doing an action stuff like that so we'll go ahead and set events fall to true and then we'll need to actually create this variable up here so this one you can do a private static bool and save it here and that is pretty much it so let's just go over the code and we have our VR van spawned we are doing our model loading for everybody we have our van created we have our van modified then we have our blip created and and named and colored appropriately then we're creating our driver setting him into the vehicle then we have our passenger setting him into the vehicle then we're coming up with where we want the driver to drive to and then we're assigning the task of Drive to to the driver to actually go to that location so now that this is done you can actually go ahead and run this code so let's go ahead and save everything and we've got everything filled out so we'll come back to here we'll save this and let's go ahead and save this and the next thing we'll need to do is import our resource to a file again so let's go ahead and do add existing item and then let's come to the desktop or wherever you have it saved and set this to all files let's go ahead and copy this guy and let's go back to our summon repo and we'll paste him here and let's go ahead and add him and then output to our copy to output directory and we'll do copy always and we should be good so now we can go ahead and build let's see if there's any problems with our code hopefully not it's always the most uh kind of nerve-wracking part but uh it looks like we're good other than it's complaining that this value is never used and that's just because we're not using any kind of main function I guess it doesn't realize when we are using it but we're not and it's its opinion so now that we have this generated we can go ahead and create our new folder in our server for this so you'll want to navigate to wherever you have FX server installed so on here i have it in c FX server resources and then let's go ahead and create a new folder and we'll name this summon and then let's go ahead and create a new another Explorer window and we'll go to our repos folder and if you don't know where that's at it would be C users admin and then you'll see source repos and then here would be repos so we'll do summon output and then you can control C control V and if you have it named client Net DLL this resource that will work fine if you don't you'll have to edit it and set it to whatever you have your actual client scripts set as so now that that's done let's go ahead and start the server and let's get into game and try it out alright so now that we're in game we can go ahead and test everything out so I'll go ahead and open up the chat menu type slash corner and we see our message a coroner is on his way to you now and we can see him he kind of spawn kind of far away maybe a tease a little too far but you can actually see him coming down the road you can see our name applied to him and everything coroner he should be coming up right now there he is and then he should be stopping yep somewhere right here so pretty close to the player and then kind of the part we're missing now is what happens after they get here so I don't want to make these videos too terribly long like I believe if we had to do the rest of the logic would be like an hour and a half video so I'm gonna split this one up into two parts and hopefully that's all and on the next video will actually cover all the logic after they get to you so them finding bodies and doing all the different kind of different kinds of logic that we'll have to work on for that so hopefully you guys enjoyed this video sorry it's got to be a two-parter but I just don't want it to be too much too much of a knowledge overload in one video so let me know what you I thought let me know what y'all think y'all would like to see because I'm very open to making some of the plugins that you guys think about I know somebody's already suggested one which was a really good idea and it'll be really detailed video for probably later on in the series because it's gonna kind of be a little harder at the beginning so anyways I blabbered on long enough I really hope you guys have enjoyed and I'll see you next time bye
Info
Channel: rubysown
Views: 16,428
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: 7_6NwBmWsgI
Channel Id: undefined
Length: 35min 37sec (2137 seconds)
Published: Sun Nov 24 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.