Scripts / A couple of questions about scripting

Author
Message
Belidos
3D Media Maker
8
Years of Service
User Offline
Joined: 23rd Nov 2015
Playing: The Game
Posted: 20th Mar 2018 09:25 Edited at: 20th Mar 2018 09:27
Hi guys, i normally don't ask outright for help writing a script, but this time it involves timers and i have never been able to work out how to incorporate them in scripts, even with detailed explanations, it just evades me, so i can't provide an example of what i have tried, because to be frank timers scare me and i haven't even tried yet, so i'm hoping someone can help me out with this one.

I have an idea for my work in progress, but for it to work i need a script that shows a clock on the screen that counts down from 10 minutes, then switches a global variable from 0 to 1 when it gets to zero. Can anybody help me out with this? because it's eluding me, i have trouble enough getting timers to count up, down is a mystery lol.

Second question, i don't need any scripting etc. just need to know if a command exists, i have a script that shows a menu with two buttons when the above global variable is triggered, what i want the two buttons to do is: button 1 - restart the level, and button 2 - leave the game and go back to the main game menu. Button 1 i can do by butchering the commands from the winzone and using the ifused field, that's easy, but for button 2 i don't know how to get it to go back to the main game menu, id there a LUA command to do this? or is it hard coded into the escape key menu? If it is a hard coded thing, can anyone suggest an alternative, as i'd like them to be able to choose whether they try again or quit when they fail.

Primary Desktop:
i7 7700,k NV1070 8GB, 16GB 3200mhz memory, 1x 2TB Hybrid, Win10.

Secondary Desktop:
i5 4760k, NV960 2GB, 16GB 2333mhz memory, 1x 2TB Hybrid, Win10.

Laptop:
i3, Intel 4000 series graphics, 6GB memory, 1x 500gb HDD, Win8.1.
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 20th Mar 2018 09:47 Edited at: 21st Mar 2018 19:55
Well the first bit is simple enough, g_Time is always counting up in milliseconds so every time 1000 milliseconds passes subtract 1 from your 10 minute count and display the result. When the result reaches 0 you can set your global flag.

This link should help with displaying the time:

http://www.lua.org/pil/22.1.html

Here is it's usage: (this was for HB's survival inventory thing)



I'll have to investigate the return to menu thingy. Gotta go earn some dosh first though.
Been there, done that, got all the T-Shirts!
PM
Belidos
3D Media Maker
8
Years of Service
User Offline
Joined: 23rd Nov 2015
Playing: The Game
Posted: 20th Mar 2018 10:23 Edited at: 20th Mar 2018 10:30
That's the problem, i can kind of understand how to display a timer. But making it run backwards and reach zero, and again parsing that zero to trigger i variable, i haven't a clue.

Also the link doesn't help me, because A- it's a dead website that won't display, and B- i've been on that site before when it was working, and the way it explains stuff is completely gibberish to me (learning difficulties, things have to be explained in a very ordered way for me or i can't grasp them, which is why i find it very hard to write scripts "outside the box" if you know wheat i mean). Which is why for the first (and hopefully only) time i was actually asking for a script, normally i can work through it, but this time it's just completely beyond me.

Primary Desktop:
i7 7700,k NV1070 8GB, 16GB 3200mhz memory, 1x 2TB Hybrid, Win10.

Secondary Desktop:
i5 4760k, NV960 2GB, 16GB 2333mhz memory, 1x 2TB Hybrid, Win10.

Laptop:
i3, Intel 4000 series graphics, 6GB memory, 1x 500gb HDD, Win8.1.
Belidos
3D Media Maker
8
Years of Service
User Offline
Joined: 23rd Nov 2015
Playing: The Game
Posted: 20th Mar 2018 10:49 Edited at: 20th Mar 2018 10:55
OK, been looking at that, and finally got on to the web page you linked (your link was broken for some reason), and it just plain doesn't help in any way, apart form none of it making any sense to me any way lol, i also think you missed the point of what i wanted.

I don't need to display time and date, i need to have it display 10:00 on the screen, then count down 09:59, 09:58, 09:57 etc. until it reaches 00:00 and then switch a variable from 0 to 1. Any ideas?

I've tried searching for info on timers and countdowns in LUA, but every example i've seen has been specific to other projects and includes LUA commands that i don't think we have for example coroutine.create(timer) etc. so i am getting more clueless and frustrated the more i look for the solution.

Primary Desktop:
i7 7700,k NV1070 8GB, 16GB 3200mhz memory, 1x 2TB Hybrid, Win10.

Secondary Desktop:
i5 4760k, NV960 2GB, 16GB 2333mhz memory, 1x 2TB Hybrid, Win10.

Laptop:
i3, Intel 4000 series graphics, 6GB memory, 1x 500gb HDD, Win8.1.
Belidos
3D Media Maker
8
Years of Service
User Offline
Joined: 23rd Nov 2015
Playing: The Game
Posted: 20th Mar 2018 11:26 Edited at: 20th Mar 2018 16:28
Right, i have been doing some reading and i think i'm getting somewhere, although i am developing a huge pain behind my eyes lol



I'm at work so can't test it yet, but does anyone see anything wrong with the above script?

Primary Desktop:
i7 7700,k NV1070 8GB, 16GB 3200mhz memory, 1x 2TB Hybrid, Win10.

Secondary Desktop:
i5 4760k, NV960 2GB, 16GB 2333mhz memory, 1x 2TB Hybrid, Win10.

Laptop:
i3, Intel 4000 series graphics, 6GB memory, 1x 500gb HDD, Win8.1.
J_C
16
Years of Service
User Offline
Joined: 9th Nov 2007
Location:
Posted: 20th Mar 2018 13:57
Hi Belidos good progress..

there is an error in this line .. can you spot it ..?

Text(5,5,1,"Time:"..minute..":"..second..)

also there is something missing from the last if statement

When fixed... and run then the Timer appears and counts down..
but.. it looks strange always starting from the 60 seconds..
and also tried it against an external timer and your timer overran by about 8 seconds give or take..


PM
Belidos
3D Media Maker
8
Years of Service
User Offline
Joined: 23rd Nov 2015
Playing: The Game
Posted: 20th Mar 2018 14:32
Thanks for checking.

Quote: "there is an error in this line .. can you spot it ..?

Text(5,5,1,"Time:"..minute..":"..second..)"


Extra .. on the end? changed.

Quote: "also there is something missing from the last if statement "


end? changed.

Quote: "but.. it looks strange always starting from the 60 seconds.."


Yeah, because i reset it to 60 instead of 59 each time. changed.

Quote: "and also tried it against an external timer and your timer overran by about 8 seconds give or take.."


Probably because of the 60/59 mistake.

Made the changes, how's it looking now?

Primary Desktop:
i7 7700,k NV1070 8GB, 16GB 3200mhz memory, 1x 2TB Hybrid, Win10.

Secondary Desktop:
i5 4760k, NV960 2GB, 16GB 2333mhz memory, 1x 2TB Hybrid, Win10.

Laptop:
i3, Intel 4000 series graphics, 6GB memory, 1x 500gb HDD, Win8.1.
J_C
16
Years of Service
User Offline
Joined: 9th Nov 2007
Location:
Posted: 20th Mar 2018 15:06
OK.. the run time is now close to external timer..
but it now looks strange going from 1 second to 59 seconds
it should go 1 second 0 second 59 seconds i think..

also I forgot to mention at end of 10 mins it continues to count down -1 min and so on..
PM
smallg
Community Leader
18
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 20th Mar 2018 15:30
Looks lime you got it
Just change
if second == 0 then
To
if second <= -1 then
(Otherwise the seconds get updated before the prompt shows 0).

I would also recommend changing all == or > to >= etc because scripts aren't always accurate to the millisecond so this way you will make sure there's no (noticeable) missed frames.

To stop the timer at 0:0 you should be able to just add Destroy(e) inside your bottom 'if' check.

For the back to menu part add
SetPlayerLives(1) -- might be 0, I'll let you test
HurtPlayer(e,g_PlayerHealth)

This will kill the player and force a game over.

lua guide for GG
https://steamcommunity.com/sharedfiles/filedetails/?id=398177770
windows 10
i5 @4ghz, 8gb ram, AMD R9 200 series , directx 11
Belidos
3D Media Maker
8
Years of Service
User Offline
Joined: 23rd Nov 2015
Playing: The Game
Posted: 20th Mar 2018 16:23 Edited at: 20th Mar 2018 16:30
Thanks for the tips smallg.

Quote: "For the back to menu part add
SetPlayerLives(1) -- might be 0, I'll let you test
HurtPlayer(e,g_PlayerHealth)

This will kill the player and force a game over."


Yeah that's a good way to do that, but the problem is that will generate the death sound and the red mess, i don't really want that, but it will do if there's no proper way of returning to the main menu.

Basically i'm taking some of the ideas i had for my "paranormal evidence" WIP, and changing it to "Escape Room", where each level is a computer version of one of those Escape Rooms you go to and have X time to work out all the clues, have some nice little puzzles planned out for it

Primary Desktop:
i7 7700,k NV1070 8GB, 16GB 3200mhz memory, 1x 2TB Hybrid, Win10.

Secondary Desktop:
i5 4760k, NV960 2GB, 16GB 2333mhz memory, 1x 2TB Hybrid, Win10.

Laptop:
i3, Intel 4000 series graphics, 6GB memory, 1x 500gb HDD, Win8.1.
Belidos
3D Media Maker
8
Years of Service
User Offline
Joined: 23rd Nov 2015
Playing: The Game
Posted: 20th Mar 2018 16:40
Quote: "also I forgot to mention at end of 10 mins it continues to count down -1 min and so on.."


Yeah it will do because i haven't set anything to stop it, i just wanted it to trigger a variable at 0.0 so that another script can pick up the variable to end the level.

Primary Desktop:
i7 7700,k NV1070 8GB, 16GB 3200mhz memory, 1x 2TB Hybrid, Win10.

Secondary Desktop:
i5 4760k, NV960 2GB, 16GB 2333mhz memory, 1x 2TB Hybrid, Win10.

Laptop:
i3, Intel 4000 series graphics, 6GB memory, 1x 500gb HDD, Win8.1.
smallg
Community Leader
18
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 20th Mar 2018 18:18
you could try FinishLevel() or LeaveGame()
not really sure what they do though or if they can just be called without any other clean up but LeaveGame() seems to be the way the 'escape' menu goes back to the main menu.
lua guide for GG
https://steamcommunity.com/sharedfiles/filedetails/?id=398177770
windows 10
i5 @4ghz, 8gb ram, AMD R9 200 series , directx 11
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 20th Mar 2018 19:23
This is what I had in mind:
Been there, done that, got all the T-Shirts!

Attachments

Login to view attachments
PM
Belidos
3D Media Maker
8
Years of Service
User Offline
Joined: 23rd Nov 2015
Playing: The Game
Posted: 20th Mar 2018 22:29
Thanks guys.

Cheers for that amen, I know I'm asking quite a bit at the moment, but any chance you can paste the code for me, I won't have access to a pc for a few days and I'd like to take a look and see what you did different.

Thanks.

Primary Desktop:
i7 7700,k NV1070 8GB, 16GB 3200mhz memory, 1x 2TB Hybrid, Win10.

Secondary Desktop:
i5 4760k, NV960 2GB, 16GB 2333mhz memory, 1x 2TB Hybrid, Win10.

Laptop:
i3, Intel 4000 series graphics, 6GB memory, 1x 500gb HDD, Win8.1.
Belidos
3D Media Maker
8
Years of Service
User Offline
Joined: 23rd Nov 2015
Playing: The Game
Posted: 21st Mar 2018 09:17
Quote: "but LeaveGame() seems to be the way the 'escape' menu goes back to the main menu"



That sounds perfect to me Smallg, thanks for that.

Primary Desktop:
i7 7700,k NV1070 8GB, 16GB 3200mhz memory, 1x 2TB Hybrid, Win10.

Secondary Desktop:
i5 4760k, NV960 2GB, 16GB 2333mhz memory, 1x 2TB Hybrid, Win10.

Laptop:
i3, Intel 4000 series graphics, 6GB memory, 1x 500gb HDD, Win8.1.
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 21st Mar 2018 19:03
There you go.

Been there, done that, got all the T-Shirts!
PM
Belidos
3D Media Maker
8
Years of Service
User Offline
Joined: 23rd Nov 2015
Playing: The Game
Posted: 22nd Mar 2018 08:35 Edited at: 22nd Mar 2018 08:36
Thanks Amen, yes that is very different, and an example of the stuff that comes across as giberish to me that i was talking about, in the one i finally managed to come up with i can at least understand how it works, but you've used stuff in there that makes my brain hurt, i can't make head nor tail of it, that's why i don't like scripting myself, i can understand the basics, but when you throw code like that at me i just want to bury my head in the ground and hope i die quickly lol

Primary Desktop:
i7 7700,k NV1070 8GB, 16GB 3200mhz memory, 1x 2TB Hybrid, Win10.

Secondary Desktop:
i5 4760k, NV960 2GB, 16GB 2333mhz memory, 1x 2TB Hybrid, Win10.

Laptop:
i3, Intel 4000 series graphics, 6GB memory, 1x 500gb HDD, Win8.1.
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 22nd Mar 2018 09:26
Which bits don't you understand? There are four if checks and two subtract statements, I doubt very much you could make it any simpler than that!

The os.date call is simply taking a number and producing a string representing it as a time value, normally when called it uses the number from the system clock but in this case I am simply passing the number representing the ten minute timer. As it stands that will produce output as "00:09:45" for example but you could use strings.sub to strip off the leading digits if need be.

Been there, done that, got all the T-Shirts!
PM
Belidos
3D Media Maker
8
Years of Service
User Offline
Joined: 23rd Nov 2015
Playing: The Game
Posted: 22nd Mar 2018 10:09 Edited at: 22nd Mar 2018 10:11
Quote: "local tenMinuteTimer = 10 * 60 "


What does the * etc do?

Quote: "if not runTimer then return end"


if not? return end? what do they do?

Quote: "%X"


What does that do?

Quote: "local timeNow = g_Time"


Why is it parsing g_Time to a variable? Could you not just use g_Time as the variable anyway, or doesn't that work?

Primary Desktop:
i7 7700,k NV1070 8GB, 16GB 3200mhz memory, 1x 2TB Hybrid, Win10.

Secondary Desktop:
i5 4760k, NV960 2GB, 16GB 2333mhz memory, 1x 2TB Hybrid, Win10.

Laptop:
i3, Intel 4000 series graphics, 6GB memory, 1x 500gb HDD, Win8.1.
smallg
Community Leader
18
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 22nd Mar 2018 11:23
Quote: "What does the * etc do?"

it's basically 10 x 60
10 minutes = 10 x 60 seconds

Quote: "if not? return end? what do they do?"

that's just short version of code
same as writing

'return' just exits the script/function so no further code is run this frame... i.e. because the timer doesn't exist yet we don't want to process the script.

Quote: "What does"%X" do?"

it's a special lua command that filters out a string to only use hexadecimal characters from it's string
there's some more alternatives
Quote: "%a letters
%c control characters
%d digits
%l lower case letters
%p punctuation characters
%s space characters
%u upper case letters
%w alphanumeric characters
%x hexadecimal digits
%z the character with representation 0"

i was confused it worked like that too but it seems the os.date() command is also a bit special and lets you search it by default, normally you will use something like
Prompt(string.gsub("hello, up-down!", "%A", "."))
which would produce
hello..up.down.
(basically swaps all non-letters with a period)

Quote: "Why is it parsing g_Time to a variable? Could you not just use g_Time as the variable anyway, or doesn't that work?"

yes you can but if you want to be fussy about speed or make sure the result is always the same you declare it as a local to make it easier for the script to access... think of it like looking at your watch every time g_Time is called compared to looking at it once and remembering the time.
lua guide for GG
https://steamcommunity.com/sharedfiles/filedetails/?id=398177770
windows 10
i5 @4ghz, 8gb ram, AMD R9 200 series , directx 11
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 22nd Mar 2018 19:19 Edited at: 22nd Mar 2018 19:21
%X is just an instruction to the os.date call to format the time in a specific way, in this case the HH:MM:SS format. There are other formats available, the website I linked to describes them all.

Global variables and functions are really slow to access as Lua has to look them up, local variables and functions are much quicker so out of habit if I am accessing a global variable or function more than once in a script I copy it to a local variable. (in the case of a function call it doesn't make a copy of the function it just points to the actual function directly, e.g. 'local sin = math.sin'.

Anything that evaluates to a constant in Lua is evaluated at 'compile' time btw so in the case of the '10 * 60' the multiplication part is only done once before the code is ever run but it helps when you read the code back later to indicate that it is 10 time 60 seconds. A purist would say you should do something like:



But that's just daft imo.
Been there, done that, got all the T-Shirts!
PM

Login to post a reply

Server time is: 2024-03-29 08:42:29
Your offset time is: 2024-03-29 08:42:29