• Print

Author Topic: Round Timer  (Read 8747 times)

0 Members and 1 Guest are viewing this topic.

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Round Timer
« on: September 17, 2014, 01:15:10 pm »
I'm in development of a gamemode (for learning purposes) and I'm got to the part where I want to create a timer (for rounds)

I don't want to be walked through it step by step, since I'm trying to do as much as I can on my own, but I'm insure about this

I assumed I'd use a timer for this, but I don't know how to go about it

Basically, I want a round to be 8 minutes long and after every 8 minutes, the map will reset and every player will be sent back to their spawn and respawned

I know how to do the respawning and map resetting on my own, but I don't know what to do about the rest

Can anybody explain a little bit but still leave me room to figure it out? Thanks~

Offline Neku

  • Hero Member
  • *****
  • Posts: 549
  • Karma: 27
Re: Round Timer
« Reply #1 on: September 17, 2014, 02:50:53 pm »
You wouldn't use a timer, you'd use CurTime().
Out of the Garry's Mod business.

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Re: Round Timer
« Reply #2 on: September 17, 2014, 03:16:39 pm »
http://wiki.garrysmod.com/page/Global/CurTime
"Returns the uptime of the server in seconds (to at least 4 decimal places)"

How would that be used in this situation? 0.o

Offline Neku

  • Hero Member
  • *****
  • Posts: 549
  • Karma: 27
Re: Round Timer
« Reply #3 on: September 17, 2014, 04:25:38 pm »
You use that function and add on how long you want the round to last.
Then you have a hook check CurTime() to see if it is equal to the one you stored.

Code: Lua
  1. local EndRoundTime = CurTime() + 200
  2.  
  3. hook.Add( "Think", "CheckRoundTimer", function() -- Don't use the Think hook. It's icky.
  4.         if CurTime() == EndRoundTime then
  5.                 -- OMG IT'S THE END
  6.         end
  7. end )
« Last Edit: September 17, 2014, 05:19:01 pm by Neku »
Out of the Garry's Mod business.

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Re: Round Timer
« Reply #4 on: September 17, 2014, 05:57:03 pm »
I didn't try this yet, since I'm doing my hw, but I'm thinking about this, so let me know if this would work (or at least if this sounds right), please :P

Code: Lua
  1. StartOfRound = CurTime()

then in a hook (480 is 8 minutes in seconds)
Code: Lua
  1. if CurTime() == (StartOfRound  + 480 )
  2.      *insert code for map reset/player respawning*
« Last Edit: September 17, 2014, 06:19:41 pm by Zmaster »

Offline Neku

  • Hero Member
  • *****
  • Posts: 549
  • Karma: 27
Re: Round Timer
« Reply #5 on: September 17, 2014, 06:31:18 pm »
I'm unsure, as I have never used CurTime() myself. I'm only suggesting it because I've seen it used in a few gamemodes.
Out of the Garry's Mod business.

Offline Caustic Soda-Senpai

  • Sr. Member
  • ****
  • Posts: 469
  • Karma: 54
  • <Insert something clever here>
    • Steam Page
Re: Round Timer
« Reply #6 on: September 20, 2014, 05:56:11 am »
it looks right. no guarantee it'll work.
Once you get to know me, you'll find you'll have never met me at all.

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Re: Round Timer
« Reply #7 on: September 21, 2014, 10:56:52 am »
Code: Lua
  1. local EndRoundTime = CurTime() + 20
  2.  
  3. hook.Add( "Think", "CheckRoundTimer", function() -- Don't use the Think hook. It's icky.
  4.         if CurTime() == EndRoundTime then
  5.                 PrintMessage( HUD_PRINTTALK, "The round has ended" )
  6.                 local EndRoundTime = CurTime() + 20
  7.         end
  8. end )

Alrighty, I tried using what Neku said earlier and ignoring the advice to not use the Think hook (because I know of nothing else to use) and it did absolutely nothing
like...
nothing happened
ever

So I started looking through the gmod wiki for all the types of hooks, but I wasn't able to find any helpful hooks (just by scanning over the names of the hooks)

In other words, anyone got any hook that will work in place of Think? :P

Offline Neku

  • Hero Member
  • *****
  • Posts: 549
  • Karma: 27
Re: Round Timer
« Reply #8 on: September 21, 2014, 11:16:27 am »
Well, after you make a local variable, you wouldn't need to define it as local again.

And where are you putting the code?
Out of the Garry's Mod business.

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Re: Round Timer
« Reply #9 on: September 21, 2014, 12:50:35 pm »
oh brain fart
fixed the local variable thing

I put that code in the shared.lua because on the GMod wiki, the Think hook was said to be shared: https://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index2902.html
« Last Edit: September 21, 2014, 12:53:16 pm by Zmaster »

Offline Neku

  • Hero Member
  • *****
  • Posts: 549
  • Karma: 27
Re: Round Timer
« Reply #10 on: September 21, 2014, 01:07:43 pm »
I mean your file path.
Out of the Garry's Mod business.

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Re: Round Timer
« Reply #11 on: September 21, 2014, 01:09:56 pm »
oh okay

garrysmod/gamemodes/zjailbreak/gamemode/shared.lua

  • Print