NEW Advanced Lua Radar Tutorial - Step by Step Guide - Part 1 - Stormworks

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello everyone and welcome back to another stormworks video now with the edition of the weapons dlc coming very soon here in game i thought it'd be a great time to revisit radars here in stormworks now we have done a radar tutorial in the past but it's been a very very basic one so i thought we'll go through and do a full tutorial on how to build a completely advanced lua radar that can actually pick up multiple targets and also is customizable to however you want to build it so we're going to be going from absolutely nothing to completely fully working radar slash sonar whichever way you want to use it and we'll go through every single step hopefully make it really nice and clear for you guys so that you can follow along and you can build your own radars here in game so to get started we're here in the workbench now before we actually start building our advanced lure radar the first thing i want to get out of the way is of course this is not going to be a full-on math tutorial i'm going to show you how to build your own radar using math of course and also lua but uh just bear in mind that some things might not be the best solution uh it's just how i've learned and how i build my radar here in stormx and i will try to explain every single step and why i'm doing that step and actually give you illustrations of every single one of those steps so the first thing we have as you can see on my screen is i have a radar i have a battery and i have a display okay so i've got some very basic components that you will have to have for your own little radar the first thing i'm going to do is i want some way to turn my little screen on now for the purpose of this video i'm just going to have it always on okay so i'm just going to use a constant on block okay and you can just find it over here place that wherever you want i'm just going to place next to my battery now i'm going to connect the electricity to my radar and to my monitor i'm going to keep my on signal over to my display that will turn the display on and we'll use some electricity the next thing of course is we're going to need to create a mic controller the mic controller is what's going to build and run all of this without a mic controller you will not get a radar on the screen so let's go and do that now i have got some pre-built empty mic controllers just for quicker use you can of course instead go up to your mic controller editor and do it over there i'm going to add my empty one by one controller just over here and i'm going to go and click on it once i've clicked on it i'm going to click on edit and you can see it's currently empty so i want to start by increasing the size of it and i also want to start by adding some nodes onto it the first node i want is a video out that's going to go to my display okay so once i've got that on the next thing i want to do is update that and connect my video from my actual little mic controller over to my display so let's start by actually doing the lure now the first thing we're going to have to have on my screen is going to be a circle simple circle on the outside which is going to be the outside of my radar so click on the my controller editor go inside here and go into the logic the first thing you can see is we've got our video out and we're going to add a lua script grab the lure script place it down and connect the video out click on the lua script block and click on edit this brings up a bunch of things on our screen now we don't have to use any of this we could retype it and you can find all these commands or functions over inside of the help panel which is quite useful actually so let's go over a few of these things untick is something that runs every second and it's going to go and get a number value on channel one and it's gonna set the number on channel number one now we don't need this currently so i'm gonna get rid of it the next thing we can see is on draw this is going to draw something to the screen currently i can see it's getting the screen width screen height it's setting the color to green and it's drawing a filled circle at the center of the screen with a radius of 30. now i can reuse some of this logic once again up to you but i'm going to for my tutorial my color of green i'm happy with that so i'm going to leave it the next thing is to get my circle this is drawing a circle filled i want a circle with no filling just an empty outline so i'm going to delete the f this is drawing a circle at the center of the screen so width divided by 2 and height divided by 2 with a radius of 30. i'm going to change that radius to 32 so that should draw a circle touching the borders of my monitor let's go and see if that actually works so i'm going to check for errors always do it get in the habit of doing it and click on done i can now go and update my creation and go and spawn this in i should now have a circle in the middle of my screen and as you can see right there i have a circle it touches the edges of the display because we said that our radius is 32 okay which is half of the screen great the next thing we want is going to be a line okay we want a line on our screen so let's go and do that and this line is what's going to eventually rotate around so just underneath our circle we're going to add a line so go in grab it it's called function or draw line so get that now draw a line once you draw between two areas okay we needed to draw from the center of our screen to the outside of the circle so the center of the screen is width divided by two okay so w divided by two i divided by two and now we need to get the outside of the circle now there's a couple different ways you can do this i'm going to be making use of costine and sin for this so i'm going to say that let's say this is x1 and y1 so x1 and y1 okay so we're going to go and do that so the first one we're going to do is we're going to say that we need to find the center of our screen so with divided by two okay we're going to add that on to the radius of our screen which is 32 okay and we're now going to times that by math.cos and in brackets is where we're going to have our angle of our radar so 0 degrees 60 degrees 90 degrees etc we're going to do the same thing for y we're going to do the height divided by 2 plus the radius which is 32 times by math dot sin okay that's also going to find the point now for the purpose of this video right now i'm going to set where i want it to be at zero degrees or zero radians okay currently at the moment you can see now we're going to draw a line from the center of our screen which is always divided by 2 and height divided by 2 and it's going to draw it until x1 and y1 now x1 and y1 are already defined here and we're getting it to radian 0. now before we go and spawn this and you will notice that i've got my width and my height down here in my on draw which means i actually can't call it up here in my untick unless i've gone and defined it so half of my width is 32 so i'm just going to replace this with 32 and half my height is 32. as i said i could move this down into my own draw or i could actually define my width and my height right here manually once again it's up to you so for example i could delete this and i could go and say that my width is 64 and my height is 64 and i can now go and say width divided by 2 and height divided by two this is up to you if you wanted to adjust accordingly this is quite useful to actually have it up here because that way if you want to have this on let's say a three by three screen i could come here and adjust this and say this is actually 96 or so on okay but once again up to you i can also move this out of ontic and i can move it above and define it top okay once again up to you let's go and spawn this and let's go and see if we get a line across our screen okay spawn it in and let's go and check great we do so we have a line from the center all the way to zero radians now that's an issue we don't want it to be on the right we want it to start at the top okay well how do we get it at the top well as i said earlier we started at the zero radians and we want to bring it back up the circle by 90 degrees so 90 degrees converted into radians is going to be five 1.57 one point five eight don't uh quote me on that math you guys can pull out your calculator and figure that one out so what we're going to do is over here where we have our mass and our costs on our sin we're going to say minus 1.58 and minus 1.58 that should bring this line back up to the top at the 12 to 12 o'clock mark let's go and confirm again bonus in and let's go and check so we should have a circle ah look at that circle with a line right at the 12 clock mark fantastic so that is great i'm really happy with that so far so the next thing of course we want to do is we want to get that line to rotate around our screen there's a couple of different ways you can do this in my previous lua radar tutorial which i think we did about a year and a half two years ago i used a i think a pivot that was constantly rotating around now there is a better way to do it you can actually do it in here using some math so we're going to start off by saying that the angle in which the radar starts at is veto zero okay so i'm just defining that at the top of my screen right now i'm also going to define my speed of my radar so let's use 0.004 or 3. i don't know you guys can play around with this and in a later video i'll show you guys how you can adjust some of these things so it can be completely customizable and you can change it on the fly the speed you can maybe change the size the range etc okay so we're just going and putting these two things and now so telling where to start at and what our speed is the next thing i'm going to actually go and do is i'm going to go and tell the system hey you know what our new angle is going to be our angle plus our speed okay so it's going to add take 0 and it's going to add 0.3 as it adds that it's going to carry on adding it and adding it and adding it and that's how we're going to get a rotation around okay because this gets called every second so every second it's adding 0.0003 that's fine now our radar and also our little screen here works in a couple different ways okay currently on our screen here we have radians okay and we also on our radar we're going to have a value from zero or negative zero point five all the way to point five okay we want to this all to rotate and stuff okay now this angle here okay which currently at the moment is going to be set as 0 and it's going to add 0.03 okay we want that to reset okay every single time it reaches a value otherwise it's just going to keep on going and going going wanted to reset so we're going to say that if this angle if the angle here okay so if angle is either greater or equal to 0.5 then we're going to reset it okay so we're going with then we're going to say now a equals negative 0.5 okay and we're going to end that so that's a simple way of just resetting it so it starts by adding it adding it adding and adding it and then it starts by resetting it resetting it resetting it okay and it just keeps on looping this and looping this and looping this and looping this okay now the next thing we're going to do is we of course want to go and send this a out and that's what's going to tell our radar to go and rotate so we can go and use an output so we can go and grab a simple art number and we're going to send it out on channel one yeah so channel one and we're going to say a can go up please yeah there we go but we want to actually get our line to move which is over here okay we want this line to rotate around our screen so how the heck are we going to do that well we need to go and take this value over here or a in theory which is currently negative 0.5 all the way to 0.5 and we need to convert that into radians how do we do that well let's go and do a little bit of math so we're going to say that let's say our radar radar a is going to be equal to let's start off with a which is our currently our angle so it could be negative 0.5 it could be 0.5 and we need to convert that into a 0 to 1 whole number to do that all i have to do is add 0.5 to it okay so that's converting it to a whole number so 1 so zero to one we now need to go and convert that into degrees so i'm going to go and put it by degrees so 360. for example okay just over here put in brackets once again the next thing i want to do is convert the degrees into radians because that's what i use for my plotting of my circle and my point on my circle so to do that i'm going to do what i've got at the moment and i'm going to times it by math dot hi divided by 180 and that will give me my radians so now i can take this radar a and plus it to whatever i had on my circle earlier so as it rotates it should rotate my line on my screen remember i've also got this angle that's coming out and going to my radar i can update this i can also add an output on my radar now so i can go and add a new node this is going to be radar yaw and it's going to be a number out make sure i've got that connected but hold on i can't connect this composite over into a number so how do we do that well you can use a write okay or read so i can go and read a number from composite and i'm gonna read it on channel one and then i'm gonna send it out update this now i can get this connected to my radar if i wanted to on my yaw you don't have to do that now because there's actually no way of knowing what the way it's facing we can have a look at it in a few seconds so now if we go and spawn in we should see a line that rotates on our screen and it does but hold on it started down here at the bottom we wanted it to start all the way at the top now we did tell it that it originally started here to go back up there but now it's actually starting down here so why is it doing that okay and you'll notice that if we go to this radar and have a look at it it's at 0.045 which should actually be yeah at the bottom okay but it's not it's actually all the way over there so we need some way of bringing this back up i also like that it is resetting itself as you can see it wasn't positive now into negative okay so we need some way of either bringing it back a little bit or pushing it up a little bit okay because at the moment it's not really doing what i wanted to do so we can do a few different things the easiest way here would be to go back into our lunar script and instead of doing a negative 0.58 a 1.58 we're just going to make it a positive check that again spawn in we should now have our line that starts right at the top let's go and check ah there we go so now it starts at the top and then it's going to start rotating around okay let's go and check it there we go that looks much better but uh this is going quite slow how do i make it go quicker well back into the microcontroller and it's again and remember that value i used for speed earlier or i can change it i can remove a zero for example okay let's go and check it now it should be running much quicker in comparison to what it was a few seconds ago aha that looks better doesn't it reset oop go again reset boop go again reset go fantastic great so now on to the last part probably for this video how do we get the targets on our screen and not just one target multiple targets so there's a couple different ways you can do this the first thing we want to do is we want to go on to our controller and tell it that we're reading two things from the radar reading if it's found a target or detected one and what distance that target is at so i'm going to go into design i'm going to add two nodes one node is going to be for the target and one node is going to be for the distance cool listen this is a number in and the target is on off in we can update that and make sure we connect the logic for it so going connected target found up to our radar and the distance avatar radar okay so we've got that now we can go and do some more lure magic and some maths so we're going to go into logic and you can see here we've got our distance and we've got our target now we need to get those two numbers into composite how do we do that well once again with with a composite right okay so we're gonna get constant right number and a composite right on off we're going to take the number into our composite right on off into our composite right and we can see we've got two composite arts but only one in here so we're going to take the bottom one or the top one go into that one so to pass through and then it's going to go through now both of these are in channel 1 that is fine there are two different types of nodes one is a number node and one is an on off node you can override them or overlap them now we can actually go into our lua script and we can edit the lure script the first thing we want to do is read those two things so inside of my function on tick i'm going to add get pool and get number we're going to say that the bull is the target so target and the number is our distance and we're reading it on both channel 1 and channel 1. another thing that we can define now and this is something we'll do in a later video where we'll go and add a feature on to scale with max distance we're going to tell the system that our maximum distance that we want our radar to operate today in this video is going to be 100 meters okay you can change this to whatever you want and i'll show you how to scale this at a later time but right now we're going to say it as 100. so what do we do next well the next thing we need to do is we need to go and find the point of that target on our screen now it works very similar to finding the point of the line exactly the same process we can just copy and paste this whole part exactly the same let's say this is x2 and y2 only difference is instead of using the radius of the screen to find that point we want to use the distance yeah that we're finding it yes we want to use the distance however that doesn't regulate to on-screen pixels distance let's say a thousand meters is not a thousand pixels so we need some way of converting this distance into pixels and to do that we're going to say that our let's say distance pixel is equal to distance divided by the max distance okay so that will give us let's say it's let's say it's 20 meters out of 100 meters that will be 0.2 we now need to convert that into pixels well what's the radius of our screen 32 so we're going to times it by 32 what's going to put it in brackets so it works out the dividing first and then times it by 32 okay so now we've got the location on our screen where we can draw that so i'm going to replace my 32 with my distance pixel so that should give me a location on my screen on where i need to have a dot for example okay now we could go and carry on and just tell the system to draw a dot at x2 and y2 only problem is this only gonna ever draw one and this is where we're going to use some lure magic or what i mean some lure functions and this is going to be tables so we're going to use a table to store these points every single time the system detects a target it's going to store the points of the target in the radar or in the table then it can actually show all those targets in one full rotation then it will reset it every time and i'm going to show you guys how to do that right now so the first thing we're going to need to do is we're going to need to define that we have a table so we can come up to the top and let's do trt targets and we're going to say targets is equal and we're going to tell the system to open a table okay there we go so that's our table that we're opening to start with the next thing we need to do is we need to tell the system to store x2 and y2 every single time it reads that there is a target so if target then add to this table okay and we're going to add our x2 and y2 okay so we can go and write the code that we need so we're going to stop by entering in our table so we can just grab this paste it in here targets equal open brackets here and we're going to say that well tables how they work is they have an index and a value okay so we're going to say that the index okay so let's go over here we're going to say that is going to be x2 and the value is going to be y2 okay so pretty too so let's go here and so that's y2 okay so index is x2 and value is y2 okay and end so what that does now is every single time it detects there's a target it's going to store its location on the map now what we can go and do is we can go and tell the system to go and draw all of those things on the map so we're going to say that for let's say um let's use x3 and y3 okay so x3 and y3 and we're going to say four so for every x3 and y3 in pairs okay which pairs is for it to rotate over the table and what table are we doing it on we're doing on this table over here so that's what this is doing it's reading that table it's going over every single value in that table and once it does that it's going to go over and it's going to go and draw a color first thing i'm going to do i'm actually going to set the screen color to red that way you can draw a red circle and let's just do 200 by 0x0 and let's draw a circle so if i go in here grab draw circle i'm going to do circle filled this time grab it there put it over there and the radius is let's say 2 but where are we drawing it at oh well we're drawing it x3 and y3 okay and then we can end this so guys just a quick review on that every time the system detects as a target it detects what the x and the y values are for that target it stores it in a table with a index of the x and the value of y then in our on draw function we're turning it to go over that entire table and find that value and index i i could literally go over here and call this inv and then go over here and call this inv it doesn't matter what you call this okay in our example it knows that this is the index and this is the value and then every single time it detects something it's going to draw it on the screen here so it's just using tables guys and tables makes the lua so much more powerful let's go and check to see if this is going to work so double check all this it looks good update in here make sure i've got my logic targets and distance yes i do electricity yes we do update this in and let's go and check and there we go detecting every single one of those targets within its distance now this is just going to keep on adding and adding and adding and as you can see if i was to move around let's say if i was to move here at the front it's gonna keep on adding more and more targets on okay which is not what we want we want it to reset every single time it does a full rotation so 360 degrees or six point something something okay so we need to reset or how do we do that well we can go back inside here and we can tell the system to empty that table every single time it's gone through a full rotation well how does it know that we've done a full rotation well we can go and simply go and get it from here if this goes to i think zero positive 0.5 whatever it is that means a full rotation okay so what we're going to go and do now is we're going to say that if this which is the rotation if the radar a is equal to let's say i know 0 or you could do 0.5 as a full rotation then or you could do actually negative 0.5 that could be a good one negative 0.5 then we want you to go and delete all the values from our table well what's our table well we need to go and tell that to go and do it just like we did to go and tell it that every single time it finds a target i want you to add to the table okay so what we want to do now is we're going to go and tell the system that for every i in the table which is pairs targets do and we're going to now say that targets so just like we did earlier to read this yeah just over here we're going to now say i is equal to nil which is nothing okay empty and we're going to end that okay just like that so now what's happening is it's checking to see if we've done a full rotation and it's going and overwriting what we have in our targets and and it's not just doing it for one it's doing it for every single one remember pairs goes and reads over every single value in the table so for every single value in the table it's making it i nil okay and that's how we're getting our reset so let's go and check that again there's a function error at nine so let's just go and double check that where that is i think i forgot to put it end yes i did okay so at the end that's why you always check the arrows guys before you do anything double check that in updates it's spawn it in and let's go and check there we go it's detecting all its parts when it gets to the top it should reset okay so it's not resetting now there can be a couple of reasons why this is not happening the most unlikely reason why this is not happening and the reason why i'm talking about this because it might happen to you is that one it's going too fast the speed is going too fast and it's never actually getting to negative 0.5 it might be getting to 0.499 okay so there's a couple different ways you could do this you could say that if this is less than this value or if it's more than this value or you could just do zero because more than likely it will obviously go over zero okay uh you could do plus a zero point four nine greater than etc it's once again up to you but equals zero usually wolf one will usually work let's go and just double check that to make sure this is going to work update that in again and now let's go and see there we go it went and reset it's going and finding all of its targets going across going across reset caron going carrying going current going karen going and you can change that number depending on when you want it to reset and now you can see it's going cross going down you can change the radius of these circles if you want them to be smaller dots as i said earlier i said it's having a radius of two uh you can go back into the lewis script and change that to radius of like one that will make smaller dots on your screen okay so let's go and check do it there we go nice smaller dots that's me that's that that's i think that that's this stuff etc which is pretty cool isn't it so you can play around with this as i said i'm going to do another fallout video on this guys where we're going to add some shadowing onto this line i'm going to add some settings to turn this radar on and off i'm going to add some settings to increase the distance decrease the distance um change the colors all the things i'm going to add so many more settings onto this definitely keep art for part number two now guys i really hope you have enjoyed this video obviously it does take quite a lot to obviously get everything all lined up here for you guys and obviously explain all these things so i hope you have enjoyed it if you have smash the like button hit the subscribe button and of course keep an eye out for part two where i show you guys how to add some more features until then we will see you soon you
Info
Channel: MrNJersey
Views: 55,223
Rating: undefined out of 5
Keywords: Stormworks, update, news, major, next, island, map, area, snow, mountain, land, friday, improvements, thumbnail, screens, monitors, radio, camera, lua, code, script, laser, distance, shark, megalodon, attack, bug, electric, battery, winch, cable, fuel, space, space engineers, seats, car, racing, pipes, rails, sonar, radar, new, audio, link, BASE, spawn, new game, game, wedges, slope, winches, connector, rope, logic, node
Id: Cosx8WYu0MY
Channel Id: undefined
Length: 31min 14sec (1874 seconds)
Published: Wed Aug 11 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.