• What do you need help with? V3
    6,419 replies, posted
I am still learning so please don't bash on my newbie script, but I am trying to put a box around my HUD that flickers to a new/random color every 10 seconds. I am tried doing this with a timer.Create function. [lua]function FlashyHUD() draw.RoundedBox(5, 10, ScrH() * .85,ScrW() * .27, 150, Color(math.random(1,255),math.random(1,255),math.random(1,255),255)) end hook.Add("HUDPaint", "FlickerHUD", FlashyHUD) timer.Create( "FlickerHud", 10, 0, FlashyHUD )[/lua] Would I use a timer like that to call the function with the HUD. Also without the timer at all(well even with it now) the the box changes colors but does it like every frame..
So I have a few questions. Is it possible to get the returned value of a CalcView hook call, or just get the modified eye angles of a local player in general, without actually delving into overriding gamemode/other functions or somesuch? I'm attempting my first try at making a three-dimensional gui system, but I've run into the problem of trying to actually get the real eye angles of the player. For example, I've had to hack around getting inside a vehicle, because the base CalcView function calls vehicle camera functions that muck the camera about, and attempting to use my old methods (that work well when the player is on foot) break. On top of that, the player position while in a vehicle is different from the player's eye pos because of CalcView. I tried to override the gamemode CalcView function to run all the other CalcView hooks to get the final pos/angle, but that was hugely overcomplicated and messy. And then, when I tried to actually test it, I wasn't able to get it to work completely. Implementation was half-assed, though, so I'm going to give it another go. It would be nice if there was another hook that returned the final CalcView pos/angles or a player function (and if there is I must be blind, because I didn't see it). [editline]7th January 2013[/editline] [QUOTE=Gaming_Unlim;39137090][lua]function FlashyHUD() draw.RoundedBox(5, 10, ScrH() * .85,ScrW() * .27, 150, Color(math.random(1,255),math.random(1,255),math.random(1,255),255)) end hook.Add("HUDPaint", "FlickerHUD", FlashyHUD) timer.Create( "FlickerHud", 10, 0, FlashyHUD )[/lua][/QUOTE] Not even close. When you add it to the HUDPaint hook, every time the HUDPaint hook is called, so is that function, and then it's called again when the timer goes off. Try this: [code] local i = 0 hook.Add("HUDPaint", "FlickerHUD", function() i = i + FrameTime() if i > 10 then draw.RoundedBox(5, 10, ScrH() * 0.85, ScrW() * 0.27, 150, Color(math.random(1, 255),math.random(1, 255),math.random(1 ,255), 255)) i = 0 end end)[/code]
I'm confused about a couple points on hooks if anyone would mind taking a second to elaborate. A hook like GM:PlayerSpawn is called when a player is spawned and if you have an implementation in your code for that hook, it will run you code. It gets passed the player class as a variable so you can do things like give them a weapon etc, correct? Now what about a hook like GM:CalcView. Is that called like every tick/every time the screen is updated? If it is and you have an implementation for it such as function GM:CalcView... will it run whats in that function every tick/render update?
[QUOTE=Sean C;39137836]I'm confused about a couple points on hooks if anyone would mind taking a second to elaborate. A hook like GM:PlayerSpawn is called when a player is spawned and if you have an implementation in your code for that hook, it will run you code. It gets passed the player class as a variable so you can do things like give them a weapon etc, correct? Now what about a hook like GM:CalcView. Is that called like every tick/every time the screen is updated? If it is and you have an implementation for it such as function GM:CalcView... will it run whats in that function every tick/render update?[/QUOTE] As far as I know, that's spot on. Every time the player's eye pos/angle needs to be retrieved, the CalcView hook is ran. The default base CalcView gamemode function asks hooks, the player class (set with the [url=http://wiki.garrysmod.com/page/Libraries/player_manager]player_manager library[/url] (see also [url=http://wiki.garrysmod.com/page/Player_Classes]this[/url] for usages)) and any vehicle the player is in for proper positions and angles.
A hook is the same thing as doing [lua] function GM:PlayerSpawn( player ) -- Default PlayerSpawn code that I don't have on hand. MyHook( player ) ASecondHook( player ) end [/lua]
...Without actually having to replace anything. Which is why I'm using the occasional hook as opposed to replacing the gamemode functions in my own gamemode :p I know, ugly. But what can you do? I don't want to have to copy the base function every time I want to add something to it.
Thanks so, continuing on with hooks, is having "function GM:CalcView(vars) *changing around fov*" then same as hook.add("CalcView", "MyCalcView", function that changes fov)"? If they are the same, is the benefit of the second just to keep things cleaner? Although I don't really see how it is an cleaner if thats the case. Or wait, does the hook.add allow you to just tack on extra things to the Calcview because if you were to actually have "function GM:CalcView(vars)..." you would have to set ALL of the things CalcView does on its own without implementation?
[QUOTE=Sean C;39138011]does the hook.add allow you to just tack on extra things to the Calcview because if you were to actually have "function GM:CalcView(vars)..." you would have to set ALL of the things CalcView does on its own without implementation?[/QUOTE] That's right. This is how gamemodes work. You choose a gamemode to base your gamemode off of (by default, unsurprisingly enough, the gamemode called "base"). In simple terms, it loads the base gamemode, whatever it may be, into the GM table and then loads yours. The base CalcView function handles the drive library, the player_manager library, and vehicles. The sandbox gamemode doesn't override it as far as I know, I think garry just used a player class with player_manager for the other things in sandbox (like taunts). [editline]7th January 2013[/editline] Hooks are called before the gamemode (unless you use hook.Call with other parameters yourself) and, should a hook return something, the other hooks are ignored and the gamemode function isn't called. If nothing is returned (nil), the gamemode function is ran. That's how hooks work, usually. [editline]7th January 2013[/editline] Just for reference, here's the default CalcView from gamemodes/base/gamemode/cl_init.lua: [code] --[[--------------------------------------------------------- Name: CalcView Allows override of the default view -----------------------------------------------------------]] function GM:CalcView( ply, origin, angles, fov, znear, zfar ) local Vehicle = ply:GetVehicle() local Weapon = ply:GetActiveWeapon() local view = {} view.origin = origin view.angles = angles view.fov = fov view.znear = znear view.zfar = zfar view.drawviewer = false -- Let the vehicle override the view if ( IsValid( Vehicle ) ) then return GAMEMODE:CalcVehicleView( Vehicle, ply, view ) end -- Let drive possibly alter the view if ( drive.CalcView( ply, view ) ) then return view end -- Give the player manager a turn at altering the view player_manager.RunClass( ply, "CalcView", view ) -- Give the active weapon a go at changing the viewmodel position if ( IsValid( Weapon ) ) then local func = Weapon.GetViewModelPosition if ( func ) then view.vm_origin, view.vm_angles = func( Weapon, origin*1, angles*1 ) -- Note: *1 to copy the object so the child function can't edit it. end local func = Weapon.CalcView if ( func ) then view.origin, view.angles, view.fov = func( Weapon, ply, origin*1, angles*1, fov ) -- Note: *1 to copy the object so the child function can't edit it. end end return view end[/code] And, again, before this function is called, all of the hooks are ran. Meaning, you won't see any special vehicle cameras or player class views if you have a hook that returns all the time.
Thanks you've been a big help. One last question. The example for CalcView on the wiki has this as the body of an example function that gets called as part of the CalcView hook local view = {} view.origin = pos-(angles:Forward()*100) view.angles = angles view.fov = fov return view So as I understand it, view is a variable created that is an array that gets the keys of origin, angles, and fov assigned in it with the corresponding values. The function returns this table but where does it go? After stumbling around on the wiki I assume its pushing that table to the CamData class? If thats the case how does it know to push it there/how can I know? There's no link between the two on the wiki, should it just be assumed that CalcView and CamData are related? In addition I'm only pushing a table with 3 entries rather than the entirety of the CamData table. Does this work because the keys for the values in my "view" table are named to match the ones in CamData? EDIT: Oh ok now that I see the source for the base CalcView I can see how you could tell where the origin variable and such come from
CamData is that table. CalcView, for clarity, uses a table of values [url=http://wiki.garrysmod.com/page/Structures/CamData]here[/url] to calculate view pos/angles/fov/etc. Hooks and the gamemode function use it. [editline]7th January 2013[/editline] Btw, "structures" in gmod are just tables (or as you said, arrays). Nothing special about them. Also, any values you don't fill in the CalcView table will use default engine-determined values, fov will use the console variable for it, pos and ang will be where the player's eye pos is and their eye angles, respectively, the z-values will be engine default.
Ok sweet that's the assumption I was getting at, thanks for the help its been incredibly beneficial!
[QUOTE=Sean C;39138412]Ok sweet that's the assumption I was getting at, thanks for the help its been incredibly beneficial![/QUOTE] Any time, broski. One thing that would be nice is having garry combine some of the hook returns, like the CamData for CalcView, so multiple CalcView hooks wouldn't override each other. [editline]7th January 2013[/editline] Try using that example code from the wiki and getting in a vehicle. That's one of the bad things about hooks, unless garry were to create a unified system where one hook could take precedence over another if it met certain conditions (like the default gamemode hook running if inside a vehicle, and the thirdperson one running if not (though this can be handled in the hook itself, it would still be nice for a system like this, especially for repetitive tasks and conflicting hooks)), hooks will still be pretty inefficient (not saying they aren't useful!). [editline]7th January 2013[/editline] ...Actually, I pretty much just described the AI weight system. Well. [editline]7th January 2013[/editline] Last thing, if you're using hooks in a gamemode (or, well, anywhere), it's best to remove old hooks before making new ones. I would guess that if you add a hook that already exists, you'll have two duplicate hooks. Not good. Put a prefix or something before your hook name so you can identify it later. Here's some code I use in my gamemode's shared.lua: [code] for HookType, Hook in pairs(hook.GetTable()) do for HookName, HookAction in pairs(Hook) do if string.sub(tostring(HookName), 1, 3) == "sc_" then hook.Remove(HookType, HookName) Msg("[SC] Removed " ..(CLIENT and "client" or (SERVER and "server" or "?")).. " hook " ..tostring(HookName).. ".\n") end end end [/code] Using that, all hooks with the prefix "sc_" (for example, hook.Add("CalcView", "sc_HandleCalcViewHooks", etc)) will be removed.
By adding a hook that already exists would you mean like having hook.Add("CalcView" ...1view) in one place in the code and hook.Add("CalcView"...view2) somewhere else? Would that be okay as long as they don't modify the same information or should it just all be contained into one function? Wouldn't removing the hook as in your snippet just remove the new functionality for the hook from the gamemode altogether? Or for your example would you have two different CalcView calls like I just described but run that removal code inbetween them so the first CalcView hook is removed before the second is run?
[code] PS.Config.PointsOverTime = true -- Should players be given points over time? PS.Config.PointsOverTimeDelay = 1 -- If so, how many minutes apart? PS.Config.PointsOverTimeAmount = 10 -- And if so, how many points to give after the time? -- Edit below if you know what you're doing [/code] I want to make it so Donators get like 20 points every minute + 25% off all items. Could you tell me where to edit all of this and if not make a quick code for me? That'd be really awesome.
I'm learning java on codeacademy.com - how similar are java and lua?
[QUOTE=DarkShadow6;39137778] Try this: [code] local i = 0 hook.Add("HUDPaint", "FlickerHUD", function() i = i + FrameTime() if i > 10 then draw.RoundedBox(5, 10, ScrH() * 0.85, ScrW() * 0.27, 150, Color(math.random(1, 255),math.random(1, 255),math.random(1 ,255), 255)) i = 0 end end)[/code][/QUOTE] So, the code you posted works in a sense. The HUD does appear every 10 seconds as a new color, but it only stays there for a tick until it goes away and a new one appears 10 seconds later and does the exact same thing. How would I make it stay there until the next rep? Edit: I tried to put a timer inside that whole function to call i = 0 every 10 seconds..it holds the hud but continously changes the color just like my original one did.
Hey, i have no experience with sents creation. Now i have a problem with this message "Crazy physics on [176][example] [Ang:-1.#IND00,1.#QNAN0,180.000000] [Pos:-1.#IND00,-1.#IND00,1.#QNAN0] - removing" How i set the size of the entity, to fix my issue ? greetz |Royal|
does any one happen to know or have a example of making the socket/plugs snap together and link
[QUOTE=|Royal|;39145803]Hey, i have no experience with sents creation. Now i have a problem with this message "Crazy physics on [176][example] [Ang:-1.#IND00,1.#QNAN0,180.000000] [Pos:-1.#IND00,-1.#IND00,1.#QNAN0] - removing" How i set the size of the entity, to fix my issue ? greetz |Royal|[/QUOTE] Show the code for the Initialize function of your entity.
[CODE]function GM:PlayerSwitchFlashlight(player, on) [B]if player:Arrested or player:KnockedOut then[/B] return false; else return true; end; end;[/CODE] function arguments expected near 'or' This is probably an easy fix I'm just too blind to recognize. Thanks for any help.
Now it works.. [LUA] function ENT:Initialize() self.Entity:SetModel("models/props_lab/blastdoor001b.mdl") self.Entity:SetMaterial("phoenix_storms/bluemetal") self.Entity:SetPos(self:GetPos()) self.Entity:SetModelScale(self.Entity:GetModelScale() ,1) // i am not sure but set this the size of the sent ?? self.Entity:SetAngles(Angle(0,0,0)) self.Entity:PhysicsInit( SOLID_VPHYSICS ) self.Entity:SetMoveType( MOVETYPE_VPHYSICS ) self.Entity:SetSolid( SOLID_VPHYSICS ) local phy = self.Entity:GetPhysicsObject() if phy:IsValid() then phy:Wake() end self.Entity:SetTrigger(true) end [/LUA] But the material dosent work, it is invisible. [editline]8th January 2013[/editline] [QUOTE=ds;39147320][CODE]function GM:PlayerSwitchFlashlight(player, on) [B]if player:Arrested or player:KnockedOut then[/B] return false; else return true; end; end;[/CODE] function arguments expected near 'or' This is probably an easy fix I'm just too blind to recognize. Thanks for any help.[/QUOTE] [LUA] function GM:PlayerSwitchFlashlight(player, on) if player:Arrested() or player:KnockedOut() then return false; else return true; end; end [/LUA]
[QUOTE=|Royal|;39147517]Now it works.. [LUA] function ENT:Initialize() self.Entity:SetModel("models/props_lab/blastdoor001b.mdl") self.Entity:SetMaterial("phoenix_storms/bluemetal") self.Entity:SetPos(self:GetPos()) self.Entity:SetModelScale(self.Entity:GetModelScale() ,1) // i am not sure but set this the size of the sent ?? self.Entity:SetAngles(Angle(0,0,0)) self.Entity:PhysicsInit( SOLID_VPHYSICS ) self.Entity:SetMoveType( MOVETYPE_VPHYSICS ) self.Entity:SetSolid( SOLID_VPHYSICS ) local phy = self.Entity:GetPhysicsObject() if phy:IsValid() then phy:Wake() end self.Entity:SetTrigger(true) end [/LUA] But the material dosent work, it is invisible. [editline]8th January 2013[/editline] [LUA] function GM:PlayerSwitchFlashlight(player, on) if player:Arrested() or player:KnockedOut() then return false; else return true; end; end [/LUA][/QUOTE] Thank you Edit: Need help with one last thing attempt to index field 'configuration' a nil value [CODE]function GM:Think() if ( gamemode.configuration["Local Voice"] ) then for k, v in pairs( player.GetAll() ) do if ( hook.Call("PlayerCanVoice", GAMEMODE, v) ) then if ( v:IsMuted() ) then v:SetMuted(); end; else if ( !v:IsMuted() ) then v:SetMuted(); end; end; end; end;[/CODE]
I want a derma window when I click a button and I don't know how to do it. [LUA]frame = vgui.Create( "DFrame" ) frame:SetPos( ScrW() / 2 - 100, ScrH() / 2 - 200) frame:SetSize( 200, 520 ) frame:SetTitle( "Choose Team & Class" ) frame:SetVisible( true ) frame:SetDraggable( false ) frame:ShowCloseButton( true ) frame:MakePopup() Terrorist = vgui.Create( "DButton", frame ) Terrorist:SetPos( 30, 30 ) Terrorist:SetSize( 140, 70 ) Terrorist:SetText( "Terrorist: Assault" ) Terrorist.DoClick = function() RunConsoleCommand( "TerroristAssault" ) end [/LUA]
You know that "gamemode" is an librarie [URL]http://wiki.garrysmod.com/page/Libraries/gamemode[/URL] and you can try this [LUA] // shared.lua GM.configuration = true // true or false for default // your file function GM:Think() if (self.configuration ) then for k, v in pairs( player.GetAll() ) do if ( hook.Call("PlayerCanVoice", GAMEMODE, v) ) then // sure that works ? is PlayerCanVoice exists ? if ( v:IsMuted() ) then v:SetMuted(); end; else if ( !v:IsMuted() ) then v:SetMuted(); end; end; end; end; [/LUA] [editline]9th January 2013[/editline] [QUOTE=lope;39147868]I want a derma window when I click a button and I don't know how to do it. [LUA]frame = vgui.Create( "DFrame" ) frame:SetPos( ScrW() / 2 - 100, ScrH() / 2 - 200) frame:SetSize( 200, 520 ) frame:SetTitle( "Choose Team & Class" ) frame:SetVisible( true ) frame:SetDraggable( false ) frame:ShowCloseButton( true ) frame:MakePopup() Terrorist = vgui.Create( "DButton", frame ) Terrorist:SetPos( 30, 30 ) Terrorist:SetSize( 140, 70 ) Terrorist:SetText( "Terrorist: Assault" ) Terrorist.DoClick = function() RunConsoleCommand( "TerroristAssault" ) end [/LUA][/QUOTE] [LUA] frame = vgui.Create( "DFrame" ) frame:SetPos( ScrW() / 2 - 100, ScrH() / 2 - 200) frame:SetSize( 200, 520 ) frame:SetTitle( "Choose Team & Class" ) frame:SetVisible( true ) frame:SetDraggable( false ) frame:ShowCloseButton( true ) frame:MakePopup() Terrorist = vgui.Create( "DButton", frame ) Terrorist:SetPos( 30, 30 ) Terrorist:SetSize( 140, 70 ) Terrorist:SetText( "Terrorist: Assault" ) Terrorist.DoClick = function() child = vgui.Create( "DFrame" ) child:SetPos( ScrW() / 2 - 100, ScrH() / 2 - 200) child:SetSize( 200, 520 ) child:SetTitle( "Hey mum" ) child:SetDraggable( true) child:ShowCloseButton( true ) child:MakePopup() RunConsoleCommand( "TerroristAssault" ) end [/LUA]
I have a beam that draws a red line from the players eyes to the hitpos of the trace but the problem is each beam is rendered permanently. I want to have this beam essentially work like a laser on a weapon so it needs to create itself each time the screen is update, and then remove itself before the next screen update where the updated laser is drawn. Is there a simple way to have an effect exist temporarily? Like a remove function I can call at the end of the laser.lua effect? EDIT: Nevermind, it seems I can just put effect.think to false right?
[URL]http://wiki.garrysmod.com/page/Libraries/gamemode[/URL] and you can try this [LUA] // shared.lua GM.configuration = true // true or false for default // your file function GM:Think() if (self.configuration ) then for k, v in pairs( player.GetAll() ) do if ( hook.Call("PlayerCanVoice", GAMEMODE, v) ) then // sure that works ? is PlayerCanVoice exists ? if ( v:IsMuted() ) then v:SetMuted(); end; else if ( !v:IsMuted() ) then v:SetMuted(); end; end; end; end; [/LUA] [editline]9th January 2013[/editline] I don't have a 'shared.lua' but i have 'sh_configuration.lua' so put that line(GM.configuration = true // true or false for default) there instead of shared.lua?
[QUOTE=Sean C;39148046]I have a beam that draws a red line from the players eyes to the hitpos of the trace but the problem is each beam is rendered permanently. I want to have this beam essentially work like a laser on a weapon so it needs to create itself each time the screen is update, and then remove itself before the next screen update where the updated laser is drawn. Is there a simple way to have an effect exist temporarily? Like a remove function I can call at the end of the laser.lua effect? EDIT: Nevermind, it seems I can just put effect.think to false right?[/QUOTE] Hey Hey wiki :D [URL]http://wiki.garrysmod.com/page/Classes/Player/GetViewModel[/URL]
[QUOTE=|Royal|;39147953] [LUA] frame = vgui.Create( "DFrame" ) frame:SetPos( ScrW() / 2 - 100, ScrH() / 2 - 200) frame:SetSize( 200, 520 ) frame:SetTitle( "Choose Team & Class" ) frame:SetVisible( true ) frame:SetDraggable( false ) frame:ShowCloseButton( true ) frame:MakePopup() Terrorist = vgui.Create( "DButton", frame ) Terrorist:SetPos( 30, 30 ) Terrorist:SetSize( 140, 70 ) Terrorist:SetText( "Terrorist: Assault" ) Terrorist.DoClick = function() child = vgui.Create( "DFrame" ) child:SetPos( ScrW() / 2 - 100, ScrH() / 2 - 200) child:SetSize( 200, 520 ) child:SetTitle( "Hey mum" ) child:SetDraggable( true) child:ShowCloseButton( true ) child:MakePopup() RunConsoleCommand( "TerroristAssault" ) end [/LUA][/QUOTE] That just creates another window infront of the other one, I want all windows to close.
[QUOTE=lope;39148160]That just creates another window infront of the other one, I want all windows to close.[/QUOTE] [LUA]child:SetPos( ScrW() / 2 + 105, ScrH() / 2 - 200)[/LUA]
[QUOTE=|Royal|;39148132]Hey Hey wiki :D [URL]http://wiki.garrysmod.com/page/Classes/Player/GetViewModel[/URL][/QUOTE] Hehe you seemed to solve my exact example XD What I'm actually trying to do is have a function thats called through the think hook. Each time this function is called it uses util.Effect to draw the beam. I need a way to remove the beam once its drawn though so is there a way within the function I can give the uti.Effect a variable name and then doign something like beam:remove? Or can I somehow put a remove call into the render function of the effect itself? Or like each time the think function is called do a remove of existing effects named beam and then draw them again? I cant syntactically figure it out.
Sorry, you need to Log In to post a reply to this thread.