• Print

Author Topic: Variables in timer functions  (Read 5280 times)

0 Members and 1 Guest are viewing this topic.

Offline captain1342

  • Full Member
  • ***
  • Posts: 104
  • Karma: 6
  • Quality is our standard
    • Aperture Development - Quality is our standard
Variables in timer functions
« on: August 12, 2017, 04:13:44 pm »
Hey,

I am working on a new addon and for that I need to start a timer for each player, but how can I get the player for wich the timer was started, inside the function? I wanted to use the timer name as storage but that doesen't work and I have no more ideas how I could do it.

This is what I tryed:

Code: Lua
  1. local num = table.insert( AntiHighPing.HighPingPlayers, v )
  2. timer.Create( (v:SteamID64().."_"..num.."_high_pinger"), AntiHighPing.Config.WaitSecBeforeKick, 1, AntiHighPing.CheckPlayer() )
Aperture Development
Quality is our standard

Website - GitHub  - Forum  - Steam  - Discord

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Variables in timer functions
« Reply #1 on: August 12, 2017, 05:19:39 pm »
Well if you already have "v" as your variable for player (in a player. GetAll() loop I'm assuming), you could just pass v to your CheckPlayer() function.
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline captain1342

  • Full Member
  • ***
  • Posts: 104
  • Karma: 6
  • Quality is our standard
    • Aperture Development - Quality is our standard
Re: Variables in timer functions
« Reply #2 on: August 12, 2017, 06:06:13 pm »
You mean I can just do CheckPlayer(v) in the function?
but when I start another timer with an other V does it still give the old V or the new V ?

Like
Player = Player1
timer.Create( "meh"..num, AntiHighPing.Config.WaitSecBeforeKick, 1, AntiHighPing.CheckPlayer(Player))
Player = Player2
timer.Create( "meh"..num, AntiHighPing.Config.WaitSecBeforeKick, 1, AntiHighPing.CheckPlayer(Player))

timer("Meh1") = Player2
timer("Meh2") = Player2
Aperture Development
Quality is our standard

Website - GitHub  - Forum  - Steam  - Discord

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Variables in timer functions
« Reply #3 on: August 12, 2017, 06:52:52 pm »
Sorry, wasn't really clear when I made that. I was assuming you had a player.GetAll() loop, which is where you got "v" from. This is what I thought:
Code: Lua
  1. for _, v in pairs( player.GetAll() ) do
  2.     -- Some code if you had before
  3.     local num = table.insert( AntiHighPing.HighPingPlayers, v )
  4.     timer.Create( v:SteamID64() .. "_" .. num .. "_high_pinger",  AntiHighPing.Config.WaitSecBeforeKick, 1, function() AntiHighPing.CheckPlayer( v ) end )
  5. end
  6.  

P.s. sorry I'm at work right now but I'll try to give a more in-depth answer when I get home.
« Last Edit: August 12, 2017, 08:58:30 pm by iViscosity »
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline captain1342

  • Full Member
  • ***
  • Posts: 104
  • Karma: 6
  • Quality is our standard
    • Aperture Development - Quality is our standard
Re: Variables in timer functions
« Reply #4 on: August 12, 2017, 07:08:03 pm »
Okay I get what you are doing but the function runns at the end of the timer, so when I start another 2 timers wouldn't that overwrite the v variable?
I mean how does it get an temp var?
Aperture Development
Quality is our standard

Website - GitHub  - Forum  - Steam  - Discord

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Variables in timer functions
« Reply #5 on: August 12, 2017, 08:32:00 pm »
I'm reasonably sure that it remembers because it's filled in when it runs.
Have you tried it? Do you get a null error?
If it doesn't work, try a simple bracket precedence.
See if using parenthesis around the var v makes it 'fill' before being run and 'remembered'
AntiHighPing.CheckPlayer( ( v ) )
Or, you just inserted it into a table...
Does anything else modify that table?
If not, you could, in theory, use;
AntiHighPing.CheckPlayer( AntiHighPing.HighPingPlayers.num )
If you can get v to work, it would be more efficient however. Tables are almost always more costly in the long run.

FYI - There's a discussion on this at Facepunch. My Google-Fu found it.
What you're trying to do, that iViscosity stated, should work, because v is set outside the timer create.
https://facepunch.com/showthread.php?t=1548817&p=51677181
« Last Edit: August 12, 2017, 08:36:05 pm by JamminR »
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline captain1342

  • Full Member
  • ***
  • Posts: 104
  • Karma: 6
  • Quality is our standard
    • Aperture Development - Quality is our standard
Re: Variables in timer functions
« Reply #6 on: August 13, 2017, 10:45:57 am »
Hadn't time to try it yet, I will test it tomorrow, thanks guys.
Aperture Development
Quality is our standard

Website - GitHub  - Forum  - Steam  - Discord

  • Print