• Replacing LocalPlayer
    16 replies, posted
Hey! I have a code for cl_init.lua for a prop, there is the LocalPlayer, so i want it to be replaced with all players, how would i do like that? Thank you!
player.GetAll()
[QUOTE=Banhfunbags;44730151]player.GetAll()[/QUOTE] So i replace every LocalPlayer to player.GetAll()? The code makes "Hud". Here some additional info. [URL="http://facepunch.com/showthread.php?t=1387398&p=44603079#post44603079"]http://facepunch.com/showthread.php?t=1387398&p=44603079#post44603079[/URL]
LocalPlayer( ) is needed in that snippet so that it can draw the list - It stops drawing it at a certain distance, and it starts fading before it completely disappears at that distance..... Add bots to your server and you'll see the list populate...
I understand what it does, i want to put it at autorun and spawn at my pos, but error is the LocalPlayer as it is null, how i can show that list to everyone??
At the top of the ENT:Draw( ), do if ( !IsValid( LocalPlayer( ) ) ) then return; end Then it'll work; for all players when that entity is in the server.
[QUOTE=Acecool;44731033]At the top of the ENT:Draw( ), do if ( !IsValid( LocalPlayer( ) ) ) then return; end Then it'll work; for all players when that entity is in the server.[/QUOTE] Thank you, do i replace LocalPlayer? As i actually dont put it to cl_init and i understand LocalPlayer is the spawner, i just want to put this into autorun, so everyone could see. I have ang and pos setted, but i dont fully understand how the player is valid when not setted it?
Replace all of the code in cl_init with this. [lua]--[[----------------------------------------------------------- Here's a small example I just made to help you get started. This is the cl_init of a entity, you could hook into other rendering methods to draw a floating hud in the world. But I like using entities because you can drag them arround when finding a place to put the 3d2d. This is all handled client side so I shouldn't need to paste the init.lua of my example entity. Happy coding --]]----------------------------------------------------------- function ENT:Draw() if (!LocalPlayer():IsValid()) then return; end self.Entity:DrawModel() if LocalPlayer():EyePos( ):Distance(self.Entity:GetPos()) > 1000 then return end -- don't render if player is far away -- Example of adding dynamics to it, background fades in as you get closer local FadeAlpha = math.Clamp(500 - LocalPlayer():EyePos( ):Distance(self.Entity:GetPos())/2, 0 , 150) local ang = self.Entity:GetAngles() local pos = self.Entity:GetPos() local drawang = self.Entity:GetAngles() drawang:RotateAroundAxis(self.Entity:GetAngles():Forward(), 90) -- set up rotation for rendering cam.Start3D2D(pos + ang:Up()*75, drawang, 0.20) --Background surface.SetDrawColor(128, 255, 0, FadeAlpha ) surface.DrawRect(-295, -195, 590, 390) --Dark Lines surface.SetDrawColor( 50, 50, 50, 255 ) surface.DrawRect(-300, -200, 400, 10) surface.DrawRect(-300, -200, 10, 100) surface.DrawRect(290, 200, 10, -100) surface.DrawRect(290, 190, -400, 10) --Player List (up to 24 names) for k,v in pairs(player.GetAll()) do local offset = -275 local rowreset = 0 if k > 24 then break elseif k > 12 then offset = 20 rowreset = 12 end draw.DrawText( v:Nick(), "DermaLarge", offset, -215 +(k-rowreset)*30, Color(0, 162, 232, 255), TEXT_ALIGN_LEFT ) draw.DrawText( v:Ping(), "DermaLarge", offset+200, -215 +(k-rowreset)*30, Color(0, 162, 232, 255), TEXT_ALIGN_LEFT ) end cam.End3D2D() -- surface.DrawRect render stuff on both sides -- draw.DrawText Does not so we need to render the names on the other side drawang:RotateAroundAxis(self.Entity:GetAngles():Up(), 180) --Rotate to render text on other side cam.Start3D2D(pos + ang:Up()*75, drawang, 0.20) --Player List (up to 24 names) same stuff as above for k,v in pairs(player.GetAll()) do local offset = -275 local rowreset = 0 if k > 24 then break elseif k > 12 then offset = 20 rowreset = 12 end draw.DrawText( v:Nick(), "DermaLarge", offset, -215 +(k-rowreset)*30, Color(0, 162, 232, 255), TEXT_ALIGN_LEFT ) draw.DrawText( v:Ping(), "DermaLarge", offset+200, -215 +(k-rowreset)*30, Color(0, 162, 232, 255), TEXT_ALIGN_LEFT ) end cam.End3D2D() end [/lua]
[QUOTE=KingofBeast;44734829]Replace all of the code in cl_init with this. [lua]--[[----------------------------------------------------------- Here's a small example I just made to help you get started. This is the cl_init of a entity, you could hook into other rendering methods to draw a floating hud in the world. But I like using entities because you can drag them arround when finding a place to put the 3d2d. This is all handled client side so I shouldn't need to paste the init.lua of my example entity. Happy coding --]]----------------------------------------------------------- function ENT:Draw() if (!LocalPlayer():IsValid()) then return; end self.Entity:DrawModel() if LocalPlayer():EyePos( ):Distance(self.Entity:GetPos()) > 1000 then return end -- don't render if player is far away -- Example of adding dynamics to it, background fades in as you get closer local FadeAlpha = math.Clamp(500 - LocalPlayer():EyePos( ):Distance(self.Entity:GetPos())/2, 0 , 150) local ang = self.Entity:GetAngles() local pos = self.Entity:GetPos() local drawang = self.Entity:GetAngles() drawang:RotateAroundAxis(self.Entity:GetAngles():Forward(), 90) -- set up rotation for rendering cam.Start3D2D(pos + ang:Up()*75, drawang, 0.20) --Background surface.SetDrawColor(128, 255, 0, FadeAlpha ) surface.DrawRect(-295, -195, 590, 390) --Dark Lines surface.SetDrawColor( 50, 50, 50, 255 ) surface.DrawRect(-300, -200, 400, 10) surface.DrawRect(-300, -200, 10, 100) surface.DrawRect(290, 200, 10, -100) surface.DrawRect(290, 190, -400, 10) --Player List (up to 24 names) for k,v in pairs(player.GetAll()) do local offset = -275 local rowreset = 0 if k > 24 then break elseif k > 12 then offset = 20 rowreset = 12 end draw.DrawText( v:Nick(), "DermaLarge", offset, -215 +(k-rowreset)*30, Color(0, 162, 232, 255), TEXT_ALIGN_LEFT ) draw.DrawText( v:Ping(), "DermaLarge", offset+200, -215 +(k-rowreset)*30, Color(0, 162, 232, 255), TEXT_ALIGN_LEFT ) end cam.End3D2D() -- surface.DrawRect render stuff on both sides -- draw.DrawText Does not so we need to render the names on the other side drawang:RotateAroundAxis(self.Entity:GetAngles():Up(), 180) --Rotate to render text on other side cam.Start3D2D(pos + ang:Up()*75, drawang, 0.20) --Player List (up to 24 names) same stuff as above for k,v in pairs(player.GetAll()) do local offset = -275 local rowreset = 0 if k > 24 then break elseif k > 12 then offset = 20 rowreset = 12 end draw.DrawText( v:Nick(), "DermaLarge", offset, -215 +(k-rowreset)*30, Color(0, 162, 232, 255), TEXT_ALIGN_LEFT ) draw.DrawText( v:Ping(), "DermaLarge", offset+200, -215 +(k-rowreset)*30, Color(0, 162, 232, 255), TEXT_ALIGN_LEFT ) end cam.End3D2D() end [/lua][/QUOTE] Ill say this clearly, i dont want cl_init. I want to put this on my server in autorun, so all players will see the hud
[QUOTE=SteppuFIN;44736221]Ill say this clearly, i dont want cl_init. I want to put this on my server in autorun, so all players will see the hud[/QUOTE]Its clientside code so it needs to be run on the client otherwise it will not work. You can't have it serverside. That code will work for all players, just make sure you run it correctly.
The file cl_init is run on every game, so whatever you put in there will be run locally, but on every client. You have to make sure to put it in a hook that is called during rendering though, such as ENT:Draw.
cl_init aint in every gamemode? Just AddCSLuaFile("custom_cl_init.lua") instead of AddCSLuaFile("cl_init.lua") but normally its used like cl_init
[QUOTE=SarahKerrigan;44737765]cl_init aint in every gamemode? Just AddCSLuaFile("custom_cl_init.lua") instead of AddCSLuaFile("cl_init.lua") but normally its used like cl_init[/QUOTE] You, thats what i need, but where i put the AddCSLuaFile? you all dont tell that. But surely this is what i need.
You place it on top the file. If you want to include the current file, you can just do AddCSLuaFile() without putting any names.
-snip-
[QUOTE=SteppuFIN;44738685]You, thats what i need, but where i put the AddCSLuaFile? you all dont tell that. But surely this is what i need.[/QUOTE] They mistakenly assumed you know how google works. [url]http://wiki.garrysmod.com/page/Global/AddCSLuaFile[/url]
[QUOTE=Hoffa1337;44744414]They mistakenly assumed you know how google works. [url]http://wiki.garrysmod.com/page/Global/AddCSLuaFile[/url][/QUOTE] I forgot for an second that google exists xD
Sorry, you need to Log In to post a reply to this thread.