execute smth for a specific time

Maverick Finn

Sheriff Weasel
I have a simple if statement:

Lua:
if this and that
    localvariableimade = true

but i want a result more like this:

Lua:
if this and that
    localvariableimade = true //but only for half a second! and then it goes false again.

i'd be happy if you guys could help!
 
Make a timer!

The game runs at 35 game tics a second. Hooks that run once a tic will run 35 times a second, like ThinkFrame or PlayerThink (watch out: this one runs once PER PLAYER, per tic).
All you have to do is make a variable that increments or decrements in one of these hooks, and that's pretty much it. You can then compare the value of the variable and do things with it.

example (untested):
local coolCounter = 0
local coolTimer = 3*TICRATE -- TICRATE is the amount of game tics on a second (35)

addHook("ThinkFrame", function() -- runs once every tic
	
	-- Timer that counts up, aka a counter lol
	
	coolCounter = $ + 1 -- go up by 1 every tic
	if coolCounter >= 3*TICRATE then -- if 3*35 tics have elapsed, i.e. 3 seconds,
		print("Three seconds have passed")
		coolCounter = 0 -- reset counter to zero
	end
	
	
	
	-- Timer that counts down
	
	coolTimer = $ - 1 -- go down by 1 every tic
	if coolTimer <= 0 then -- if timer depleted (all 3 seconds have passed),
		print("Three seconds have passed")
		coolTimer = 3*TICRATE -- restore timer to the time to count down from
	end
	
end)

Make sure to adapt this to use a player or mobj field if it's something that only concerns that particular mobj. You usually don't want the timer to be shared across all objects otherwise.
 
Make sure to adapt this to use a player or mobj field if it's something that only concerns that particular mobj. You usually don't want the timer to be shared across all objects otherwise.
If I only want it for the player, PlayerThink, right?
Post automatically merged:

I know all of the concepts presented in your code and the logic, but I still don’t have, you know, that “coding vision” to put two and two together lol
Post automatically merged:

~~ except for ticrate, thats one thing i never figured
 

Who is viewing this thread (Total: 1, Members: 0, Guests: 1)

Back
Top