Как сделать hud для garry s mod
Мы рады всем гостям которые любят провести свое время в онлайн или одиночных играх. У вас случилась беда, и вы не знаете как решить ту или иную ошибку? При заходе в любимую игрушку детства у вас появляется черный экран и вы уже не можете насладится теми ощущениями что испытывали раньше? Бывает такое что при попытке поиграть, любимая сага игры просто не запускается, тогда уже становится очень обидно. Не волнуйтесь, даже если вы думаете что это не поправимо, у нас вам помогут с этим справится.
Спецаильно для наших уважаемых посетителей, мы собираем лучшие советы и рекомендации которые помогут справится с той или иной проблемой. Как мы уже сказали раньше, даже если вы столкнулись с самыми редкими вылетами, даже если при запуске игры процесс находится в задачах но ничего не происходит, мы все равно найдем оптимальное решение которое подойдет каждому. Ах да, мы забыли упомянуть о самом главном, часто любители поиграть в 3D игры сталкиваются с такой ситуацией когда при попытке зайти в игровое приложение, у них просто происходит вылет или крэш на рабочий стол - с этим мы тоже знаем как боротся.
Вы думали что мы умеем только решать различные технические задачи? Нет это не правда, мы можем помочь каждому взрослому парню который решил найти или вспомнить название игры в которую он так любил поиграть в детстве, но забыл как она называется. Ведь наше игровое сообщество явлется прямой системой вопрос-ответ, вам нужно лишь обратится, и мы сразу же вам дадим ответ.
LUA:Hud Tutorial
Thanks for viewing this tutorial, today we will be creating a DarkRP HUD. Firstly, this is a TUTORIAL and you are meant to learn from it. It's not supposed to be actually used in a practical example no matter how tempting it may be.
This shows you where to start in creating your own hud, and show's a mighty fine example.
Preface
This tutorial is about making heads-up-displays, or HUDs in short. I've seen a lot of you young scripters and even some of the old ones making huds, but only a few of you make them right.
This tutorial is going to show you how to make a small and well laid out health and armor display. Now, we are going to approach this by not slapping everything into one HUDPaint event. We are going to do it in a modular way - by making a few functions for drawing similar panels and then use those functions to draw the hud itself.
Getting started
To make the code tidy, we'll be putting every variable and function in a table, so lets start off with making the table.
I'm going to assume that you know about gmod's file structure and where everything goes.
The bar
Next, we'll make a function for drawing a bar.
X, Y, W and H are fairly obvious, but if you don't know what they are - X is the horizontal coordinate on the screen, Y is the vertical coordinate on the screen, W is the width, H is the height. Next we have colors. Colors for us will be a table with four elements - border, background, fill, shade. I will explain these later. And the last argument - value - is going to indicate how far the bar is gone from 0 width to W width. Lets go and fill the function up!
What we're doing here is basically just putting some function calls into one function and doing some small math. But wait, did you notice? There's a small function called "clr" that you have never heard of before. That's right.
This function just takes a color object and splits it up into multiple returns. This is specifically for the "surface" functions, since none of them accept a color object.
To the hud, I used the following setup.
And the outcome looks like this.
This is just an example of what the function does, we won't be using that color setup in the end.
The box
We don't want to leave our bars just hanging there, they need a background! Don't fear, here is how we draw the background.
It is as simple as that. X, Y, W and H I already explained. Colors is almost the same, except that it only needs two fields - border and background.
Using the following code.
It looks like this.
But wait.. it looks similar to the one we already made. Yes, it does. I thought you said something about optimised and well laid out stuff.. Yeah, I did, I thought you would figure that out yourself, but I guess I'll have to do it myself.
Now our PaintBar function uses our new PaintPanel function to draw some common parts. Works exactly the same, except it now has less code. And if we wanted to change how the background looks( maybe add an image or something ), we wouldn't have to change it in two different places.
The message
We have the bars and stuff done, now it's time for text. We already have some functions in the draw library that handle text, but for the sake of the tutorial we'll make our own.
The PaintText function draws a piece of text with a small shadow. X and Y I already explained, text is the text we want to draw, font is the font and colors is again a table of colors, except with different fields - shadow - color of the text shadow, text - color of the text itself.
Using the following code.
The output looks like this.
Putting it all together
So, we went through how each component works, added a convenience function and made one component interact with the other. Now it's time to put everything together, so it looks like this.
Here is the code you should have fiddled together by now.
After tinkering with the hud, I decided to add this function as well.
It basically just gets width and height of the given text using the given font.
The hud
This is where we start making our hud.
Now, the vars table is where we will keep our sizes and offsets, and colors is where we'll keep our colors. We are going to make this dependant on the screen resolution, so there won't be any direct sizes stored.
This is how it looks like.
Font is the font we'll be using for our text, padding will be the inner space between bars and text and the border, marging is the space between the hud element and the screen sides, text_spacing is the space between text and its bar, bar_spacing is the space between a bar and the next text line, bar_height is the height of a bar and width is the width of the element relative to the screen width.
Here are the colors. The bar colors aren't really as good as I wanted them to be, but this is just an example, so the colors are up to you.
And finally here is our HUDPaint event.
I hope this tutorial helped you understand how to make a proper hud. There are some parts that could have been done better, but I'll leave that up to you.
PS: I just realized that some of my comments aren't explaining stuff correctly, but the coding is pretty straight-forward and doesn't need much explanation.
Preface
This tutorial is about making heads-up-displays, or HUDs in short. I've seen a lot of you young scripters and even some of the old ones making huds, but only a few of you make them right.
This tutorial is going to show you how to make a small and well laid out health and armor display. Now, we are going to approach this by not slapping everything into one HUDPaint event. We are going to do it in a modular way - by making a few functions for drawing similar panels and then use those functions to draw the hud itself.
Workspace
Now you've got the custom icons your ready to go! First let's set up our workspace, we need to get rid of that stupid, old, deprecated hud that some godforsaken person drew up (No offence, but it's terrible).
The file's we need to edit are simply cl_hud.lua, located usually in darkrp/gamemode/cl_hud.lua. If you do not have this file, you need to update to the latest DarkRP SVN as the hud system is totally different. You will end up getting considerably confused.
Getting started
To make the code tidy, we'll be putting every variable and function in a table, so lets start off with making the table.
I'm going to assume that you know about gmod's file structure and where everything goes.
The bar
Next, we'll make a function for drawing a bar.
X, Y, W and H are fairly obvious, but if you don't know what they are - X is the horizontal coordinate on the screen, Y is the vertical coordinate on the screen, W is the width, H is the height. Next we have colors. Colors for us will be a table with four elements - border, background, fill, shade. I will explain these later. And the last argument - value - is going to indicate how far the bar is gone from 0 width to W width. Lets go and fill the function up!
What we're doing here is basically just putting some function calls into one function and doing some small math. But wait, did you notice? There's a small function called "clr" that you have never heard of before. That's right.
This function just takes a color object and splits it up into multiple returns. This is specifically for the "surface" functions, since none of them accept a color object.
To the hud, I used the following setup.
And the outcome looks like this.
This is just an example of what the function does, we won't be using that color setup in the end.
The box
We don't want to leave our bars just hanging there, they need a background! Don't fear, here is how we draw the background.
It is as simple as that. X, Y, W and H I already explained. Colors is almost the same, except that it only needs two fields - border and background.
Using the following code.
It looks like this.
But wait.. it looks similar to the one we already made. Yes, it does. I thought you said something about optimised and well laid out stuff.. Yeah, I did, I thought you would figure that out yourself, but I guess I'll have to do it myself.
Now our PaintBar function uses our new PaintPanel function to draw some common parts. Works exactly the same, except it now has less code. And if we wanted to change how the background looks( maybe add an image or something ), we wouldn't have to change it in two different places.
The message
We have the bars and stuff done, now it's time for text. We already have some functions in the draw library that handle text, but for the sake of the tutorial we'll make our own.
The PaintText function draws a piece of text with a small shadow. X and Y I already explained, text is the text we want to draw, font is the font and colors is again a table of colors, except with different fields - shadow - color of the text shadow, text - color of the text itself.
Using the following code.
The output looks like this.
Putting it all together
So, we went through how each component works, added a convenience function and made one component interact with the other. Now it's time to put everything together, so it looks like this.
Here is the code you should have fiddled together by now.
After tinkering with the hud, I decided to add this function as well.
It basically just gets width and height of the given text using the given font.
Files
Once you have downloaded the file, look for money.vtf, money.vmt, money_add.vtf and money_add.vmt. Copy them over to your garrysmod/garrysmod/materials/gui/silkicons folder. If it does not exist, create it and place your icons in.
Coding
Removing the Shitty Hud
We need to go to cl_hud.lua now and find the area containing the default hud, which should be located near:
Inside this area, cut out all these lines:
Congratulations, you've freed yourself from the oppression of the default HUD.
Paving the way
Now we need to setup our hud and make sure it's called, under the whole:
We've added our function, but it won't get called on it's own, Near:
So Far
DoActualHUD() MUST BE ABOVE the DrawHUD() function, or you will get an error along these lines [DarkRP\gamemode\cl_hud.lua:212] attempt to call global 'DoActualHUD' (a nil value)(Hook: HUDPaint)
We need to calculate our money and salary in advance, so let's add that to our DoActualHUD() function.
Now we can move on to drawing
Drawing on our canvas
Now it's time to start drawing, garrysmod draw's the HUD every frame. Which is measured by your FPS, (frames per seccond). Since garrysmod redraws every frame. If you don't keep drawing something, it will simply disappear. This moves towards our advantage, as we don't have to mess around and worrying about our HUD's not drawing correctly.
First of all, let's get a background going.
Why not change the colour?
A sudden buzz of code their, basicly, we're drawing a rounded box with a 5 radius at the bottom left part of the screen. You have to rely on all resolutions, so never make anything bigger than the reasonable 800x600. Another alternative is percentage scaling, so you're hud will scale it's size based on the resolution.
But we're not occupying much space, so we don't need to.
If you play in singleplayer now, you will see a pretty empty HUD glaring at you with nothing on it. Now it's time to draw some icons!
This makes sure we draw with no specific colour or transparency. Our HUD does not have an alpha (transparency) so we don't specify it.
Now we can draw the icons!
As the icons get further ahead, I move closer to the bottom of the screen. Hence the ScrH() - 80 and so on. If you play, you will find a nice, iconed hud in all it's greatness!
Information
Now we need to display some information, this part is just as easy as the others.
Like the background and the images, that's alot to take in. Basicly, we're draw the persons name, with a certain font. At the bottom left corner of the screen and make it white. We also add the correct aligning.
Now we have add the rest!
Result
The hud
This is where we start making our hud.
Now, the vars table is where we will keep our sizes and offsets, and colors is where we'll keep our colors. We are going to make this dependant on the screen resolution, so there won't be any direct sizes stored.
This is how it looks like.
Font is the font we'll be using for our text, padding will be the inner space between bars and text and the border, marging is the space between the hud element and the screen sides, text_spacing is the space between text and its bar, bar_spacing is the space between a bar and the next text line, bar_height is the height of a bar and width is the width of the element relative to the screen width.
Here are the colors. The bar colors aren't really as good as I wanted them to be, but this is just an example, so the colors are up to you.
And finally here is our HUDPaint event.
I hope this tutorial helped you understand how to make a proper hud. There are some parts that could have been done better, but I'll leave that up to you.
PS: I just realized that some of my comments aren't explaining stuff correctly, but the coding is pretty straight-forward and doesn't need much explanation.
Extension & Links
How about making the money show like the default DarkRP Hud, with $5,000 instead of $5000.
Читайте также: