• Print

Author Topic: Help with Check AFK command?  (Read 23824 times)

0 Members and 1 Guest are viewing this topic.

Offline Bytewave

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 718
  • Karma: 116
  • :)
    • My Homepage
Re: Help with Check AFK command?
« Reply #30 on: May 17, 2016, 06:17:50 pm »
Code: Lua
  1. function forceSpec( target )
  2.     -- ...
  3.     target_ply:ConCommand( "ttt_spectator_mode 1" )
parameters: target
variables used: target_ply
If the variable is named target in your parameter list, you need to use it under the name target, not target_ply.

Also, you are creating that timer out of the scope of your command. I'm not sure if you intentionally do that, but it won't work that way if you do. Well, it won't work either way, but it definitely won't work if you want it there. ;)
bw81@ulysses-forums ~ % whoami
Homepage

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Help with Check AFK command?
« Reply #31 on: May 17, 2016, 06:30:15 pm »
I had the variable at target_ply at first but I changed it trying to do something else. I hate how you guys say "it won't work anyways" Well WHY? I don't get it.
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline Bytewave

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 718
  • Karma: 116
  • :)
    • My Homepage
Re: Help with Check AFK command?
« Reply #32 on: May 17, 2016, 06:39:53 pm »
I had the variable at target_ply at first but I changed it trying to do something else. I hate how you guys say "it won't work anyways" Well WHY? I don't get it.
You're creating a timer that runs every ten seconds to force spectator on a player regardless of context.
The reason(s) this will not work (disregarding that this probably isn't your use case):
  • You're trying to use target_ply when target_ply is not defined in that scope. Think of scope as indentation level (for a properly-indented file). Function parameters and local variables are like ULX permissions, inherited down the tree but not back up. The target_ply variable is defined only for ulx.cafk(), and is inaccessible outside of it.

However, I'm sure you don't actually want to force just some random player to spectator every ten seconds. So, instead of fixing the issue of target_ply not being defined at the scope you're at, try moving the timer creation inside the body of ulx.cafk(). This should force the targeted player to spectate approximately 10 seconds after running the command.
bw81@ulysses-forums ~ % whoami
Homepage

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Help with Check AFK command?
« Reply #33 on: May 17, 2016, 07:42:17 pm »
Ok, I sort of get it. I think. So I should have something along the lines of
Code: Lua
  1. local CATEGORY_NAME = "Utility"
  2.  
  3. function forceSpec( target_ply )
  4.  
  5.         message = "You have been forced to AFK."
  6.        
  7.         target_ply:ConCommand( "ttt_spectator_mode 1" )
  8.         ULib.tsayError( target_ply, message )
  9.  
  10. end
  11.  
  12. function ulx.cafk( calling_ply, target_ply )
  13.  
  14.         ULib.clientRPC( target_ply, "ulx.cafkClientSide" )
  15.         ulx.fancyLogAdmin( calling_ply, "#A is checking if #T is AFK.", target_ply )
  16.  
  17.         timer.Create( "AFK Timer", 1, 10, function() forceSpec( target_ply ) end )
  18.  
  19. end
  20.  
  21.  
  22. local cafk = ulx.command( CATEGORY_NAME, "ulx cafk", ulx.cafk, "!cafk" )
  23. cafk:addParam{ type=ULib.cmds.PlayerArg }
  24. cafk:defaultAccess( ULib.ACCESS_OPERATOR )
  25. cafk:help( "Used to check if a player is AFK." )
?

And what do you mean "disregarding that this probably isn't your use case"?

Also, how do I get the target_ply variable to be used in both? How do I set "target_ply" == the person I'm using it on in both functions?

Or. Should I do something like
Code: Lua
  1. timer.Create( "AFK Timer", 1, 10, function forceSpec( target_ply ) -- or should it be "function( target_ply )" then the rest?
  2.  
  3. -- Code here
  4.  
  5. end )
?
« Last Edit: May 17, 2016, 07:47:04 pm by iViscosity »
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline Bytewave

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 718
  • Karma: 116
  • :)
    • My Homepage
Re: Help with Check AFK command?
« Reply #34 on: May 17, 2016, 08:06:18 pm »
You've already got what looks to be a proper script at first glance, so no, it needs no modification.

What I was saying about use case was mostly irrelevant, and I regret mentioning it. I was pointing out what was syntactically wrong with your code, but not logically.
bw81@ulysses-forums ~ % whoami
Homepage

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Help with Check AFK command?
« Reply #35 on: May 17, 2016, 08:18:03 pm »
I'm not sure why you keep trying to put 'target_ply' as an argument to forceSpec. It seems you don't understand how arguments work in Lua. I will try to explain, but I also suggest you read the section on functions in Programming in Lua (https://www.lua.org/pil/5.html).

Function declarations consist of three things: a name, a list of parameters, and a body.

The name is simply how you refer to and call the function. In your case, the name is 'forceSpec'.

The parameters are a list of 'slots' that can be assigned values via passing arguments to the function. In the function's code, these parameters are initialized as local variables and thus their values can be accessed.

The body is simply the code to be run when the function is called.

Here's an example:

Defining a function:

Code: Lua
  1. myFunction( parameter1, parameter2, parameter3 )
  2.   print( parameter2 ) -- we can access the parameters as local functions
  3. end

Calling a function:

Code: Lua
  1. myFunction( "value1", "value2", "value3" )

Output:

Code: [Select]
value2
Your forceSpec function has one parameter, target_ply. When you call the function, you should call it with a player argument, that is put a player object within the parentheses.

Example:

Code: Lua
  1. forceSpec( myPlayerObject )

I assume you want to be calling this on the player who is AFK, so within your cafk function just create your timer and pass target_ply to forceSpec. In this case it will work because target_ply is defined in that scope, and is a valid player.
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Help with Check AFK command?
« Reply #36 on: May 18, 2016, 03:41:35 am »
Ok I think I get it. So you're saying put the timer in the ulx.cafk function, but what do you mean "pass target_ply to forceSpec"
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Help with Check AFK command?
« Reply #37 on: May 18, 2016, 04:45:16 am »
When I say "pass <blank> to <function>" I mean to call <function> with <blank> as an argument.

e.g. "pass carrot to eatFood"

Code: Lua
  1. eatFood( carrot )
« Last Edit: May 18, 2016, 04:47:23 am by roastchicken »
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Help with Check AFK command?
« Reply #38 on: May 18, 2016, 05:14:57 am »
So for the forceSpec function, should I take out the target_ply variable and instead put it in the timer, then put the timer inside the ulx.cafk function?
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Help with Check AFK command?
« Reply #39 on: May 18, 2016, 05:35:55 am »
So for the forceSpec function, should I take out the target_ply variable and instead put it in the timer, then put the timer inside the ulx.cafk function?


I'm not exactly sure what you mean, but no, that sounds incorrect. Just put:

Code: Lua
  1. timer.Create( "AFK Timer", 1, 10, function() forceSpec( target_ply ) end )

in your cafk function.
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Help with Check AFK command?
« Reply #40 on: May 18, 2016, 05:56:11 am »
But I mean the actual function that the timer is calling, should it be function forceSpec( target_ply ) or function forceSpec() (when the actual function is defined)
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Help with Check AFK command?
« Reply #41 on: May 18, 2016, 12:38:35 pm »
If you don't have the target_ply parameter then you can't supply a player for it to target, so yes, you should keep

Code: Lua
  1. function forceSpec( target_ply )
« Last Edit: May 18, 2016, 12:42:34 pm by roastchicken »
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Help with Check AFK command?
« Reply #42 on: May 18, 2016, 04:37:04 pm »
Ok... everything seems to be working now. The timer isn't causing errors or anything and it's forcing me to spectator perfectly. However, I'm having one issue. When they click on the button, it's supposed to end it. I have
Code: Lua
  1. function ulx.cafkClientSide()
  2.        
  3.         local afkBox = vgui.Create("DFrame")
  4.         afkBox:MakePopup()
  5.         afkBox:SetSize(600,200)
  6.         afkBox:Center()
  7.         afkBox:SetTitle("Are you AFK?")
  8.         afkBox:SetDraggable( false )
  9.         afkBox.Width = 600
  10.         afkBox.Height = 200
  11.         afkBox:ShowCloseButton( false )
  12.  
  13.  
  14.  
  15.  
  16.         local afkText = vgui.Create("DLabel", afkBox)
  17.         afkText:SetText("Are You AFK?")
  18.         afkText:Center()
  19.         afkText:SetWidth(600)
  20.         afkText:SetFont( "AFKText" )
  21.  
  22.  
  23.  
  24.         local afkButton = vgui.Create("DButton", afkBox)
  25.         afkButton:SetPos(afkBox.Width/2-25, afkBox.Height/2+30)
  26.         afkButton:SetText("Not AFK")
  27.         afkButton.DoClick = function()
  28.  
  29.                 RunConsoleCommand( "say", "I'm not AFK." )
  30.                 afkBox:Close() 
  31.                 timer.Stop( "AFK Timer" )      
  32.                 timer.Remove( "AFK Timer" )    
  33.  
  34.         end
  35.        
  36. end

and I don't understand why it's not ending when I click the button. In other words, I'm clicking the button but regardless, in 10 seconds, I'm forced to spectator mode.
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Help with Check AFK command?
« Reply #43 on: May 18, 2016, 04:58:16 pm »
Nowhere in that code is there a timer called "AFK Timer" (or any timer at all, for that matter). Without all the code it will be difficult for us to help you.
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Help with Check AFK command?
« Reply #44 on: May 18, 2016, 05:12:44 pm »
Well I have the AFK Timer defined in the shared file, but I have the stop timer in the client file (because it's calling vgui, has to be in client). How can I make the clientside timer.Stop stop a shared timer.Create?

Would an Include help?
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

  • Print