Scripts / Need guidance on making a HUD that you can toggle!

Author
Message
ZephaHD
8
Years of Service
User Offline
Joined: 19th Mar 2016
Playing:
Posted: 17th Feb 2017 17:02
I think I am pretty close to getting this to work, but something is going wrong when I try to end the function near else apparently? Can anybody spot the error I've made and correct it for me? Would be a great help. Thanks!
GTX 970 Strix, Intel I7 870 (4 cores) -2.9GHz, 16GB RAM, 1920x1080

Attachments

Login to view attachments
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 17th Feb 2017 17:15
Yes you had an else that wasn't connected to an if

Is this what you were trying to do?

Been there, done that, got all the T-Shirts!
PM
ZephaHD
8
Years of Service
User Offline
Joined: 19th Mar 2016
Playing:
Posted: 17th Feb 2017 17:22
Almost, it turns on the HUD by pressing E, but I then wanted to be able to turn it off again using E. So for instance, I could keep the HUD on when I was in a gunfight, then afterwards I could disable it cause it wouldn't be needed, then at any point I could turn it back on again etc.

Thanks for all the really good replies dude, you've helped me a lot!
GTX 970 Strix, Intel I7 870 (4 cores) -2.9GHz, 16GB RAM, 1920x1080
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 17th Feb 2017 18:40
Something like this:

Been there, done that, got all the T-Shirts!
PM
ZephaHD
8
Years of Service
User Offline
Joined: 19th Mar 2016
Playing:
Posted: 17th Feb 2017 18:47 Edited at: 17th Feb 2017 18:53
Thank you so much! I had a rough idea, I created a variable called KeyDown and set it to 0 initially, then when KeyPressE was equal to 1, I updated KeyDown to 1 in an attempt to tell the computer the button needed to be pressed not held down, but that didn't exactly work. Just out of curiosity, does the g behind the variables at the top signify a global variable? And for things like "Destroy(e)", does that "(e)" signify an entity? I started LUA coding today, I'm coming from Python.

Is there any way to add a timer to the Prompt so the messages only stay there for 2-3 seconds?
GTX 970 Strix, Intel I7 870 (4 cores) -2.9GHz, 16GB RAM, 1920x1080
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 17th Feb 2017 19:38
The g_ naming is just a GameGuru thing, the save/load system works better if you name them that way but Lua doen't really care (be aware that Lua is case sensitive btw).

'e' is indeed the entity number assigned by the engine when you place it in the editor.

There is a timed prompt command, look in global.lua and you should find it listed there.
Been there, done that, got all the T-Shirts!
PM
ZephaHD
8
Years of Service
User Offline
Joined: 19th Mar 2016
Playing:
Posted: 17th Feb 2017 20:01
I'll take a look, thank you for all your help.
GTX 970 Strix, Intel I7 870 (4 cores) -2.9GHz, 16GB RAM, 1920x1080
ZephaHD
8
Years of Service
User Offline
Joined: 19th Mar 2016
Playing:
Posted: 17th Feb 2017 20:07
I think PromptDuration is the right command to use here. So I assume it is in milliseconds? Either way, the message is not disappearing whatsoever like I expected it to do.
GTX 970 Strix, Intel I7 870 (4 cores) -2.9GHz, 16GB RAM, 1920x1080
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 17th Feb 2017 20:15
Don't know as I've not used it myself.

You can do simple time stuff using g_Time directly, i.e.

g_time_to_do_something = g_Time + 1000 -- 1 second iow


if g_Time > g_time_to_do_something then

etc

just set the value to math.huge when done to stop it ever triggering again.
Been there, done that, got all the T-Shirts!
PM
ZephaHD
8
Years of Service
User Offline
Joined: 19th Mar 2016
Playing:
Posted: 17th Feb 2017 20:22
What value does g_Time actually hold? Just so I know how I can use this in relation to how many seconds I want to use. And what is "iow"?
GTX 970 Strix, Intel I7 870 (4 cores) -2.9GHz, 16GB RAM, 1920x1080
smallg
Community Leader
19
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 17th Feb 2017 20:40
if you constantly call PromptDuration then naturally it will never disappear as it's always being told to update, you need to use it in a toggle or some such.
and yes it's in milliseconds.

g_Time should hold the amount of time the game has been running (if i remember correctly then it's actually started along with the script loading during the loading process so can be a little above 0 when the game finishes loading.. if that makes sense - i.e. don't rely on a value of 0 on entering the game).

iow = in other words

lua guide for GG
https://steamcommunity.com/sharedfiles/filedetails/?id=398177770
windows 10
i5 @4ghz, 8gb ram, AMD R9 200 series , directx 11
ZephaHD
8
Years of Service
User Offline
Joined: 19th Mar 2016
Playing:
Posted: 17th Feb 2017 20:43
So how do you propose that I display my message for 3 seconds if its going to constantly update, how would I use it in a toggle? Thanks for the quick replies!
GTX 970 Strix, Intel I7 870 (4 cores) -2.9GHz, 16GB RAM, 1920x1080
smallg
Community Leader
19
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 17th Feb 2017 20:50
like this
lua guide for GG
https://steamcommunity.com/sharedfiles/filedetails/?id=398177770
windows 10
i5 @4ghz, 8gb ram, AMD R9 200 series , directx 11
ZephaHD
8
Years of Service
User Offline
Joined: 19th Mar 2016
Playing:
Posted: 17th Feb 2017 20:52
Thank you! Could you please explain what you did to change the code, and how it will work now? It looks quite daunting for somebody of my skill level!
GTX 970 Strix, Intel I7 870 (4 cores) -2.9GHz, 16GB RAM, 1920x1080
smallg
Community Leader
19
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 17th Feb 2017 20:56
like i say, you just need a toggle so that the command is only called once, so take this part
Quote: "if first_run == nil then
PromptDuration("Press E to disable HUD.",3000)
first_run = 1
end"

it checks first_run is nil (which is true if not defined or you can define it to be nil (as i did) then calls the PromptDuration and then i set first_run to not be nil.. that's all.
the others follow the same principle but use the toggle that is there to check for pressing (and releasing) the 'e' key.
lua guide for GG
https://steamcommunity.com/sharedfiles/filedetails/?id=398177770
windows 10
i5 @4ghz, 8gb ram, AMD R9 200 series , directx 11
ZephaHD
8
Years of Service
User Offline
Joined: 19th Mar 2016
Playing:
Posted: 17th Feb 2017 21:03 Edited at: 17th Feb 2017 21:05
So is nil effectively 0? What is the difference between using nil and 1 and 0, or True and False? Also, why is first_run only on the first if statement? Why is first_run a local variable? I didn't think you had to specify that, I thought you could just write: first_run = nil
GTX 970 Strix, Intel I7 870 (4 cores) -2.9GHz, 16GB RAM, 1920x1080
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 17th Feb 2017 21:09
'nil' means something has not been defined yet.

true and false are 'special' in the sense that the interpreter will deal with them how it see fit.

0 and 1 are just numbers, 64 bit double precision floating point numbers, a bit of overkill if you ask me.

'0' and '1' would be more efficient as they will only take a small amount of storage (not necessarily 8 bits though!).
Been there, done that, got all the T-Shirts!
PM
ZephaHD
8
Years of Service
User Offline
Joined: 19th Mar 2016
Playing:
Posted: 17th Feb 2017 21:11 Edited at: 17th Feb 2017 21:11
So in that case, I should be using 'nil' rather than assigning 0 or false? Also, why did the local part have to be included at the beginning?
GTX 970 Strix, Intel I7 870 (4 cores) -2.9GHz, 16GB RAM, 1920x1080
smallg
Community Leader
19
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 17th Feb 2017 21:16 Edited at: 17th Feb 2017 21:19
anything not defined as local is global, you can write "first_run = nil" but that would be a global and it doesn't need to be one if we're only using it in this one script so better to define it as a local.

my first 'if' (if first_run == nil then) is just to show the prompt on first loading/starting the game, the others handle what happens when you actually press e to toggle the hud
lua guide for GG
https://steamcommunity.com/sharedfiles/filedetails/?id=398177770
windows 10
i5 @4ghz, 8gb ram, AMD R9 200 series , directx 11
ZephaHD
8
Years of Service
User Offline
Joined: 19th Mar 2016
Playing:
Posted: 17th Feb 2017 21:19
Which I'm guessing would be unnecessary here, hense why you used local? In that case, why were g_togglehud_Hud_On and g_togglehud_EPressed globals?
GTX 970 Strix, Intel I7 870 (4 cores) -2.9GHz, 16GB RAM, 1920x1080
smallg
Community Leader
19
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 17th Feb 2017 21:22
i didn't write those and merely kept them as they were but it's generally a better idea to use "pressed" toggles as global so that you can also change these in other scripts that might use the same key (in this case, 'e'), so there's less "multiple actions" on 1 press of e.
for 'g_togglehud_Hud_On' it is likely ok to be a local though - unless you wanted another script to know if the hud was showing or not.
lua guide for GG
https://steamcommunity.com/sharedfiles/filedetails/?id=398177770
windows 10
i5 @4ghz, 8gb ram, AMD R9 200 series , directx 11
ZephaHD
8
Years of Service
User Offline
Joined: 19th Mar 2016
Playing:
Posted: 17th Feb 2017 21:24
Right thank you! I've learnt a lot from all this.
GTX 970 Strix, Intel I7 870 (4 cores) -2.9GHz, 16GB RAM, 1920x1080
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 17th Feb 2017 21:26
With Lua a variable can be defined anywhere on the fly but unless you specify it as local it will be added to the global name table, the bigger that gets the slower it is to access. Also Lua cannot free up variable storage if it global because it doesn't know what you have finished with (unless you specifically set it to nil), local variables can be freed up at the end of the function by the garbage collection.

Defining local variables outside of functions doesn't really do anything much, they will still be added to the global name table but access to them may be restricted, it depends on how Lee has set up the environment.

The best use of local variables is to hold virtual pointers into global lists , the "Soldier" variable in the other script is an example of that, not only is it more readable but it is far more efficient that accessing the global list directly. At first glance you may think it is making a copy of the contents of the list but that isn't how Lua operates, it is actually pointing directly at the entry in the list so you could for example say Soldier.health = 50 and that would update the global list. Takes a bit of getting used to I'll admit.

Been there, done that, got all the T-Shirts!
PM
ZephaHD
8
Years of Service
User Offline
Joined: 19th Mar 2016
Playing:
Posted: 17th Feb 2017 21:30
That is a lot to take in, but I think after a few reads I could get fully to grips with this
GTX 970 Strix, Intel I7 870 (4 cores) -2.9GHz, 16GB RAM, 1920x1080

Login to post a reply

Server time is: 2024-12-23 03:40:29
Your offset time is: 2024-12-23 03:40:29