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

0 Members and 1 Guest are viewing this topic.

Offline captain1342

  • Full Member
  • ***
  • Posts: 100
  • Karma: 4
    • View Profile
    • Aperture-Hosting
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-Hosting Manager

Web: https://Aperture-Hosting.de
Mail: Webmaster@Aperture-Hosting.de
Github: https://github.com/Aperture-Hosting
Discord: https://discord.gg/wxpbtYk
Teamspeak: ts3.Aperture-Hosting.de
Support: https://www.aperture-hosting.de/ticketsystem/
"Hosting with Vision"

Offline iViscosity

  • Hero Member
  • *****
  • Posts: 698
  • Karma: 39
    • View Profile
    • iViscosity Gaming
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 a PM or Join my Discord!.
--==[ Links ]==--
Steam
YouTube
Steam Group (feel free to join!)
Forums
Public Discord

Quote from: Steve Jobs
Your work is going to fill a large part of your life, and the only way to be truly satisfied is to do what you believe is great work. And the only way to do great work is to love what you do. If you haven't found it yet, keep looking. Don't settle. As with all matters of the heart, you'll know when you find it. And, like any great relationship, it just gets better and better as the years roll on. So keep looking until you find it. Don't settle.

Offline captain1342

  • Full Member
  • ***
  • Posts: 100
  • Karma: 4
    • View Profile
    • Aperture-Hosting
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-Hosting Manager

Web: https://Aperture-Hosting.de
Mail: Webmaster@Aperture-Hosting.de
Github: https://github.com/Aperture-Hosting
Discord: https://discord.gg/wxpbtYk
Teamspeak: ts3.Aperture-Hosting.de
Support: https://www.aperture-hosting.de/ticketsystem/
"Hosting with Vision"

Offline iViscosity

  • Hero Member
  • *****
  • Posts: 698
  • Karma: 39
    • View Profile
    • iViscosity Gaming
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 a PM or Join my Discord!.
--==[ Links ]==--
Steam
YouTube
Steam Group (feel free to join!)
Forums
Public Discord

Quote from: Steve Jobs
Your work is going to fill a large part of your life, and the only way to be truly satisfied is to do what you believe is great work. And the only way to do great work is to love what you do. If you haven't found it yet, keep looking. Don't settle. As with all matters of the heart, you'll know when you find it. And, like any great relationship, it just gets better and better as the years roll on. So keep looking until you find it. Don't settle.

Offline captain1342

  • Full Member
  • ***
  • Posts: 100
  • Karma: 4
    • View Profile
    • Aperture-Hosting
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-Hosting Manager

Web: https://Aperture-Hosting.de
Mail: Webmaster@Aperture-Hosting.de
Github: https://github.com/Aperture-Hosting
Discord: https://discord.gg/wxpbtYk
Teamspeak: ts3.Aperture-Hosting.de
Support: https://www.aperture-hosting.de/ticketsystem/
"Hosting with Vision"

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 7758
  • Karma: 353
  • Sertafide Ulysses Jenius
    • View Profile
    • 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 »
The price one pays for pursuing any profession or calling is an intimate knowledge of its ugly side. - James Baldwin

Offline captain1342

  • Full Member
  • ***
  • Posts: 100
  • Karma: 4
    • View Profile
    • Aperture-Hosting
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-Hosting Manager

Web: https://Aperture-Hosting.de
Mail: Webmaster@Aperture-Hosting.de
Github: https://github.com/Aperture-Hosting
Discord: https://discord.gg/wxpbtYk
Teamspeak: ts3.Aperture-Hosting.de
Support: https://www.aperture-hosting.de/ticketsystem/
"Hosting with Vision"