News: Welcome back to Bullworth! If you haven't already, you will need to reset your password..


Author Topic: LUA Timing  (Read 2832 times)

0 Members and 1 Guest are viewing this topic.

Offline DaBOSS54320

  • Hero Member
  • ****
  • Posts: 3,398
  • Gender: Female
    • View Profile
LUA Timing
« on: June 06, 2015, 09:17:57 PM »
As you probably know... you can use Wait(ms) to make a script wait for a certain amount of time. This works... when you want the script to temporarily stop processing anything. When a script "waits" using the Wait function, it let's the game process everything it needs to process when your script is running (this is the reason that the game will crash if you're in a loop too long without "waiting", and why you need a Wait(0) in most loops). The game won't do anything but process the functions called in your script until "Wait" is called. 0 ms being a single frame. On a similar note, we should look at the button functions (IsButtonBeingPressed, IsButtonPressed, IsButtonBeingReleased). IsButtonPressed returns true (and will thus activate an "if" statement") if the button (or key) is being pressed when the script reaches the IsButtonPressed (because obviously that's when it will be called, so that's when it will be processed). So if you have a Wait(ms) before it, you'll have to still be pressing the button by the time the script gets past the Wait() for it to return true. This is simple enough, but let's look at how IsButtonBeingPressed and IsButtonBeingReleased in a more technical fashion as the Wait function can influence them.
IsButtonBeingPressed, returns true if the button was just pressed. But... let's see how that works exactly. As we know a Wait(0) will let 1 frame pass, then our script will run, and the game will be paused and not process anything but the script until the script "Waits" again. Well IsButtonBeingPressed actually technically means, was the button pressed on this specific frame? Let's look at some ways this could affect your script.
Situation 1: You have a "Wait(2000)" then immediately after, IsButtonBeingPressed(3,0). Well the IsButtonBeingPressed(3,0) will only return true if you press the button 3 (zoom-out) on the single frame after the 2 second wait (which is very difficulty, as there are 30-60 frames every second). So this could badly hurt your script.
Situation 2: You don't "Wait" at all, because you don't want the game to process and you want the game to be frozen until a button is pressed. This is a bad idea and won't work at all. The reason behind it is because button detection takes place while the game processes outside of your script (during a Wait). So it is ideal to always just have a Wait(0) running so that you can always detect button presses and respond to them. Unless you want to temporarily disable button pressing of course...
Situation 3: You have 2 "Wait(0)"s (or Wait(0) is somehow called twice) before an IsButtonBeingPressed. The IsButtonBeingPressed will of course only return true if pressed during the second Wait(0) (the one that happened last before IsButtonBeingPressed).

Alright, so after all that technical crap, we have a new question. We know buttons can't be detected during a long Wait(ms) because more than 1 frame will pass during that time. But... what if we want time to pass while still detecting buttons? Or what if we just want to process other things while waiting? It's quite simple, we can use the "GetTimer" function! GetTimer() returns a time in milliseconds for how long the script has been running. Here's a basic example of it's use.

local t = GetTimer() -- This gets the current time, and stores it in "t" (t is short for time. We can't use "time" because that is a reserved keyword)
repeat -- repeat, you should know this
  -- Process shit
  Wait(0) -- A frame pass
until GetTimer() > t + 3000 -- repeat until the current time goes past what the time was before the repeat loop + 3000

Basically, that code will run a repeat loop for 3 seconds. (3000 ms)
The basic code GetTimer() > t + 3000 works fairly well. GetTimer() gets the current time, and "t" (or whatever you decide to name it) stores the time from an earlier point. Basically the code returns true if it's been the specified amount of milliseconds (replace 3000) since the earlier time.

Here's some possible uses:
Timing a cutscene (like in a custom mission)
Hiding a menu after inactivity ("t" would be changed to the current time whenever the menu is used)
An event in a mission where you have to quickly do something or fail

There is tons of uses too, and I hope you guys find them and find this tutorial helpful!