1. Post #401
    Heisenberg
    Neddy's Avatar
    March 2005
    4,336 Posts
    Is there any good gmod lua tutorials around? The ones on the wiki are abit.. Bland. I got to http://maurits.tv/data/garrysmod/wik...he_for_in_loop and then when I got to the abit about k and v I just got lost.
    The next two letters, k and v, are the names of the variables in the loop. If you remember, in the for loop we had one variable, "i". "i" was a number that was increased each time the loop run. In the for in loop, "k" is the slot the loop is on, and v is what's inside of the slot. Take a look back at our code:
    I just got lost.
    Reply With Quote Edit / Delete Windows 7 Show Events

  2. Post #402

    December 2011
    350 Posts
    I suggest http://www.lua.org/pil/ for learning the basic's of lua

    when I got to the abit about k and v I just got lost.
    I just got lost.
    Read this http://www.lua.org/pil/4.3.5.html
    Reply With Quote Edit / Delete Windows 7 United Kingdom Show Events

  3. Post #403

    July 2007
    864 Posts
    Is there any good gmod lua tutorials around? The ones on the wiki are abit.. Bland. I got to http://maurits.tv/data/garrysmod/wik...he_for_in_loop and then when I got to the abit about k and v I just got lost.
    I just got lost.

    Do you know how a table is contructed?
    tbl = {}
    tbl[key] = "value"

    For example:
    tbl[1] = "foo"
    tbl[2] = "bar"
    for k, v in pairs(tbl) do --for key, value in pairs
         Msg(v .. " ")
    end
    
    for k, v in pairs(tbl) do
         Msg(k .. " ")
    end
    
    > foo bar
    > 1 2
    Reply With Quote Edit / Delete Windows 7 Denmark Show Events Lua Helper Lua Helper x 1 (list)

  4. Post #404
    Heisenberg
    Neddy's Avatar
    March 2005
    4,336 Posts
    I get the whole tables bit. Just didn't understand where k and V came into it. So V is displaying what is stored in that table entry and K is for the numbers that are stored in that table? So like

     
    tbl[1] = "My"
    tbl[2] = "Name"
    tbl[3] = "is"
    tbl[4] = "Neddy"
    
    for k, v in pairs(tbl) do --for key, value in pairs
         Msg(v .. " ")
    end
     
    for k, v in pairs(tbl) do
         Msg(k .. " ")
    end 
    

    > My Name is Neddy
    > 1 2 3 4

    ?
    Reply With Quote Edit / Delete Windows 7 Show Events Agree Agree x 3 (list)

  5. Post #405
    Fantym420's Avatar
    October 2010
    897 Posts
    I get the whole tables bit. Just didn't understand where k and V came into it. So V is displaying what is stored in that table entry and K is for the numbers that are stored in that table? So like

     
    tbl[1] = "My"
    tbl[2] = "Name"
    tbl[3] = "is"
    tbl[4] = "Neddy"
    
    for k, v in pairs(tbl) do --for key, value in pairs
         Msg(v .. " ")
    end
     
    for k, v in pairs(tbl) do
         Msg(k .. " ")
    end 
    

    > My Name is Neddy
    > 1 2 3 4

    ?
    k stands for key, which is the index of the table
    v stands for value, which is the value stored at that index

    doing a for you can make the k and v what ever you want like if you were doing players you could do

    
    for useless, ply in pairs(player.GetAll()) do
      ply:ChatPrint("Hi!!")
    end
    
    

    i called the first useless because i'm not using it, sometimes people use _ as the first if they aren't using the key, and the second I called ply because it's a player object.

    You pretty much got the idea with the example you posted back, just thought i'd fill in some more info.
    Reply With Quote Edit / Delete Windows 7 United States Show Events Lua Helper Lua Helper x 2 (list)

  6. Post #406

    July 2007
    864 Posts
    The key CAN be a non-numeric value, but is set to a numeric if it's set automatic.
    You can do:
    tbl["Name"] = "John"
    tbl["Age"] = 12
    
    for key, value in pairs(tbl) do
        print(k .. " - " .. v)
    end
    
    > Name - John
    > Age - 12
    Reply With Quote Edit / Delete Windows 7 Denmark Show Events Agree Agree x 1 (list)

  7. Post #407
    Title

    August 2009
    300 Posts
    Ah. Well for one, I don't have the screen clicker enabled. That's probably why you can't rotate with the mouse. I'd remove the "gui.EnableScreenClicker(true)" line and limit the player's range some other way.
    I've done this
    function GM:CreateMove(cmd)
        if LocalPlayer():Alive() then
            gui.EnableScreenClicker(true)
            local mx,my = gui.MousePos()
            local pos = LocalPlayer():GetShootPos():ToScreen()
            pos = Vector(pos.x,pos.y,0)
             
            local ang = (Vector(mx,my,0)-pos):Angle()
    
            cmd:SetViewAngles(ang)
        end
    end
    

    That's working (a bit wrong as its back to front) but It's not exactly what I want. What I'm wanting is a top-down view where the player rotates when you move your mouse around but the camera is static and won't rotate (So you can aim in different directions around you.) It needs to actually aim there as-well tho so flash light and weapons work.

    I'm guessing I shouldn't use SetViewAngles for this, but what else is there?
    Reply With Quote Edit / Delete Windows 7 United Kingdom Show Events

  8. Post #408
    LuaGuy's Avatar
    April 2011
    129 Posts
    I'm coming from C++ and i have a question. I'm looking at Lua for C Programmers in the Garry's Mod wiki but i cant find any answer to my question.

    For example

    local meta = FindMetaTable("Player")
     
    function meta:WowFunction(command)
         self:ConCommand(command)
         self:ChatPrint("Wow function ran the console command: " .. tostring(command))
    end
    
    ply = LocalPlayer()
    ply:WowFunction()
    

    is the colon means the same thing as Unary Scope Resolution Operator in C++ ( :: ) ?
    Reply With Quote Edit / Delete Windows 7 Turkey Show Events

  9. Post #409

    July 2007
    864 Posts
    I'm coming from C++ and i have a question. I'm looking at Lua for C Programmers in the Garry's Mod wiki but i cant find any answer to my question.

    For example

    local meta = FindMetaTable("Player")
     
    function meta:WowFunction(command)
         self:ConCommand(command)
         self:ChatPrint("Wow function ran the console command: " .. tostring(command))
    end
    
    ply = LocalPlayer()
    ply:WowFunction()
    

    is the colon means the same thing as Unary Scope Resolution Operator in C++ ( :: ) ?
    I don't know c++, but I think yes.
    That code should work, except you didn't parse anything as argument 1 on line 9 to ply:WowFunction()
    Reply With Quote Edit / Delete Windows 7 Denmark Show Events

  10. Post #410
    PENISCORP DIRECTOR
    Gran PC's Avatar
    August 2007
    3,114 Posts
    obj:function() is syntactic sugar for obj.function(obj). In declarations it's syntactic sugar for obj.function(self)
    Reply With Quote Edit / Delete Spain Show Events Useful Useful x 1 (list)

  11. Post #411
    Title

    August 2009
    300 Posts
    I've done this
    function GM:CreateMove(cmd)
        if LocalPlayer():Alive() then
            gui.EnableScreenClicker(true)
            local mx,my = gui.MousePos()
            local pos = LocalPlayer():GetShootPos():ToScreen()
            pos = Vector(pos.x,pos.y,0)
             
            local ang = (Vector(mx,my,0)-pos):Angle()
    
            cmd:SetViewAngles(ang)
        end
    end
    

    That's working (a bit wrong as its back to front) but It's not exactly what I want. What I'm wanting is a top-down view where the player rotates when you move your mouse around but the camera is static and won't rotate (So you can aim in different directions around you.) It needs to actually aim there as-well tho so flash light and weapons work.

    I'm guessing I shouldn't use SetViewAngles for this, but what else is there?
    Didn't explain that well but basically is there a function to set the players rotation? (So he faces the mouse all the time) and the camera can stay static facing down and not rotate.
    Reply With Quote Edit / Delete Windows 7 United Kingdom Show Events

  12. Post #412
    Awesome Gmod Servers!
    brandonj4's Avatar
    September 2011
    2,073 Posts
    I am looking for a way to make a hud that i can move with a derma panel slider control that accords to the y and x axis.
    Reply With Quote Edit / Delete Windows 7 Canada Show Events

  13. Post #413
    JustSoFaded's Avatar
    December 2011
    432 Posts
    I'm coming from C++ and i have a question. I'm looking at Lua for C Programmers in the Garry's Mod wiki but i cant find any answer to my question.

    For example

    local meta = FindMetaTable("Player")
     
    function meta:WowFunction(command)
         self:ConCommand(command)
         self:ChatPrint("Wow function ran the console command: " .. tostring(command))
    end
    
    ply = LocalPlayer()
    ply:WowFunction()
    

    is the colon means the same thing as Unary Scope Resolution Operator in C++ ( :: ) ?
    It's somewhat similar. In C++ it's used to access a class/namespace function (I believe, I'm still learning C++). In Lua, you are passing meta data as the first arg in the function. Like this:

    local meta = FindMetaTable("Player");
    

    You have now made a variable storing meta data for the Player metatable locally.

    You can do
    function meta:WowFunc( command );
    

    or you can do

    function meta.WowFunc(player_object, command);
    

    Like GranPC said, it's just a fancier/cleaner way of doing things.
    Reply With Quote Edit / Delete Windows 7 United States Show Events

  14. Post #414
    Gold Banana
    Banana Lord.'s Avatar
    May 2010
    6,843 Posts
    I personally prefer to do _R.Player:WowFunc
    Reply With Quote Edit / Delete Windows 7 United States Show Events Agree Agree x 5 (list)

  15. Post #415
    Ask me about my ex-boyfriend being raided by the FBI
    Nerdeboy's Avatar
    August 2009
    1,862 Posts
    Didn't explain that well but basically is there a function to set the players rotation? (So he faces the mouse all the time) and the camera can stay static facing down and not rotate.
    Player.SetEyeAngles can be used to modify a Player's current facing. Calling it clientside wouldn't do anything other than compensation, though, so you'll probably want to call it somewhere in your shared.lua. The problem would be finding a way to transfer the angle between the player and the cursor into it.

    I recommend calling it somewhere during Gamemode.Move because this runs constantly and it's relevant.
    Reply With Quote Edit / Delete Windows 7 United States Show Events

  16. Post #416
    JustSoFaded's Avatar
    December 2011
    432 Posts
    I personally prefer to do _R.Player:WowFunc
    FindMetaTable("Player") basically returns _R.Player, it's no different. It just looks different.
    Reply With Quote Edit / Delete Windows 7 United States Show Events Agree Agree x 1 (list)

  17. Post #417
    PROUD BRONY 4LYFE
    Drakehawke's Avatar
    February 2009
    3,312 Posts
    Anyone know of a hacky way to find out if an entity is on fire clientside? I know entity.IsOnFire is shared in GMod 13 but I kinda need it now.
    Reply With Quote Edit / Delete Windows Vista United Kingdom Show Events

  18. Post #418
    Gold Member
    ralle105's Avatar
    October 2005
    4,648 Posts
    function _R.Entity:IsOnFire()
        for k,v in pairs(ents.FindByClass("entityflame")) do
            if (v:GetParent() == self) then
                return true
            end
        end
        return false
    end
    
    Reply With Quote Edit / Delete Windows 7 Sweden Show Events Useful Useful x 1 (list)

  19. Post #419
    Piciul's Avatar
    April 2011
    339 Posts
    Hi, Im a newfag to lua, and I've recently been accepted into a HL2RP development team for a really nice and popular community I play in. Anyways, here's the deal, Im supposed to create a script that plays random metrocop radio chatter whenever a metrocop(s) is near. The chatter basically isnt the metrocop speaking, its just "imaginary" metrocops reporting in stuff and etc. Actually just like you hear combine chatter in HL2. Should I do this by making a function which then would be called automatically upon server start? And how? Help would be very appreciated.

    Thanks for reading,

    Piciul
    Reply With Quote Edit / Delete Windows 7 Denmark Show Events Lua King Lua King x 2 (list)

  20. Post #420
    Gold Banana
    Banana Lord.'s Avatar
    May 2010
    6,843 Posts
    FindMetaTable("Player") basically returns _R.Player, it's no different. It just looks different.
    yeah, just my preference.
    Reply With Quote Edit / Delete Windows 7 United States Show Events

  21. Post #421

    July 2007
    864 Posts
    Hi, Im a newfag to lua, and I've recently been accepted into a HL2RP development team for a really nice and popular community I play in. Anyways, here's the deal, Im supposed to create a script that plays random metrocop radio chatter whenever a metrocop(s) is near. The chatter basically isnt the metrocop speaking, its just "imaginary" metrocops reporting in stuff and etc. Actually just like you hear combine chatter in HL2. Should I do this by making a function which then would be called automatically upon server start? And how? Help would be very appreciated.

    Thanks for reading,

    Piciul
    So wait, you were accepted to the development team without knowing Lua?
    Reply With Quote Edit / Delete Windows 7 Denmark Show Events Funny Funny x 3Agree Agree x 1 (list)

  22. Post #422
    Piciul's Avatar
    April 2011
    339 Posts
    So wait, you were accepted to the development team without knowing Lua?
    Oh, I do know little bits of it. Just not the advanced stuff, I just wanted to edit my post saying I found out half the shit I listed above, right now I only need help on how to make it play only when CPs are near. And how not to make it play for players who are out of range. You're right though, my post sure does express that I dont know shit about lua.
    Reply With Quote Edit / Delete Windows 7 Denmark Show Events Dumb Dumb x 1Optimistic Optimistic x 1Lua King Lua King x 1 (list)

  23. Post #423
    "The superior man understands what is right; the inferior man understands what will sell"
    Chessnut's Avatar
    August 2011
    3,543 Posts
    Use Entity.FindInSphere and EmitSound. You should probably use a timer or the Think hook along with setting some sort of next play time kind of variable for the player to know when to emit the sound.
    Reply With Quote Edit / Delete United States Show Events Lua King Lua King x 1 (list)

  24. Post #424
    Awesome Gmod Servers!
    brandonj4's Avatar
    September 2011
    2,073 Posts
    I am looking for a way to make a hud that i can move with a derma panel slider control that accords to the y and x axis.

    Should I use Convars for this?
    Reply With Quote Edit / Delete Windows 7 Canada Show Events

  25. Post #425
    "The superior man understands what is right; the inferior man understands what will sell"
    Chessnut's Avatar
    August 2011
    3,543 Posts
    Yes, it's also how most STools handle their properties.
    Reply With Quote Edit / Delete Windows 7 United States Show Events

  26. Post #426
    Awesome Gmod Servers!
    brandonj4's Avatar
    September 2011
    2,073 Posts
    Yes, it's also how most STools handle their properties.
    I tried that and the only way it updates is if I reload the script.
    Reply With Quote Edit / Delete Windows 7 Canada Show Events

  27. Post #427
    "The superior man understands what is right; the inferior man understands what will sell"
    Chessnut's Avatar
    August 2011
    3,543 Posts
    Is the position of the HUD being updated based on the convar?
    Reply With Quote Edit / Delete Windows 7 United States Show Events

  28. Post #428
    Awesome Gmod Servers!
    brandonj4's Avatar
    September 2011
    2,073 Posts
    Is the position of the HUD being updated based on the convar?
    CreateClientConVar("xpos", "0", true, false)
    CreateClientConVar("ypos", "0", true, false)
    local xpos = GetConVarNumber("xpos")
    concommand.Add("menu", function()
    local DermaPanel = vgui.Create( "DFrame" )
    DermaPanel:SetPos( 50,50 )
    DermaPanel:SetSize( 200, 250 )
    DermaPanel:SetTitle( "Testing Derma Stuff" )
    DermaPanel:SetVisible( true )
    DermaPanel:SetDraggable( true )
    DermaPanel:ShowCloseButton( true )
    DermaPanel:MakePopup()
     
    local NumSlider = vgui.Create( "DNumSlider", DermaPanel )
    NumSlider:SetPos( 25,50 )
    NumSlider:SetWide( 150 )
    NumSlider:SetText( "Max Props" )
    NumSlider:SetMin( 0 ) -- Minimum number of the slider
    NumSlider:SetMax( ScrH() ) -- Maximum number of the slider
    NumSlider:SetDecimals( 0 ) -- Sets a decimal. Zero means it's a whole number
    NumSlider:SetConVar( "xpos" ) -- Set the convar
    end)
    function HUDPAINT()
    	if POINTSHOP.Config.DisplayPoints then
    		hook.Add("HUDPaint", "PointShop_HUDPaint", function()
    			local text = "Points: " .. LocalPlayer():PS_GetPoints()
    		if LocalPlayer():Alive() then
    			draw.WordBox( 4, xpos,100, text, "ScoreboardText", Color(0,0,0,b), Color(255,255,255,a))
    			draw.WordBox( 4, 10,10, "Shop[F4]", "ScoreboardText", Color(0,0,0,shopalpha), Color(255,255,255,shoptxtalpha))
    		end
    			points = LocalPlayer():PS_GetPoints()
    		end)
    	end
    end
    
    Reply With Quote Edit / Delete Windows 7 Canada Show Events

  29. Post #429
    Gold Banana
    Banana Lord.'s Avatar
    May 2010
    6,843 Posts
    that sets it to xpos when it's created, use GetConVarNumber( "xpos" ) in the draw code
    Reply With Quote Edit / Delete Windows 7 United States Show Events

  30. Post #430
    joyenusi's Avatar
    May 2006
    148 Posts
    How am I able to tell whether or not the player is looking at a door ?
    Reply With Quote Edit / Delete Windows 7 United Kingdom Show Events

  31. Post #431
    ASK ME ABOUT MY SAM 6420 FETISH
    ArmageddonScr's Avatar
    July 2011
    691 Posts
    How am I able to tell whether or not the player is looking at a door ?
    function _R.Player:LookingAtDoor()
        local door=self:GetEyeTraceNoCursor().Entity
        return door:GetClass()=="prop_door_rotating" -- Something like this
    end
    
    Reply With Quote Edit / Delete Windows 7 United States Show Events

  32. Post #432
    "The superior man understands what is right; the inferior man understands what will sell"
    Chessnut's Avatar
    August 2011
    3,543 Posts
    Get the door entity and check what class it is.

    Code:
    lua_run print( string.find(player.GetByID(1):GetEyeTrace().Entity:GetClass(), "door") )
    Reply With Quote Edit / Delete Windows 7 United States Show Events

  33. Post #433
    Awesome Gmod Servers!
    brandonj4's Avatar
    September 2011
    2,073 Posts
    that sets it to xpos when it's created, use GetConVarNumber( "xpos" ) in the draw code
    local xpos = GetConVarNumber("xpos")

    isnt that the same thing?
    Reply With Quote Edit / Delete Windows 7 Canada Show Events

  34. Post #434
    joyenusi's Avatar
    May 2006
    148 Posts
    How do I spawn an entity in a function, say I want to have an NPC drop an entity item when killed ?
    Reply With Quote Edit / Delete Windows 7 United Kingdom Show Events

  35. Post #435
    "The superior man understands what is right; the inferior man understands what will sell"
    Chessnut's Avatar
    August 2011
    3,543 Posts
    Use GM:OnNPCKilled() and ents.Create() for spawning the weapon.
    Reply With Quote Edit / Delete Windows 7 United States Show Events

  36. Post #436
    Ask me about my ex-boyfriend being raided by the FBI
    Nerdeboy's Avatar
    August 2009
    1,862 Posts
    local xpos = GetConVarNumber("xpos")

    isnt that the same thing?
    You're only setting xpos when the script first runs. You have to set it every time your HUD is drawn.
    Reply With Quote Edit / Delete Windows 7 United States Show Events

  37. Post #437
    Awesome Gmod Servers!
    brandonj4's Avatar
    September 2011
    2,073 Posts
    You're only setting xpos when the script first runs. You have to set it every time your HUD is drawn.
    Care to explain how?
    Reply With Quote Edit / Delete Windows 7 Canada Show Events

  38. Post #438
    Ask me about my ex-boyfriend being raided by the FBI
    Nerdeboy's Avatar
    August 2009
    1,862 Posts
    CreateClientConVar("xpos", "0", true, false)
    CreateClientConVar("ypos", "0", true, false)
    concommand.Add("menu", function()
    local DermaPanel = vgui.Create( "DFrame" )
    DermaPanel:SetPos( 50,50 )
    DermaPanel:SetSize( 200, 250 )
    DermaPanel:SetTitle( "Testing Derma Stuff" )
    DermaPanel:SetVisible( true )
    DermaPanel:SetDraggable( true )
    DermaPanel:ShowCloseButton( true )
    DermaPanel:MakePopup()
     
    local NumSlider = vgui.Create( "DNumSlider", DermaPanel )
    NumSlider:SetPos( 25,50 )
    NumSlider:SetWide( 150 )
    NumSlider:SetText( "Max Props" )
    NumSlider:SetMin( 0 ) -- Minimum number of the slider
    NumSlider:SetMax( ScrH() ) -- Maximum number of the slider
    NumSlider:SetDecimals( 0 ) -- Sets a decimal. Zero means it's a whole number
    NumSlider:SetConVar( "xpos" ) -- Set the convar
    end)
    function HUDPAINT()
    	if POINTSHOP.Config.DisplayPoints then
    		hook.Add("HUDPaint", "PointShop_HUDPaint", function()
    			local text = "Points: " .. LocalPlayer():PS_GetPoints()
    			local xpos = GetConVarNumber("xpos")  -- by moving it here
    		if LocalPlayer():Alive() then
    			draw.WordBox( 4, xpos,100, text, "ScoreboardText", Color(0,0,0,b), Color(255,255,255,a))
    			draw.WordBox( 4, 10,10, "Shop[F4]", "ScoreboardText", Color(0,0,0,shopalpha), Color(255,255,255,shoptxtalpha))
    		end
    			points = LocalPlayer():PS_GetPoints()
    		end)
    	end
    end
    
    Reply With Quote Edit / Delete Windows 7 United States Show Events

  39. Post #439
    "The superior man understands what is right; the inferior man understands what will sell"
    Chessnut's Avatar
    August 2011
    3,543 Posts
    Since you're opening the script, it will seem like the HUD is working. It's only being called one.
    You have to hook the function to the HUDPaint hook.
    Reply With Quote Edit / Delete Windows 7 United States Show Events

  40. Post #440

    February 2012
    56 Posts
    Is there a list of methods available for various things?
    Like a list of things you can do to a player in your code, and variables of a player you can change?
    Also, when i'm writing scripts for darkrp, how do i know where to put them? Like, if i wanted to make a script execute when a player types a command, where would i put that code?
    Reply With Quote Edit / Delete Windows 7 United States Show Events